blob: 7430d481f973d008ebd349fa75316b4596edff1c [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'o36585792008-06-07 22:07:50 -040048#include <limits.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000049
Theodore Ts'o54c637d2001-05-14 11:45:38 +000050#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000052#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000053#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000055#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050056#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050057#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000059#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000060
61#define STRIDE_LENGTH 8
62
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000063#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000064#define ZAP_BOOTBLOCK
65#endif
66
Theodore Ts'o3839e651997-04-26 13:21:57 +000067extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000068extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
70const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000071const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072
73/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000074int cflag;
75int verbose;
76int quiet;
77int super_only;
78int force;
79int noaction;
80int journal_size;
81int journal_flags;
Theodore Ts'oa4396e92008-04-18 10:19:27 -040082int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000083char *bad_blocks_filename;
84__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050086struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000087char *creator_os;
88char *volume_label;
89char *mount_dir;
90char *journal_device;
91int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053092char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +000093
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050094profile_t profile;
95
Theodore Ts'o31e29a12002-05-17 10:53:07 -040096int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040097int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040098
Theodore Ts'o63985322001-01-03 17:02:13 +000099static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500101 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400102 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500103 "[-J journal-options]\n"
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400104 "\t[-G meta group size] [-N number-of-inodes]\n"
105 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
106 "\t[-g blocks-per-group] [-L volume-label] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400107 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500108 "[-r fs-revision] [-E extended-option[,...]]\n"
109 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 program_name);
111 exit(1);
112}
113
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000114static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115{
116 int l = 0;
117
118 arg >>= 1;
119 while (arg) {
120 l++;
121 arg >>= 1;
122 }
123 return l;
124}
125
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000126static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000127{
128 int l;
129
130 for (l=0; arg ; l++)
131 arg = arg / 10;
132 return l;
133}
134
Theodore Ts'od99225e2004-09-25 07:40:12 -0400135static int parse_version_number(const char *s)
136{
137 int major, minor, rev;
138 char *endptr;
139 const char *cp = s;
140
141 if (!s)
142 return 0;
143 major = strtol(cp, &endptr, 10);
144 if (cp == endptr || *endptr != '.')
145 return 0;
146 cp = endptr + 1;
147 minor = strtol(cp, &endptr, 10);
148 if (cp == endptr || *endptr != '.')
149 return 0;
150 cp = endptr + 1;
151 rev = strtol(cp, &endptr, 10);
152 if (cp == endptr)
153 return 0;
154 return ((((major * 256) + minor) * 256) + rev);
155}
156
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000157/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158 * Helper function for read_bb_file and test_disk
159 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500160static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161{
Theodore Ts'o66938372002-03-08 00:14:46 -0500162 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 return;
164}
165
166/*
167 * Reads the bad blocks list from a file
168 */
169static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
170 const char *bad_blocks_file)
171{
172 FILE *f;
173 errcode_t retval;
174
175 f = fopen(bad_blocks_file, "r");
176 if (!f) {
177 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000178 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 exit(1);
180 }
181 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
182 fclose (f);
183 if (retval) {
184 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000185 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 exit(1);
187 }
188}
189
190/*
191 * Runs the badblocks program to test the disk
192 */
193static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
194{
195 FILE *f;
196 errcode_t retval;
197 char buf[1024];
198
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400199 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500200 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500201 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000203 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 f = popen(buf, "r");
205 if (!f) {
206 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400207 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 exit(1);
209 }
210 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000211 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000212 if (retval) {
213 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000214 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000215 exit(1);
216 }
217}
218
219static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
220{
Theodore Ts'o54434922003-12-07 01:28:50 -0500221 dgrp_t i;
222 blk_t j;
223 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224 blk_t blk;
225 badblocks_iterate bb_iter;
226 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000227 blk_t group_block;
228 int group;
229 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230
231 if (!bb_list)
232 return;
233
234 /*
235 * The primary superblock and group descriptors *must* be
236 * good; if not, abort.
237 */
238 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
239 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000240 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000241 fprintf(stderr, _("Block %d in primary "
242 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400243 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000244 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500246 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 exit(1);
248 }
249 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000250
251 /*
252 * See if any of the bad blocks are showing up in the backup
253 * superblocks and/or group descriptors. If so, issue a
254 * warning and adjust the block counts appropriately.
255 */
256 group_block = fs->super->s_first_data_block +
257 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258
259 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000260 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000261 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000262 if (ext2fs_badblocks_list_test(bb_list,
263 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000264 if (!group_bad)
265 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500266_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000267" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000268 group_block);
269 group_bad++;
270 group = ext2fs_group_of_blk(fs, group_block+j);
271 fs->group_desc[group].bg_free_blocks_count++;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400272 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000273 fs->super->s_free_blocks_count++;
274 }
275 }
276 group_block += fs->super->s_blocks_per_group;
277 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278
279 /*
280 * Mark all the bad blocks as used...
281 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000282 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000284 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000285 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 exit(1);
287 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000288 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000290 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291}
292
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000293/*
294 * These functions implement a generalized progress meter.
295 */
296struct progress_struct {
297 char format[20];
298 char backup[80];
299 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400300 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000301};
302
303static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500304 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000306 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000307
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000308 memset(progress, 0, sizeof(struct progress_struct));
309 if (quiet)
310 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000311
312 /*
313 * Figure out how many digits we need
314 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000315 i = int_log10(max);
316 sprintf(progress->format, "%%%dd/%%%dld", i, i);
317 memset(progress->backup, '\b', sizeof(progress->backup)-1);
318 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500319 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000320 progress->backup[(2*i)+1] = 0;
321 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000322
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400323 progress->skip_progress = 0;
324 if (getenv("MKE2FS_SKIP_PROGRESS"))
325 progress->skip_progress++;
326
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000327 fputs(label, stdout);
328 fflush(stdout);
329}
330
331static void progress_update(struct progress_struct *progress, __u32 val)
332{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400333 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000334 return;
335 printf(progress->format, val, progress->max);
336 fputs(progress->backup, stdout);
337}
338
339static void progress_close(struct progress_struct *progress)
340{
341 if (progress->format[0] == 0)
342 return;
343 fputs(_("done \n"), stdout);
344}
345
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400346static void write_inode_tables(ext2_filsys fs, int lazy_flag)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000347{
348 errcode_t retval;
349 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500350 dgrp_t i;
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400351 int num, ipb;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000352 struct progress_struct progress;
353
354 if (quiet)
355 memset(&progress, 0, sizeof(progress));
356 else
357 progress_init(&progress, _("Writing inode tables: "),
358 fs->group_desc_count);
359
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000361 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000362
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000363 blk = fs->group_desc[i].bg_inode_table;
364 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000365
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400366 if (lazy_flag) {
367 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400368 num = ((((fs->super->s_inodes_per_group -
369 fs->group_desc[i].bg_itable_unused) *
370 EXT2_INODE_SIZE(fs->super)) +
371 EXT2_BLOCK_SIZE(fs->super) - 1) /
372 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400373 } else {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500374 /* The kernel doesn't need to zero the itable blocks */
375 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400376 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000377 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530378 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400379 if (retval) {
380 fprintf(stderr, _("\nCould not write %d "
381 "blocks in inode table starting at %u: %s\n"),
382 num, blk, error_message(retval));
383 exit(1);
384 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000385 if (sync_kludge) {
386 if (sync_kludge == 1)
387 sync();
388 else if ((i % sync_kludge) == 0)
389 sync();
390 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530392 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000393 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394}
395
396static void create_root_dir(ext2_filsys fs)
397{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400398 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000399 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400400 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401
402 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
403 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000404 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 exit(1);
406 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000407 if (geteuid()) {
408 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
409 if (retval) {
410 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000411 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000412 exit(1);
413 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400414 uid = getuid();
415 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500416 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400417 if (uid) {
418 gid = getgid();
419 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500420 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400421 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500422 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000423 if (retval) {
424 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000425 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000426 exit(1);
427 }
428 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429}
430
431static void create_lost_and_found(ext2_filsys fs)
432{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400433 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000435 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 const char *name = "lost+found";
437 int i;
438
Theodore Ts'o6a525062001-12-24 09:40:00 -0500439 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
441 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000442 com_err("ext2fs_mkdir", retval,
443 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 exit(1);
445 }
446
447 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
448 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000449 com_err("ext2_lookup", retval,
450 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 exit(1);
452 }
453
454 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100455 /* Ensure that lost+found is at least 2 blocks, so we always
456 * test large empty blocks for big-block filesystems. */
457 if ((lpf_size += fs->blocksize) >= 16*1024 &&
458 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000459 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 retval = ext2fs_expand_dir(fs, ino);
461 if (retval) {
462 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000463 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 exit(1);
465 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500466 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467}
468
469static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
470{
471 errcode_t retval;
472
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400474 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 retval = ext2fs_update_bb_inode(fs, bb_list);
476 if (retval) {
477 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000478 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479 exit(1);
480 }
481
482}
483
484static void reserve_inodes(ext2_filsys fs)
485{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000486 ext2_ino_t i;
487 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400489 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
490 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 ext2fs_mark_ib_dirty(fs);
492}
493
Theodore Ts'o756df352002-03-07 20:52:12 -0500494#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500495#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500496#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500497
Theodore Ts'o04a96852001-08-30 21:55:26 -0400498static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400500 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500502 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000503
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400504 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600505 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500506 printf(_("Out of memory erasing sectors %d-%d\n"),
507 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600508 exit(1);
509 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500510
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500511 if (sect == 0) {
512 /* Check for a BSD disklabel, and don't erase it if so */
513 retval = io_channel_read_blk(fs->io, 0, -512, buf);
514 if (retval)
515 fprintf(stderr,
516 _("Warning: could not read block 0: %s\n"),
517 error_message(retval));
518 else {
519 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
520 if ((*magic == BSD_DISKMAGIC) ||
521 (*magic == BSD_MAGICDISK))
522 return;
523 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500524 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500525
Theodore Ts'o70988102002-07-14 08:00:00 -0400526 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000527 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400528 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000529 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400530 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500532 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
533 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000534}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000535
536static void create_journal_dev(ext2_filsys fs)
537{
538 struct progress_struct progress;
539 errcode_t retval;
540 char *buf;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530541 blk_t blk, err_blk;
542 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000543
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000544 retval = ext2fs_create_journal_superblock(fs,
545 fs->super->s_blocks_count, 0, &buf);
546 if (retval) {
547 com_err("create_journal_dev", retval,
548 _("while initializing journal superblock"));
549 exit(1);
550 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000551 if (quiet)
552 memset(&progress, 0, sizeof(progress));
553 else
554 progress_init(&progress, _("Zeroing journal device: "),
555 fs->super->s_blocks_count);
556
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530557 blk = 0;
558 count = fs->super->s_blocks_count;
559 while (count > 0) {
560 if (count > 1024)
561 c = 1024;
562 else
563 c = count;
564 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
565 if (retval) {
566 com_err("create_journal_dev", retval,
567 _("while zeroing journal device "
568 "(block %u, count %d)"),
569 err_blk, err_count);
570 exit(1);
571 }
572 blk += c;
573 count -= c;
574 progress_update(&progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000575 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530576 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000577
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000578 retval = io_channel_write_blk(fs->io,
579 fs->super->s_first_data_block+1,
580 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000581 if (retval) {
582 com_err("create_journal_dev", retval,
583 _("while writing journal superblock"));
584 exit(1);
585 }
586 progress_close(&progress);
587}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000588
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589static void show_stats(ext2_filsys fs)
590{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000591 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000592 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500593 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500595 dgrp_t i;
596 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500598 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500599 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500600 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000601
602 memset(buf, 0, sizeof(buf));
603 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000604 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500605 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500606 os = e2p_os2string(fs->super->s_creator_os);
607 fputs(os, stdout);
608 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000609 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000610 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000611 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000612 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000613 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000614 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000616 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 s->s_r_blocks_count,
618 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000619 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500620 if (s->s_reserved_gdt_blocks)
621 printf(_("Maximum filesystem blocks=%lu\n"),
622 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200623 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000624 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000625 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000626 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000627 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000628 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000630 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631
632 if (fs->group_desc_count == 1) {
633 printf("\n");
634 return;
635 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500636
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000637 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 group_block = s->s_first_data_block;
639 col_left = 0;
640 for (i = 1; i < fs->group_desc_count; i++) {
641 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000642 if (!ext2fs_bg_has_super(fs, i))
643 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000644 if (i != 1)
645 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000646 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000647 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000649 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000651 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000652 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653 }
654 printf("\n\n");
655}
656
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000657/*
658 * Set the S_CREATOR_OS field. Return true if OS is known,
659 * otherwise, 0.
660 */
661static int set_os(struct ext2_super_block *sb, char *os)
662{
663 if (isdigit (*os))
664 sb->s_creator_os = atoi (os);
665 else if (strcasecmp(os, "linux") == 0)
666 sb->s_creator_os = EXT2_OS_LINUX;
667 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
668 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500669 else if (strcasecmp(os, "freebsd") == 0)
670 sb->s_creator_os = EXT2_OS_FREEBSD;
671 else if (strcasecmp(os, "lites") == 0)
672 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000673 else
674 return 0;
675 return 1;
676}
677
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000678#define PATH_SET "PATH=/sbin"
679
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500680static void parse_extended_opts(struct ext2_super_block *param,
681 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000682{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400683 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000684 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500685 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000686
687 len = strlen(opts);
688 buf = malloc(len+1);
689 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500690 fprintf(stderr,
691 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000692 exit(1);
693 }
694 strcpy(buf, opts);
695 for (token = buf; token && *token; token = next) {
696 p = strchr(token, ',');
697 next = 0;
698 if (p) {
699 *p = 0;
700 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500701 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000702 arg = strchr(token, '=');
703 if (arg) {
704 *arg = 0;
705 arg++;
706 }
707 if (strcmp(token, "stride") == 0) {
708 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500709 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500710 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000711 continue;
712 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500713 param->s_raid_stride = strtoul(arg, &p, 0);
714 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000715 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400716 _("Invalid stride parameter: %s\n"),
717 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500718 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000719 continue;
720 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500721 } else if (strcmp(token, "stripe-width") == 0 ||
722 strcmp(token, "stripe_width") == 0) {
723 if (!arg) {
724 r_usage++;
725 badopt = token;
726 continue;
727 }
728 param->s_raid_stripe_width = strtoul(arg, &p, 0);
729 if (*p || (param->s_raid_stripe_width == 0)) {
730 fprintf(stderr,
731 _("Invalid stripe-width parameter: %s\n"),
732 arg);
733 r_usage++;
734 continue;
735 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500736 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500737 unsigned long resize, bpg, rsv_groups;
738 unsigned long group_desc_count, desc_blocks;
739 unsigned int gdpb, blocksize;
740 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500741
742 if (!arg) {
743 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500744 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500745 continue;
746 }
747
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500748 resize = parse_num_blocks(arg,
749 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500750
751 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500752 fprintf(stderr,
753 _("Invalid resize parameter: %s\n"),
754 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500755 r_usage++;
756 continue;
757 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500758 if (resize <= param->s_blocks_count) {
759 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400760 _("The resize maximum must be greater "
761 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500762 r_usage++;
763 continue;
764 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500765
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500766 blocksize = EXT2_BLOCK_SIZE(param);
767 bpg = param->s_blocks_per_group;
768 if (!bpg)
769 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200770 gdpb = EXT2_DESC_PER_BLOCK(param);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400771 group_desc_count =
772 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500773 desc_blocks = (group_desc_count +
774 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400775 rsv_groups = ext2fs_div_ceil(resize, bpg);
776 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500777 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500778 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500779 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
780
781 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400782 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
783 fprintf(stderr,
784 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400785 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400786 exit(1);
787 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500788 param->s_feature_compat |=
789 EXT2_FEATURE_COMPAT_RESIZE_INODE;
790
791 param->s_reserved_gdt_blocks = rsv_gdb;
792 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500793 } else if (!strcmp(token, "test_fs")) {
794 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400795 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400796 if (arg)
797 lazy_itable_init = strtoul(arg, &p, 0);
798 else
799 lazy_itable_init = 1;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500800 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500801 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500802 badopt = token;
803 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000804 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500805 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500806 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400807 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000808 "and may take an argument which\n"
809 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400810 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500811 "\tstride=<RAID per-disk data chunk in blocks>\n"
812 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400813 "\tresize=<resize maximum size in blocks>\n"
814 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
815 "\ttest_fs\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400816 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400817 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000818 exit(1);
819 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500820 if (param->s_raid_stride &&
821 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
822 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
823 "multiple of stride %u.\n\n"),
824 param->s_raid_stripe_width, param->s_raid_stride);
825
Brian Behlendorf21400382007-05-31 11:30:47 -0400826 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000827}
828
Theodore Ts'o896938d1999-10-23 01:04:50 +0000829static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500830 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400831 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500832 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400833 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500834 EXT2_FEATURE_COMPAT_EXT_ATTR,
835 /* Incompat */
836 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500837 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400838 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500839 EXT2_FEATURE_INCOMPAT_META_BG|
840 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500841 /* R/O compat */
842 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500843 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
844 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000845};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000846
847
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500848static void syntax_err_report(const char *filename, long err, int line_num)
849{
850 fprintf(stderr,
851 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
852 filename, line_num, error_message(err));
853 exit(1);
854}
855
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200856static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500857
858static void edit_feature(const char *str, __u32 *compat_array)
859{
860 if (!str)
861 return;
862
863 if (e2p_edit_feature(str, compat_array, ok_features)) {
864 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
865 str);
866 exit(1);
867 }
868}
869
Theodore Ts'o3d438362008-02-19 08:32:58 -0500870struct str_list {
871 char **list;
872 int num;
873 int max;
874};
875
876static errcode_t init_list(struct str_list *sl)
877{
878 sl->num = 0;
879 sl->max = 0;
880 sl->list = malloc((sl->max+1) * sizeof(char *));
881 if (!sl->list)
882 return ENOMEM;
883 sl->list[0] = 0;
884 return 0;
885}
886
887static errcode_t push_string(struct str_list *sl, const char *str)
888{
889 char **new_list;
890
891 if (sl->num >= sl->max) {
892 sl->max += 2;
893 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
894 if (!new_list)
895 return ENOMEM;
896 sl->list = new_list;
897 }
898 sl->list[sl->num] = malloc(strlen(str)+1);
899 if (sl->list[sl->num] == 0)
900 return ENOMEM;
901 strcpy(sl->list[sl->num], str);
902 sl->num++;
903 sl->list[sl->num] = 0;
904 return 0;
905}
906
907static void print_str_list(char **list)
908{
909 char **cpp;
910
911 for (cpp = list; *cpp; cpp++) {
912 printf("'%s'", *cpp);
913 if (cpp[1])
914 fputs(", ", stdout);
915 }
916 fputc('\n', stdout);
917}
918
919static char **parse_fs_type(const char *fs_type,
920 const char *usage_types,
921 struct ext2_super_block *fs_param,
922 char *progname)
923{
924 const char *ext_type = 0;
925 char *parse_str;
926 char *profile_type = 0;
927 char *cp, *t;
928 const char *size_type;
929 struct str_list list;
930 int state = 0;
931 unsigned long meg;
932
933 if (init_list(&list))
934 return 0;
935
936 if (fs_type)
937 ext_type = fs_type;
938 else if (progname) {
939 ext_type = strrchr(progname, '/');
940 if (ext_type)
941 ext_type++;
942 else
943 ext_type = progname;
944
945 if (!strncmp(ext_type, "mkfs.", 5)) {
946 ext_type += 5;
947 if (ext_type[0] == 0)
948 ext_type = 0;
949 } else
950 ext_type = 0;
951 }
952
953 if (!ext_type) {
954 profile_get_string(profile, "defaults", "fs_type", 0,
955 "ext2", &profile_type);
956 ext_type = profile_type;
957 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
958 ext_type = "ext3";
959 }
960
961 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
962 if (fs_param->s_blocks_count < 3 * meg)
963 size_type = "floppy";
964 else if (fs_param->s_blocks_count < 512 * meg)
965 size_type = "small";
966 else
967 size_type = "default";
968
969 if (!usage_types)
970 usage_types = size_type;
971
972 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
973 if (!parse_str) {
974 free(list.list);
975 return 0;
976 }
977 if (usage_types)
978 strcpy(parse_str, usage_types);
979 else
980 *parse_str = '\0';
981
982 if (ext_type)
983 push_string(&list, ext_type);
984 cp = parse_str;
985 while (1) {
986 t = strchr(cp, ',');
987 if (t)
988 *t = '\0';
989
990 if (*cp)
991 push_string(&list, cp);
992 if (t)
993 cp = t+1;
994 else {
995 cp = "";
996 break;
997 }
998 }
999 free(parse_str);
1000 if (profile_type)
1001 free(profile_type);
1002 return (list.list);
1003}
1004
1005static char *get_string_from_profile(char **fs_types, const char *opt,
1006 const char *def_val)
1007{
1008 char *ret = 0;
1009 char **cpp;
1010 int i;
1011
1012 for (i=0; fs_types[i]; i++);
1013 for (i-=1; i >=0 ; i--) {
1014 profile_get_string(profile, "fs_types", fs_types[i],
1015 opt, 0, &ret);
1016 if (ret)
1017 return ret;
1018 }
1019 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1020 return (ret);
1021}
1022
1023static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1024{
1025 int ret;
1026 char **cpp;
1027
1028 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1029 for (cpp = fs_types; *cpp; cpp++)
1030 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1031 return ret;
1032}
1033
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001034static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1035{
1036 int ret;
1037 char **cpp;
1038
1039 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1040 for (cpp = fs_types; *cpp; cpp++)
1041 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1042 return ret;
1043}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001044
Theodore Ts'od48bc602007-07-04 14:10:46 -04001045extern const char *mke2fs_default_profile;
1046static const char *default_files[] = { "<default>", 0 };
1047
Theodore Ts'o3839e651997-04-26 13:21:57 +00001048static void PRS(int argc, char *argv[])
1049{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001050 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001051 int size;
Theodore Ts'o2d363582008-05-14 18:09:37 -04001052 char *tmp, *tmp2, **cpp;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001053 int blocksize = 0;
1054 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001055 int inode_size = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001056 unsigned long flex_bg_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -05001057 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001058 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001059 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001060 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001061 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001062 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001063 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -05001064 const char * fs_type = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001065 const char * usage_types = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001066 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -05001067#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001068 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001069#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001070 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001071 int s_opt = -1, r_opt = -1;
1072 char *fs_features = 0;
1073 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001074 char *newpath;
1075 int pathlen = sizeof(PATH_SET) + 1;
1076
1077 if (oldpath)
1078 pathlen += strlen(oldpath);
1079 newpath = malloc(pathlen);
1080 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001081
Theodore Ts'o3839e651997-04-26 13:21:57 +00001082 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001083 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001084 strcat (newpath, ":");
1085 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001086 }
1087 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001088
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001089 tmp = getenv("MKE2FS_SYNC");
1090 if (tmp)
1091 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001092
1093 /* Determine the system page size if possible */
1094#ifdef HAVE_SYSCONF
1095#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1096#define _SC_PAGESIZE _SC_PAGE_SIZE
1097#endif
1098#ifdef _SC_PAGESIZE
1099 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001100 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001101 sys_page_size = sysval;
1102#endif /* _SC_PAGESIZE */
1103#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001104
1105 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1106 config_fn[0] = tmp;
1107 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001108 retval = profile_init(config_fn, &profile);
1109 if (retval == ENOENT) {
1110 profile_init(default_files, &profile);
1111 profile_set_default(profile, mke2fs_default_profile);
1112 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001113
Theodore Ts'o3839e651997-04-26 13:21:57 +00001114 setbuf(stdout, NULL);
1115 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001116 add_error_table(&et_ext2_error_table);
1117 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001118 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1119 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001120
Theodore Ts'o756df352002-03-07 20:52:12 -05001121#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001122 if (uname(&ut)) {
1123 perror("uname");
1124 exit(1);
1125 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001126 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001127 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001128 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001129#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001130
1131 if (argc && *argv) {
1132 program_name = get_progname(*argv);
1133
1134 /* If called as mkfs.ext3, create a journal inode */
1135 if (!strcmp(program_name, "mkfs.ext3"))
1136 journal_size = -1;
1137 }
1138
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001139 while ((c = getopt (argc, argv,
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001140 "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 +00001141 switch (c) {
1142 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001143 blocksize = strtol(optarg, &tmp, 0);
1144 b = (blocksize > 0) ? blocksize : -blocksize;
1145 if (b < EXT2_MIN_BLOCK_SIZE ||
1146 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001147 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001148 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001149 exit(1);
1150 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001151 if (blocksize > 4096)
1152 fprintf(stderr, _("Warning: blocksize %d not "
1153 "usable on most systems.\n"),
1154 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001155 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001156 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001157 int_log2(blocksize >>
1158 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001159 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001160 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001161 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001162 break;
1163 case 'f':
1164 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001165 if (size < EXT2_MIN_BLOCK_SIZE ||
1166 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001167 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001168 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001169 optarg);
1170 exit(1);
1171 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001172 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001173 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001174 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001175 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001176 break;
1177 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001178 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001179 if (*tmp) {
1180 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001181 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001182 exit(1);
1183 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001184 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001185 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001186 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001187 exit(1);
1188 }
1189 break;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001190 case 'G':
1191 flex_bg_size = strtoul(optarg, &tmp, 0);
1192 if (*tmp) {
1193 com_err(program_name, 0,
1194 _("Illegal number for flex_bg size"));
1195 exit(1);
1196 }
1197 if (flex_bg_size < 2 ||
1198 (flex_bg_size & (flex_bg_size-1)) != 0) {
1199 com_err(program_name, 0,
1200 _("flex_bg size must be a power of 2"));
1201 exit(1);
1202 }
1203 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001204 case 'i':
1205 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001206 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1207 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001208 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001209 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001210 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001211 optarg, EXT2_MIN_BLOCK_SIZE,
1212 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001213 exit(1);
1214 }
1215 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001216 case 'J':
1217 parse_journal_opts(optarg);
1218 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001219 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001220 if (!journal_size)
1221 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001222 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001223 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001224 bad_blocks_filename = malloc(strlen(optarg)+1);
1225 if (!bad_blocks_filename) {
1226 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001227 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001228 exit(1);
1229 }
1230 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001231 break;
1232 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001233 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001234 if (reserved_ratio > 50 || *tmp) {
1235 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001236 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001237 optarg);
1238 exit(1);
1239 }
1240 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001241 case 'n':
1242 noaction++;
1243 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001244 case 'o':
1245 creator_os = optarg;
1246 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001247 case 'q':
1248 quiet = 1;
1249 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001250 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001251 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001252 if (*tmp) {
1253 com_err(program_name, 0,
1254 _("bad revision level - %s"), optarg);
1255 exit(1);
1256 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001257 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001258 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001259 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001260 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001261 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001262 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001263 inode_size = strtoul(optarg, &tmp, 0);
1264 if (*tmp) {
1265 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001266 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001267 exit(1);
1268 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001269 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001270 case 'v':
1271 verbose = 1;
1272 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001273 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001274 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001275 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001276 case 'L':
1277 volume_label = optarg;
1278 break;
1279 case 'M':
1280 mount_dir = optarg;
1281 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001282 case 'N':
1283 num_inodes = strtoul(optarg, &tmp, 0);
1284 if (*tmp) {
1285 com_err(program_name, 0,
1286 _("bad num inodes - %s"), optarg);
1287 exit(1);
1288 }
1289 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001290 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001291 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001292 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001293 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001294 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001295 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001296 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001297 case 'S':
1298 super_only = 1;
1299 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001300 case 't':
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001301 fs_type = optarg;
1302 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001303 case 'T':
1304 usage_types = optarg;
1305 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001306 case 'V':
1307 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001308 show_version_only++;
1309 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001310 default:
1311 usage();
1312 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001313 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001314 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001315 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001316 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001317
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001318 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001319 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1320 E2FSPROGS_DATE);
1321
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001322 if (show_version_only) {
1323 fprintf(stderr, _("\tUsing %s\n"),
1324 error_message(EXT2_ET_BASE));
1325 exit(0);
1326 }
1327
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001328 /*
1329 * If there's no blocksize specified and there is a journal
1330 * device, use it to figure out the blocksize
1331 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001332 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001333 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001334 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001335
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001336#ifdef CONFIG_TESTIO_DEBUG
1337 io_ptr = test_io_manager;
1338 test_io_backing_manager = unix_io_manager;
1339#else
1340 io_ptr = unix_io_manager;
1341#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001342 retval = ext2fs_open(journal_device,
1343 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001344 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001345 if (retval) {
1346 com_err(program_name, retval,
1347 _("while trying to open journal device %s\n"),
1348 journal_device);
1349 exit(1);
1350 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001351 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001352 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001353 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001354 "minimum blocksize %d\n"), jfs->blocksize,
1355 -blocksize);
1356 exit(1);
1357 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001358 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001359 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001360 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1361 ext2fs_close(jfs);
1362 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001363
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001364 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001365 if (!force) {
1366 com_err(program_name, 0,
1367 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001368 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001369 proceed_question();
1370 }
1371 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1372 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001373 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001374 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001375 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001376 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1377 fs_param.s_log_block_size);
1378 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001379 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001380 argv[optind - 1]);
1381 exit(1);
1382 }
1383 }
1384 if (optind < argc)
1385 usage();
1386
Theodore Ts'o74becf31997-04-26 14:37:06 +00001387 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001388 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001389 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001390
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001391 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001392
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001393 if (noaction && fs_param.s_blocks_count) {
1394 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001395 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001396 } else {
1397 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001398 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001399 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001400 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001401 if ((retval == EFBIG) &&
1402 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001403 (fs_param.s_log_block_size == 0)) {
1404 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001405 blocksize = 4096;
1406 goto retry;
1407 }
1408 }
1409
Theodore Ts'oa789d841998-03-30 01:20:55 +00001410 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1411 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001412 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001413 exit(1);
1414 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001415 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001416 if (retval == EXT2_ET_UNIMPLEMENTED) {
1417 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001418 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001419 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001420 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001421 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001422 } else {
1423 if (dev_size == 0) {
1424 com_err(program_name, 0,
1425 _("Device size reported to be zero. "
1426 "Invalid partition specified, or\n\t"
1427 "partition table wasn't reread "
1428 "after running fdisk, due to\n\t"
1429 "a modified partition being busy "
1430 "and in use. You may need to reboot\n\t"
1431 "to re-read your partition table.\n"
1432 ));
1433 exit(1);
1434 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001435 fs_param.s_blocks_count = dev_size;
1436 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1437 fs_param.s_blocks_count &= ~((sys_page_size /
1438 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001439 }
1440
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001441 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001442 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001443 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001444 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001445 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001446
Theodore Ts'o3d438362008-02-19 08:32:58 -05001447 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1448 if (!fs_types) {
1449 fprintf(stderr, _("Failed to parse fs types list\n"));
1450 exit(1);
1451 }
1452 if (verbose) {
1453 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1454 print_str_list(fs_types);
1455 }
1456
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001457 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001458 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001459 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1460
Brian Behlendorfe0661502007-03-19 08:25:38 -04001461 if (fs_param.s_feature_incompat &
1462 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1463 fs_type = "journal";
1464 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001465 fs_type = "floppy";
1466 else if (megs <= 512)
1467 fs_type = "small";
1468 else
1469 fs_type = "default";
1470 }
1471
1472 /* Figure out what features should be enabled */
1473
Theodore Ts'o3d438362008-02-19 08:32:58 -05001474 tmp = NULL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001475 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001476 tmp = get_string_from_profile(fs_types, "base_features",
1477 "sparse_super,filetype,resize_inode,dir_index");
1478 edit_feature(tmp, &fs_param.s_feature_compat);
1479 free(tmp);
1480
1481 for (cpp = fs_types; *cpp; cpp++) {
1482 tmp = NULL;
1483 profile_get_string(profile, "fs_types", *cpp,
1484 "features", "", &tmp);
1485 if (tmp && *tmp)
1486 edit_feature(tmp, &fs_param.s_feature_compat);
1487 if (tmp)
1488 free(tmp);
1489 }
1490 tmp = get_string_from_profile(fs_types, "default_features",
1491 "");
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001492 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001493 edit_feature(fs_features ? fs_features : tmp,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001494 &fs_param.s_feature_compat);
1495 if (tmp)
1496 free(tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001497
1498 if (r_opt == EXT2_GOOD_OLD_REV &&
1499 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1500 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001501 fprintf(stderr, _("Filesystem features not supported "
1502 "with revision 0 filesystems\n"));
1503 exit(1);
1504 }
1505
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001506 if (s_opt > 0) {
1507 if (r_opt == EXT2_GOOD_OLD_REV) {
1508 fprintf(stderr, _("Sparse superblocks not supported "
1509 "with revision 0 filesystems\n"));
1510 exit(1);
1511 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001512 fs_param.s_feature_ro_compat |=
1513 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001514 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001515 fs_param.s_feature_ro_compat &=
1516 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1517
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001518 if (journal_size != 0) {
1519 if (r_opt == EXT2_GOOD_OLD_REV) {
1520 fprintf(stderr, _("Journals not supported "
1521 "with revision 0 filesystems\n"));
1522 exit(1);
1523 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001524 fs_param.s_feature_compat |=
1525 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001526 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001527
1528 if (fs_param.s_feature_incompat &
1529 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001530 reserved_ratio = 0;
1531 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1532 fs_param.s_feature_compat = 0;
1533 fs_param.s_feature_ro_compat = 0;
1534 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001535
1536 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1537 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1538 fprintf(stderr, _("The resize_inode and meta_bg features "
1539 "are not compatible.\n"
1540 "They can not be both enabled "
1541 "simultaneously.\n"));
1542 exit(1);
1543 }
1544
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001545 /* Set first meta blockgroup via an environment variable */
1546 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001547 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001548 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001549 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001550
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001551 /* Get the hardware sector size, if available */
1552 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1553 if (retval) {
1554 com_err(program_name, retval,
1555 _("while trying to determine hardware sector size"));
1556 exit(1);
1557 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001558
Theodore Ts'o54434922003-12-07 01:28:50 -05001559 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001560 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001561
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001562 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001563 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001564
1565 if (use_bsize == -1) {
1566 use_bsize = sys_page_size;
1567 if ((linux_version_code < (2*65536 + 6*256)) &&
1568 (use_bsize > 4096))
1569 use_bsize = 4096;
1570 }
1571 if (sector_size && use_bsize < sector_size)
1572 use_bsize = sector_size;
1573 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1574 use_bsize = -blocksize;
1575 blocksize = use_bsize;
1576 fs_param.s_blocks_count /= blocksize / 1024;
1577 }
1578
1579 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001580 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1581 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001582 if (inode_ratio < blocksize)
1583 inode_ratio = blocksize;
1584 }
1585
1586 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1587 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1588
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001589 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001590
1591 lazy_itable_init = get_bool_from_profile(fs_types,
1592 "lazy_itable_init", 0);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001593
Theodore Ts'o2d363582008-05-14 18:09:37 -04001594 /* Get options from profile */
1595 for (cpp = fs_types; *cpp; cpp++) {
1596 tmp = NULL;
1597 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1598 if (tmp && *tmp)
1599 parse_extended_opts(&fs_param, tmp);
1600 if (tmp)
1601 free(tmp);
1602 }
1603
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001604 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001605 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001606
1607 /* Since sparse_super is the default, we would only have a problem
1608 * here if it was explicitly disabled.
1609 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001610 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1611 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001612 com_err(program_name, 0,
1613 _("reserved online resize blocks not supported "
1614 "on non-sparse filesystem"));
1615 exit(1);
1616 }
1617
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001618 if (fs_param.s_blocks_per_group) {
1619 if (fs_param.s_blocks_per_group < 256 ||
1620 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001621 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001622 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001623 exit(1);
1624 }
1625 }
1626
Theodore Ts'o3d438362008-02-19 08:32:58 -05001627 if (inode_size == 0)
1628 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001629 if (!flex_bg_size && (fs_param.s_feature_incompat &
1630 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1631 flex_bg_size = get_int_from_profile(fs_types,
1632 "flex_bg_size", 16);
1633 if (flex_bg_size) {
1634 if (!(fs_param.s_feature_incompat &
1635 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1636 com_err(program_name, 0,
1637 _("Flex_bg feature not enabled, so "
1638 "flex_bg size may not be specified"));
1639 exit(1);
1640 }
1641 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1642 }
Andreas Dilger067911a2006-07-15 22:08:20 -04001643
1644 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001645 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001646 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001647 inode_size & (inode_size - 1)) {
1648 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001649 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001650 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001651 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001652 exit(1);
1653 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001654 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001655 }
1656
Eric Sandeenf3358642006-09-12 14:56:17 -04001657 /* Make sure number of inodes specified will fit in 32 bits */
1658 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001659 unsigned long long n;
1660 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001661 if (n > ~0U) {
1662 com_err(program_name, 0,
1663 _("too many inodes (%llu), raise inode ratio?"), n);
1664 exit(1);
1665 }
1666 } else if (num_inodes > ~0U) {
1667 com_err(program_name, 0,
1668 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001669 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001670 exit(1);
1671 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001672 /*
1673 * Calculate number of inodes based on the inode ratio
1674 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001675 fs_param.s_inodes_count = num_inodes ? num_inodes :
1676 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001677 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001678
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001679 if ((((long long)fs_param.s_inodes_count) *
1680 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1681 (((long long)fs_param.s_blocks_count) *
1682 EXT2_BLOCK_SIZE(&fs_param))) {
1683 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1684 "(%u) too big for a\n\t"
1685 "filesystem with %lu blocks, "
1686 "specify higher inode_ratio (-i)\n\t"
1687 "or lower inode count (-N).\n"),
1688 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001689 fs_param.s_inodes_count,
1690 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001691 exit(1);
1692 }
1693
Theodore Ts'o3839e651997-04-26 13:21:57 +00001694 /*
1695 * Calculate number of blocks to reserve
1696 */
Theodore Ts'o9ff8ece2008-06-17 19:58:29 -04001697 fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1698 fs_param.s_blocks_count / 100.0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001699}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001700
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301701static int should_do_undo(const char *name)
1702{
1703 errcode_t retval;
1704 io_channel channel;
1705 __u16 s_magic;
1706 struct ext2_super_block super;
1707 io_manager manager = unix_io_manager;
1708 int csum_flag, force_undo;
1709
1710 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1711 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1712 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1713 if (!force_undo && (!csum_flag || !lazy_itable_init))
1714 return 0;
1715
1716 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1717 if (retval) {
1718 /*
1719 * We don't handle error cases instead we
1720 * declare that the file system doesn't exist
1721 * and let the rest of mke2fs take care of
1722 * error
1723 */
1724 retval = 0;
1725 goto open_err_out;
1726 }
1727
1728 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1729 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1730 if (retval) {
1731 retval = 0;
1732 goto err_out;
1733 }
1734
1735#if defined(WORDS_BIGENDIAN)
1736 s_magic = ext2fs_swab16(super.s_magic);
1737#else
1738 s_magic = super.s_magic;
1739#endif
1740
1741 if (s_magic == EXT2_SUPER_MAGIC)
1742 retval = 1;
1743
1744err_out:
1745 io_channel_close(channel);
1746
1747open_err_out:
1748
1749 return retval;
1750}
1751
1752static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1753{
1754 errcode_t retval = 0;
1755 char *tdb_dir, tdb_file[PATH_MAX];
1756 char *device_name, *tmp_name;
1757
1758 /*
1759 * Configuration via a conf file would be
1760 * nice
1761 */
1762 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1763 if (!tdb_dir)
1764 profile_get_string(profile, "defaults",
1765 "undo_dir", 0, "/var/lib/e2fsprogs",
1766 &tdb_dir);
1767
1768 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1769 access(tdb_dir, W_OK))
1770 return 0;
1771
1772 tmp_name = strdup(name);
1773 device_name = basename(tmp_name);
1774 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1775
1776 if (!access(tdb_file, F_OK)) {
1777 if (unlink(tdb_file) < 0) {
1778 retval = errno;
1779 com_err(program_name, retval,
1780 _("while trying to delete %s"),
1781 tdb_file);
1782 return retval;
1783 }
1784 }
1785
1786 set_undo_io_backing_manager(*io_ptr);
1787 *io_ptr = undo_io_manager;
1788 set_undo_io_backup_file(tdb_file);
1789 printf(_("Overwriting existing filesystem; this can be undone "
1790 "using the command:\n"
1791 " e2undo %s %s\n\n"), tdb_file, name);
1792err_out:
1793 free(tmp_name);
1794 return retval;
1795}
1796
Theodore Ts'o3839e651997-04-26 13:21:57 +00001797int main (int argc, char *argv[])
1798{
1799 errcode_t retval = 0;
1800 ext2_filsys fs;
1801 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001802 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001803 unsigned int i;
1804 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001805 io_manager io_ptr;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301806 char tdb_string[40];
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001807
1808#ifdef ENABLE_NLS
1809 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001810 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001811 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1812 textdomain(NLS_CAT_NAME);
1813#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001814 PRS(argc, argv);
1815
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001816#ifdef CONFIG_TESTIO_DEBUG
1817 io_ptr = test_io_manager;
1818 test_io_backing_manager = unix_io_manager;
1819#else
1820 io_ptr = unix_io_manager;
1821#endif
1822
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301823 if (should_do_undo(device_name)) {
1824 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1825 if (retval)
1826 exit(1);
1827 }
1828
Theodore Ts'o3839e651997-04-26 13:21:57 +00001829 /*
1830 * Initialize the superblock....
1831 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001832 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001833 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001834 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001835 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001836 exit(1);
1837 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301838 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1839 32768 : fs->blocksize * 8);
1840 io_channel_set_options(fs->io, tdb_string);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001841
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001842 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1843 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1844
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001845 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001846 * Wipe out the old on-disk superblock
1847 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001848 if (!noaction)
1849 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001850
1851 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001852 * Generate a UUID for it...
1853 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001854 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001855
1856 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001857 * Initialize the directory index variables
1858 */
1859 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1860 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1861
1862 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001863 * Add "jitter" to the superblock's check interval so that we
1864 * don't check all the filesystems at the same time. We use a
1865 * kludgy hack of using the UUID to derive a random jitter value.
1866 */
1867 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1868 val += fs->super->s_uuid[i];
1869 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1870
1871 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001872 * Override the creator OS, if applicable
1873 */
1874 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001875 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001876 exit(1);
1877 }
1878
1879 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001880 * For the Hurd, we will turn off filetype since it doesn't
1881 * support it.
1882 */
1883 if (fs->super->s_creator_os == EXT2_OS_HURD)
1884 fs->super->s_feature_incompat &=
1885 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1886
1887 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001888 * Set the volume label...
1889 */
1890 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001891 memset(fs->super->s_volume_name, 0,
1892 sizeof(fs->super->s_volume_name));
1893 strncpy(fs->super->s_volume_name, volume_label,
1894 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001895 }
1896
1897 /*
1898 * Set the last mount directory
1899 */
1900 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001901 memset(fs->super->s_last_mounted, 0,
1902 sizeof(fs->super->s_last_mounted));
1903 strncpy(fs->super->s_last_mounted, mount_dir,
1904 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001905 }
1906
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001907 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001908 show_stats(fs);
1909
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001910 if (noaction)
1911 exit(0);
1912
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001913 if (fs->super->s_feature_incompat &
1914 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1915 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001916 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001917 }
1918
Theodore Ts'o3839e651997-04-26 13:21:57 +00001919 if (bad_blocks_filename)
1920 read_bb_file(fs, &bb_list, bad_blocks_filename);
1921 if (cflag)
1922 test_disk(fs, &bb_list);
1923
1924 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001925 fs->stride = fs_stride = fs->super->s_raid_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001926 retval = ext2fs_allocate_tables(fs);
1927 if (retval) {
1928 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001929 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001930 exit(1);
1931 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001932 if (super_only) {
1933 fs->super->s_state |= EXT2_ERROR_FS;
1934 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1935 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001936 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001937 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001938 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001939 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001940 blk_t ret_blk;
1941
1942#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001943 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001944#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001945
1946 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001947 * Wipe out any old MD RAID (or other) metadata at the end
1948 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001949 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001950 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001951 start = (blocks & ~(rsv - 1));
1952 if (start > rsv)
1953 start -= rsv;
1954 if (start > 0)
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301955 retval = ext2fs_zero_blocks(fs, start, blocks - start,
1956 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06001957
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001958 if (retval) {
1959 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001960 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001961 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001962 }
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001963 write_inode_tables(fs, lazy_itable_init);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001964 create_root_dir(fs);
1965 create_lost_and_found(fs);
1966 reserve_inodes(fs);
1967 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001968 if (fs->super->s_feature_compat &
1969 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1970 retval = ext2fs_create_resize_inode(fs);
1971 if (retval) {
1972 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001973 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001974 exit(1);
1975 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001976 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001977 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001978
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001979 if (journal_device) {
1980 ext2_filsys jfs;
1981
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001982 if (!force)
1983 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001984 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001985
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001986 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1987 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1988 fs->blocksize, unix_io_manager, &jfs);
1989 if (retval) {
1990 com_err(program_name, retval,
1991 _("while trying to open journal device %s\n"),
1992 journal_device);
1993 exit(1);
1994 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001995 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001996 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001997 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001998 fflush(stdout);
1999 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002000 retval = ext2fs_add_journal_device(fs, jfs);
2001 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002002 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00002003 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002004 journal_device);
2005 exit(1);
2006 }
2007 if (!quiet)
2008 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002009 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06002010 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002011 } else if ((journal_size) ||
2012 (fs_param.s_feature_compat &
2013 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002014 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002015
2016 if (!journal_blocks) {
2017 fs->super->s_feature_compat &=
2018 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2019 goto no_journal;
2020 }
2021 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002022 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00002023 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002024 fflush(stdout);
2025 }
Theodore Ts'o63985322001-01-03 17:02:13 +00002026 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2027 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002028 if (retval) {
2029 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00002030 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002031 exit(1);
2032 }
2033 if (!quiet)
2034 printf(_("done\n"));
2035 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002036no_journal:
2037
Theodore Ts'o3839e651997-04-26 13:21:57 +00002038 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002039 printf(_("Writing superblocks and "
2040 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002041 retval = ext2fs_flush(fs);
2042 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05002043 fprintf(stderr,
2044 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002045 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002046 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002047 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002048 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2049 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002050 }
Andreas Dilger568101f2001-10-13 01:22:25 -06002051 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002052 remove_error_table(&et_ext2_error_table);
2053 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002054 profile_release(profile);
Andreas Dilger568101f2001-10-13 01:22:25 -06002055 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002056}