blob: dd77a00a4fba8411bff4f9cf9b8a9120b66b937c [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -040014 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000015 * The device may be a block device or a image of one, but this isn't
Theodore Ts'oefc6f622008-08-27 23:07:54 -040016 * enforced (but it's not much fun on a character device :-).
Theodore Ts'o3839e651997-04-26 13:21:57 +000017 */
18
Theodore Ts'oebabf2a2008-07-13 15:32:37 -040019#define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
20
Theodore Ts'od1154eb2011-09-18 17:34:37 -040021#include "config.h"
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000022#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <string.h>
Theodore Ts'od0c53772009-07-18 18:41:51 -040024#include <strings.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <fcntl.h>
26#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050028#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000029#include <sys/utsname.h>
30#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000033#else
34extern char *optarg;
35extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000036#endif
37#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000038#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000039#endif
40#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000041#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000042#endif
43#ifdef HAVE_ERRNO_H
44#include <errno.h>
45#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000046#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000047#include <sys/types.h>
Eric Sandeen13b0b122010-01-23 21:50:45 -060048#include <sys/stat.h>
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053049#include <libgen.h>
Theodore Ts'o36585792008-06-07 22:07:50 -040050#include <limits.h>
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -050051#include <blkid/blkid.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000052
Theodore Ts'o54c637d2001-05-14 11:45:38 +000053#include "ext2fs/ext2_fs.h"
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -040054#include "ext2fs/ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000056#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000057#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000059#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050060#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050061#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000062#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000063#include "nls-enable.h"
Aditya Kali1f5d7a82011-07-20 11:40:04 -070064#include "quota/mkquota.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000065
66#define STRIDE_LENGTH 8
67
Theodore Ts'o493024e2010-06-13 14:00:00 -040068#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
69
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000070#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000071#define ZAP_BOOTBLOCK
72#endif
73
Lukas Czerner7d9e3162011-01-24 20:52:00 +010074#define DISCARD_STEP_MB (2048)
75
Theodore Ts'o3839e651997-04-26 13:21:57 +000076extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000077extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000078
Theodore Ts'of4041672013-12-16 18:56:36 -050079static const char * program_name = "mke2fs";
80static const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
82/* Command line options */
Theodore Ts'of4041672013-12-16 18:56:36 -050083static int cflag;
84static int verbose;
85static int quiet;
86static int super_only;
87static int discard = 1; /* attempt to discard device before fs creation */
88static int direct_io;
89static int force;
90static int noaction;
91static uid_t root_uid;
92static gid_t root_gid;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000093int journal_size;
94int journal_flags;
Theodore Ts'of4041672013-12-16 18:56:36 -050095static int lazy_itable_init;
96static char *bad_blocks_filename = NULL;
97static __u32 fs_stride;
98static int quotatype = -1; /* Initialize both user and group quotas by default */
Theodore Ts'o88ee0232013-12-30 23:03:09 -050099static __u64 offset;
Theodore Ts'ob8182052014-01-28 12:58:56 -0500100static blk64_t journal_location = ~0LL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101
Theodore Ts'of4041672013-12-16 18:56:36 -0500102static struct ext2_super_block fs_param;
103static char *fs_uuid = NULL;
104static char *creator_os;
105static char *volume_label;
106static char *mount_dir;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000107char *journal_device;
Theodore Ts'of4041672013-12-16 18:56:36 -0500108static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
109static char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110
Theodore Ts'of4041672013-12-16 18:56:36 -0500111static profile_t profile;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500112
Theodore Ts'of4041672013-12-16 18:56:36 -0500113static int sys_page_size = 4096;
114static int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400115
Theodore Ts'o63985322001-01-03 17:02:13 +0000116static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500118 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Eric Sandeen28e2cb92011-10-04 17:12:11 -0500119 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500120 "[-J journal-options]\n"
Yongqiang Yang8dbcbe12012-01-23 11:46:43 -0500121 "\t[-G flex-group-size] [-N number-of-inodes]\n"
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400122 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
123 "\t[-g blocks-per-group] [-L volume-label] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400124 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500125 "[-r fs-revision] [-E extended-option[,...]]\n"
Mike Frysinger65794cf2012-01-09 21:17:57 -0500126 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
Theodore Ts'o37c8db72012-03-22 16:00:49 -0400127 "[-jnqvDFKSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 program_name);
129 exit(1);
130}
131
Jose R. Santos02d6f472010-06-13 13:00:00 -0400132static int int_log2(unsigned long long arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133{
134 int l = 0;
135
136 arg >>= 1;
137 while (arg) {
138 l++;
139 arg >>= 1;
140 }
141 return l;
142}
143
Jose R. Santos02d6f472010-06-13 13:00:00 -0400144static int int_log10(unsigned long long arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000145{
146 int l;
147
148 for (l=0; arg ; l++)
149 arg = arg / 10;
150 return l;
151}
152
Andreas Dilger1d6fd6d2012-11-29 05:47:52 -0700153#ifdef __linux__
Theodore Ts'od99225e2004-09-25 07:40:12 -0400154static int parse_version_number(const char *s)
155{
156 int major, minor, rev;
157 char *endptr;
158 const char *cp = s;
159
160 if (!s)
161 return 0;
162 major = strtol(cp, &endptr, 10);
163 if (cp == endptr || *endptr != '.')
164 return 0;
165 cp = endptr + 1;
166 minor = strtol(cp, &endptr, 10);
167 if (cp == endptr || *endptr != '.')
168 return 0;
169 cp = endptr + 1;
170 rev = strtol(cp, &endptr, 10);
171 if (cp == endptr)
172 return 0;
173 return ((((major * 256) + minor) * 256) + rev);
174}
Andreas Dilger1d6fd6d2012-11-29 05:47:52 -0700175#endif
Theodore Ts'od99225e2004-09-25 07:40:12 -0400176
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000177/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 * Helper function for read_bb_file and test_disk
179 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500180static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181{
Theodore Ts'o66938372002-03-08 00:14:46 -0500182 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 return;
184}
185
186/*
187 * Reads the bad blocks list from a file
188 */
189static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
190 const char *bad_blocks_file)
191{
192 FILE *f;
193 errcode_t retval;
194
195 f = fopen(bad_blocks_file, "r");
196 if (!f) {
197 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000198 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 exit(1);
200 }
201 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
202 fclose (f);
203 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500204 com_err("ext2fs_read_bb_FILE", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000205 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 exit(1);
207 }
208}
209
210/*
211 * Runs the badblocks program to test the disk
212 */
213static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
214{
215 FILE *f;
216 errcode_t retval;
217 char buf[1024];
218
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400219 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500220 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400221 fs->device_name, ext2fs_blocks_count(fs->super)-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000223 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224 f = popen(buf, "r");
225 if (!f) {
226 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400227 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 exit(1);
229 }
230 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000231 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500233 com_err("ext2fs_read_bb_FILE", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000234 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000235 exit(1);
236 }
237}
238
239static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
240{
Theodore Ts'o54434922003-12-07 01:28:50 -0500241 dgrp_t i;
242 blk_t j;
243 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 blk_t blk;
245 badblocks_iterate bb_iter;
246 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000247 blk_t group_block;
248 int group;
249 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250
251 if (!bb_list)
252 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400253
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 /*
255 * The primary superblock and group descriptors *must* be
256 * good; if not, abort.
257 */
258 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
259 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000260 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000261 fprintf(stderr, _("Block %d in primary "
262 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400263 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000264 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500266 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267 exit(1);
268 }
269 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000270
271 /*
272 * See if any of the bad blocks are showing up in the backup
273 * superblocks and/or group descriptors. If so, issue a
274 * warning and adjust the block counts appropriately.
275 */
276 group_block = fs->super->s_first_data_block +
277 fs->super->s_blocks_per_group;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400278
Theodore Ts'of3db3561997-04-26 13:34:30 +0000279 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000280 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000282 if (ext2fs_badblocks_list_test(bb_list,
283 group_block + j)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400284 if (!group_bad)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000285 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500286_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000287" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 group_block);
289 group_bad++;
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400290 group = ext2fs_group_of_blk2(fs, group_block+j);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400291 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400292 ext2fs_group_desc_csum_set(fs, group);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400293 ext2fs_free_blocks_count_add(fs->super, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000294 }
295 }
296 group_block += fs->super->s_blocks_per_group;
297 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400298
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299 /*
300 * Mark all the bad blocks as used...
301 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000302 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000303 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500304 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000305 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 exit(1);
307 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400308 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -0400309 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000310 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000311}
312
Eric Sandeen6fcd6f82010-08-20 16:41:14 -0500313static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000314{
315 errcode_t retval;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400316 blk64_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500317 dgrp_t i;
Theodore Ts'oe1632422010-12-20 10:42:57 -0500318 int num;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400319 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000320
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400321 ext2fs_numeric_progress_init(fs, &progress,
322 _("Writing inode tables: "),
323 fs->group_desc_count);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000324
Theodore Ts'o3839e651997-04-26 13:21:57 +0000325 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'oc498cb12012-09-22 21:26:48 -0400326 ext2fs_numeric_progress_update(fs, &progress, i);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400327
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400328 blk = ext2fs_inode_table_loc(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000329 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000330
Theodore Ts'oe1632422010-12-20 10:42:57 -0500331 if (lazy_flag)
332 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
333 ext2fs_bg_itable_unused(fs, i)) *
334 EXT2_INODE_SIZE(fs->super),
335 EXT2_BLOCK_SIZE(fs->super));
Eric Sandeen6fcd6f82010-08-20 16:41:14 -0500336 if (!lazy_flag || itable_zeroed) {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500337 /* The kernel doesn't need to zero the itable blocks */
Eric Sandeene633b582009-10-25 21:41:32 -0400338 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400339 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000340 }
Jose R. Santos02d6f472010-06-13 13:00:00 -0400341 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400342 if (retval) {
343 fprintf(stderr, _("\nCould not write %d "
Jose R. Santos02d6f472010-06-13 13:00:00 -0400344 "blocks in inode table starting at %llu: %s\n"),
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400345 num, blk, error_message(retval));
346 exit(1);
347 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000348 if (sync_kludge) {
349 if (sync_kludge == 1)
350 sync();
351 else if ((i % sync_kludge) == 0)
352 sync();
353 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000354 }
Jose R. Santos02d6f472010-06-13 13:00:00 -0400355 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400356 ext2fs_numeric_progress_close(fs, &progress,
357 _("done \n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000358}
359
360static void create_root_dir(ext2_filsys fs)
361{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400362 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000363 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364
365 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
366 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500367 com_err("ext2fs_mkdir", retval, "%s",
368 _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 exit(1);
370 }
Andreas Dilgerdc9cc702013-06-15 21:45:37 -0400371 if (root_uid != 0 || root_gid != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000372 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
373 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500374 com_err("ext2fs_read_inode", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000375 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000376 exit(1);
377 }
Andreas Dilgerdc9cc702013-06-15 21:45:37 -0400378
379 inode.i_uid = root_uid;
380 ext2fs_set_i_uid_high(inode, root_uid >> 16);
381 inode.i_gid = root_gid;
382 ext2fs_set_i_gid_high(inode, root_gid >> 16);
383
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500384 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000385 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500386 com_err("ext2fs_write_inode", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000387 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000388 exit(1);
389 }
390 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391}
392
393static void create_lost_and_found(ext2_filsys fs)
394{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400395 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000396 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000397 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000398 const char *name = "lost+found";
399 int i;
400
Theodore Ts'o6a525062001-12-24 09:40:00 -0500401 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
403 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500404 com_err("ext2fs_mkdir", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000405 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 exit(1);
407 }
408
409 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
410 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500411 com_err("ext2_lookup", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000412 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 exit(1);
414 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400415
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100417 /* Ensure that lost+found is at least 2 blocks, so we always
418 * test large empty blocks for big-block filesystems. */
419 if ((lpf_size += fs->blocksize) >= 16*1024 &&
420 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000421 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 retval = ext2fs_expand_dir(fs, ino);
423 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500424 com_err("ext2fs_expand_dir", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000425 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426 exit(1);
427 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500428 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429}
430
431static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
432{
433 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400434
Valerie Aurora Henson463e7322009-08-05 00:17:56 -0400435 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400436 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 retval = ext2fs_update_bb_inode(fs, bb_list);
438 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500439 com_err("ext2fs_update_bb_inode", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000440 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 exit(1);
442 }
443
444}
445
446static void reserve_inodes(ext2_filsys fs)
447{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000448 ext2_ino_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400450 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
451 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 ext2fs_mark_ib_dirty(fs);
453}
454
Theodore Ts'o756df352002-03-07 20:52:12 -0500455#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500456#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500457#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500458
Theodore Ts'o04a96852001-08-30 21:55:26 -0400459static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000460{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400461 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500463 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000464
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400465 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600466 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500467 printf(_("Out of memory erasing sectors %d-%d\n"),
468 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600469 exit(1);
470 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500471
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500472 if (sect == 0) {
473 /* Check for a BSD disklabel, and don't erase it if so */
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400474 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500475 if (retval)
476 fprintf(stderr,
477 _("Warning: could not read block 0: %s\n"),
478 error_message(retval));
479 else {
480 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
481 if ((*magic == BSD_DISKMAGIC) ||
482 (*magic == BSD_MAGICDISK))
483 return;
484 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500485 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500486
Theodore Ts'o70988102002-07-14 08:00:00 -0400487 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000488 io_channel_set_blksize(fs->io, 512);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400489 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000490 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400491 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000492 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500493 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
494 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000495}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000496
497static void create_journal_dev(ext2_filsys fs)
498{
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400499 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000500 errcode_t retval;
501 char *buf;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400502 blk64_t blk, err_blk;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530503 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000504
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000505 retval = ext2fs_create_journal_superblock(fs,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400506 ext2fs_blocks_count(fs->super), 0, &buf);
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000507 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500508 com_err("create_journal_dev", retval, "%s",
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000509 _("while initializing journal superblock"));
510 exit(1);
511 }
Andreas Dilger6c546892011-06-11 12:17:29 -0400512
513 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
514 goto write_superblock;
515
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400516 ext2fs_numeric_progress_init(fs, &progress,
517 _("Zeroing journal device: "),
518 ext2fs_blocks_count(fs->super));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530519 blk = 0;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400520 count = ext2fs_blocks_count(fs->super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530521 while (count > 0) {
522 if (count > 1024)
523 c = 1024;
524 else
525 c = count;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400526 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530527 if (retval) {
528 com_err("create_journal_dev", retval,
529 _("while zeroing journal device "
Jose R. Santos02d6f472010-06-13 13:00:00 -0400530 "(block %llu, count %d)"),
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530531 err_blk, err_count);
532 exit(1);
533 }
534 blk += c;
535 count -= c;
Theodore Ts'oc498cb12012-09-22 21:26:48 -0400536 ext2fs_numeric_progress_update(fs, &progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000537 }
Jose R. Santos02d6f472010-06-13 13:00:00 -0400538 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000539
Andreas Dilger6c546892011-06-11 12:17:29 -0400540 ext2fs_numeric_progress_close(fs, &progress, NULL);
541write_superblock:
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400542 retval = io_channel_write_blk64(fs->io,
543 fs->super->s_first_data_block+1,
544 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000545 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500546 com_err("create_journal_dev", retval, "%s",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000547 _("while writing journal superblock"));
548 exit(1);
549 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000550}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000551
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552static void show_stats(ext2_filsys fs)
553{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000554 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000555 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500556 char *os;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400557 blk64_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500558 dgrp_t i;
559 int need, col_left;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400560
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400561 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
562 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
563 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000564
565 memset(buf, 0, sizeof(buf));
566 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000567 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o74e12112010-12-21 18:12:12 -0500568 os = e2p_os2string(fs->super->s_creator_os);
569 if (os)
570 printf(_("OS type: %s\n"), os);
Theodore Ts'o63253942005-03-19 01:13:22 -0500571 free(os);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000572 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000573 s->s_log_block_size);
Theodore Ts'o412376e2011-02-25 21:43:54 -0500574 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
575 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
576 printf(_("Cluster size=%u (log=%u)\n"),
Theodore Ts'o1da5ef72011-06-04 10:20:47 -0400577 fs->blocksize << fs->cluster_ratio_bits,
578 s->s_log_cluster_size);
Theodore Ts'o412376e2011-02-25 21:43:54 -0500579 else
Theodore Ts'o1da5ef72011-06-04 10:20:47 -0400580 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
Theodore Ts'o412376e2011-02-25 21:43:54 -0500581 s->s_log_cluster_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -0500582 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
583 s->s_raid_stride, s->s_raid_stripe_width);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400584 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
585 ext2fs_blocks_count(s));
586 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
587 ext2fs_r_blocks_count(s),
588 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000589 printf(_("First data block=%u\n"), s->s_first_data_block);
Andreas Dilgerdc9cc702013-06-15 21:45:37 -0400590 if (root_uid != 0 || root_gid != 0)
591 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500592 if (s->s_reserved_gdt_blocks)
593 printf(_("Maximum filesystem blocks=%lu\n"),
594 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200595 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000596 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000597 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000598 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000599 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'o412376e2011-02-25 21:43:54 -0500600 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
601 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
602 printf(_("%u blocks per group, %u clusters per group\n"),
603 s->s_blocks_per_group, s->s_clusters_per_group);
604 else
605 printf(_("%u blocks per group, %u fragments per group\n"),
606 s->s_blocks_per_group, s->s_clusters_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000607 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608
609 if (fs->group_desc_count == 1) {
610 printf("\n");
611 return;
612 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500613
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500614 printf("%s", _("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 group_block = s->s_first_data_block;
616 col_left = 0;
617 for (i = 1; i < fs->group_desc_count; i++) {
618 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000619 if (!ext2fs_bg_has_super(fs, i))
620 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000621 if (i != 1)
622 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000623 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000624 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000626 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000628 col_left -= need;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400629 printf("%llu", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630 }
631 printf("\n\n");
632}
633
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000634/*
635 * Set the S_CREATOR_OS field. Return true if OS is known,
636 * otherwise, 0.
637 */
638static int set_os(struct ext2_super_block *sb, char *os)
639{
640 if (isdigit (*os))
641 sb->s_creator_os = atoi (os);
642 else if (strcasecmp(os, "linux") == 0)
643 sb->s_creator_os = EXT2_OS_LINUX;
644 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
645 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500646 else if (strcasecmp(os, "freebsd") == 0)
647 sb->s_creator_os = EXT2_OS_FREEBSD;
648 else if (strcasecmp(os, "lites") == 0)
649 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000650 else
651 return 0;
652 return 1;
653}
654
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000655#define PATH_SET "PATH=/sbin"
656
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400657static void parse_extended_opts(struct ext2_super_block *param,
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500658 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000659{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400660 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000661 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500662 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000663
664 len = strlen(opts);
665 buf = malloc(len+1);
666 if (!buf) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500667 fprintf(stderr, "%s",
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500668 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000669 exit(1);
670 }
671 strcpy(buf, opts);
672 for (token = buf; token && *token; token = next) {
673 p = strchr(token, ',');
674 next = 0;
675 if (p) {
676 *p = 0;
677 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500678 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000679 arg = strchr(token, '=');
680 if (arg) {
681 *arg = 0;
682 arg++;
683 }
Andreas Dilger2bc30412013-12-23 16:04:46 -0500684 if (strcmp(token, "desc-size") == 0 ||
685 strcmp(token, "desc_size") == 0) {
686 int desc_size;
687
688 if (!(fs_param.s_feature_incompat &
689 EXT4_FEATURE_INCOMPAT_64BIT)) {
690 fprintf(stderr,
691 _("%s requires '-O 64bit'\n"), token);
692 r_usage++;
693 continue;
694 }
695 if (param->s_reserved_gdt_blocks != 0) {
696 fprintf(stderr,
697 _("'%s' must be before 'resize=%u'\n"),
698 token, param->s_reserved_gdt_blocks);
699 r_usage++;
700 continue;
701 }
702 if (!arg) {
703 r_usage++;
704 badopt = token;
705 continue;
706 }
707 desc_size = strtoul(arg, &p, 0);
708 if (*p || (desc_size & (desc_size - 1))) {
709 fprintf(stderr,
710 _("Invalid desc_size: '%s'\n"), arg);
711 r_usage++;
712 continue;
713 }
714 param->s_desc_size = desc_size;
Theodore Ts'o88ee0232013-12-30 23:03:09 -0500715 } else if (strcmp(token, "offset") == 0) {
716 if (!arg) {
717 r_usage++;
718 badopt = token;
719 continue;
720 }
721 offset = strtoull(arg, &p, 0);
722 if (*p) {
723 fprintf(stderr, _("Invalid offset: %s\n"),
724 arg);
725 r_usage++;
726 continue;
727 }
Andreas Dilger2bc30412013-12-23 16:04:46 -0500728 } else if (strcmp(token, "mmp_update_interval") == 0) {
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400729 if (!arg) {
730 r_usage++;
731 badopt = token;
732 continue;
733 }
734 param->s_mmp_update_interval = strtoul(arg, &p, 0);
735 if (*p) {
736 fprintf(stderr,
737 _("Invalid mmp_update_interval: %s\n"),
738 arg);
739 r_usage++;
740 continue;
741 }
742 } else if (strcmp(token, "stride") == 0) {
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000743 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500744 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500745 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000746 continue;
747 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500748 param->s_raid_stride = strtoul(arg, &p, 0);
Theodore Ts'o5b734a02011-07-04 20:22:19 -0400749 if (*p) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000750 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400751 _("Invalid stride parameter: %s\n"),
752 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500753 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000754 continue;
755 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500756 } else if (strcmp(token, "stripe-width") == 0 ||
757 strcmp(token, "stripe_width") == 0) {
758 if (!arg) {
759 r_usage++;
760 badopt = token;
761 continue;
762 }
763 param->s_raid_stripe_width = strtoul(arg, &p, 0);
Theodore Ts'o5b734a02011-07-04 20:22:19 -0400764 if (*p) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500765 fprintf(stderr,
766 _("Invalid stripe-width parameter: %s\n"),
767 arg);
768 r_usage++;
769 continue;
770 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500771 } else if (!strcmp(token, "resize")) {
Jose R. Santos02d6f472010-06-13 13:00:00 -0400772 blk64_t resize;
773 unsigned long bpg, rsv_groups;
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500774 unsigned long group_desc_count, desc_blocks;
775 unsigned int gdpb, blocksize;
776 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500777
778 if (!arg) {
779 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500780 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500781 continue;
782 }
783
Jose R. Santos02d6f472010-06-13 13:00:00 -0400784 resize = parse_num_blocks2(arg,
785 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500786
787 if (resize == 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400788 fprintf(stderr,
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500789 _("Invalid resize parameter: %s\n"),
790 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500791 r_usage++;
792 continue;
793 }
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400794 if (resize <= ext2fs_blocks_count(param)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500795 fprintf(stderr, "%s",
Theodore Ts'of37ab682005-05-05 23:15:55 -0400796 _("The resize maximum must be greater "
797 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500798 r_usage++;
799 continue;
800 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500801
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500802 blocksize = EXT2_BLOCK_SIZE(param);
803 bpg = param->s_blocks_per_group;
804 if (!bpg)
805 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200806 gdpb = EXT2_DESC_PER_BLOCK(param);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400807 group_desc_count = (__u32) ext2fs_div64_ceil(
808 ext2fs_blocks_count(param), bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500809 desc_blocks = (group_desc_count +
810 gdpb - 1) / gdpb;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400811 rsv_groups = ext2fs_div64_ceil(resize, bpg);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400812 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500813 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500814 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500815 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
816
817 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400818 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500819 fprintf(stderr, "%s",
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400820 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400821 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400822 exit(1);
823 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500824 param->s_feature_compat |=
825 EXT2_FEATURE_COMPAT_RESIZE_INODE;
826
827 param->s_reserved_gdt_blocks = rsv_gdb;
828 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500829 } else if (!strcmp(token, "test_fs")) {
830 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400831 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400832 if (arg)
833 lazy_itable_init = strtoul(arg, &p, 0);
834 else
835 lazy_itable_init = 1;
Andreas Dilger6c546892011-06-11 12:17:29 -0400836 } else if (!strcmp(token, "lazy_journal_init")) {
837 if (arg)
838 journal_flags |= strtoul(arg, &p, 0) ?
839 EXT2_MKJOURNAL_LAZYINIT : 0;
840 else
841 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
Andreas Dilgerdc9cc702013-06-15 21:45:37 -0400842 } else if (!strcmp(token, "root_owner")) {
843 if (arg) {
844 root_uid = strtoul(arg, &p, 0);
845 if (*p != ':') {
846 fprintf(stderr,
847 _("Invalid root_owner: '%s'\n"),
848 arg);
849 r_usage++;
850 continue;
851 }
852 p++;
853 root_gid = strtoul(p, &p, 0);
854 if (*p) {
855 fprintf(stderr,
856 _("Invalid root_owner: '%s'\n"),
857 arg);
858 r_usage++;
859 continue;
860 }
861 } else {
862 root_uid = getuid();
863 root_gid = getgid();
864 }
Lukas Czerner0bc85df2010-11-18 14:38:39 +0100865 } else if (!strcmp(token, "discard")) {
866 discard = 1;
867 } else if (!strcmp(token, "nodiscard")) {
868 discard = 0;
Aditya Kalid678fef2011-11-14 10:55:54 -0500869 } else if (!strcmp(token, "quotatype")) {
870 if (!arg) {
871 r_usage++;
872 badopt = token;
873 continue;
874 }
875 if (!strncmp(arg, "usr", 3)) {
876 quotatype = 0;
877 } else if (!strncmp(arg, "grp", 3)) {
878 quotatype = 1;
879 } else {
880 fprintf(stderr,
881 _("Invalid quotatype parameter: %s\n"),
882 arg);
883 r_usage++;
884 continue;
885 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500886 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500887 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500888 badopt = token;
889 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000890 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500891 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500892 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400893 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000894 "and may take an argument which\n"
895 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400896 "Valid extended options are:\n"
Theodore Ts'o88ee0232013-12-30 23:03:09 -0500897 "\tmmp_update_interval=<interval>\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500898 "\tstride=<RAID per-disk data chunk in blocks>\n"
899 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'o88ee0232013-12-30 23:03:09 -0500900 "\toffset=<offset to create the file system>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400901 "\tresize=<resize maximum size in blocks>\n"
902 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
Andreas Dilger6c546892011-06-11 12:17:29 -0400903 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
Andreas Dilgerdc9cc702013-06-15 21:45:37 -0400904 "\troot_uid=<uid of root directory>\n"
905 "\troot_gid=<gid of root directory>\n"
Lukas Czerner0bc85df2010-11-18 14:38:39 +0100906 "\ttest_fs\n"
907 "\tdiscard\n"
Aditya Kalid678fef2011-11-14 10:55:54 -0500908 "\tnodiscard\n"
909 "\tquotatype=<usr OR grp>\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400910 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400911 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000912 exit(1);
913 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500914 if (param->s_raid_stride &&
915 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
916 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
917 "multiple of stride %u.\n\n"),
918 param->s_raid_stripe_width, param->s_raid_stride);
919
Brian Behlendorf21400382007-05-31 11:30:47 -0400920 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400921}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000922
Theodore Ts'o896938d1999-10-23 01:04:50 +0000923static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500924 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400925 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500926 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400927 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500928 EXT2_FEATURE_COMPAT_EXT_ATTR,
929 /* Incompat */
930 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500931 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400932 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500933 EXT2_FEATURE_INCOMPAT_META_BG|
Jose R. Santos02d6f472010-06-13 13:00:00 -0400934 EXT4_FEATURE_INCOMPAT_FLEX_BG|
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400935 EXT4_FEATURE_INCOMPAT_MMP |
Jose R. Santos02d6f472010-06-13 13:00:00 -0400936 EXT4_FEATURE_INCOMPAT_64BIT,
Theodore Ts'o558df542008-02-27 15:01:19 -0500937 /* R/O compat */
938 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400939 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
940 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
941 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500942 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -0400943 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
Aditya Kali1f5d7a82011-07-20 11:40:04 -0700944 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
Theodore Ts'o7becb202011-11-14 10:40:43 -0500945#ifdef CONFIG_QUOTA
946 EXT4_FEATURE_RO_COMPAT_QUOTA|
947#endif
948 0
Theodore Ts'o896938d1999-10-23 01:04:50 +0000949};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000950
951
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500952static void syntax_err_report(const char *filename, long err, int line_num)
953{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400954 fprintf(stderr,
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500955 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
956 filename, line_num, error_message(err));
957 exit(1);
958}
959
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200960static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500961
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400962static void edit_feature(const char *str, __u32 *compat_array)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500963{
964 if (!str)
965 return;
966
967 if (e2p_edit_feature(str, compat_array, ok_features)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400968 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500969 str);
970 exit(1);
971 }
972}
973
Eric Sandeen6a426c92011-02-17 15:56:17 -0600974static void edit_mntopts(const char *str, __u32 *mntopts)
975{
976 if (!str)
977 return;
978
979 if (e2p_edit_mntopts(str, mntopts, ~0)) {
980 fprintf(stderr, _("Invalid mount option set: %s\n"),
981 str);
982 exit(1);
983 }
984}
985
Theodore Ts'o3d438362008-02-19 08:32:58 -0500986struct str_list {
987 char **list;
988 int num;
989 int max;
990};
991
992static errcode_t init_list(struct str_list *sl)
993{
994 sl->num = 0;
995 sl->max = 0;
996 sl->list = malloc((sl->max+1) * sizeof(char *));
997 if (!sl->list)
998 return ENOMEM;
999 sl->list[0] = 0;
1000 return 0;
1001}
1002
1003static errcode_t push_string(struct str_list *sl, const char *str)
1004{
1005 char **new_list;
1006
1007 if (sl->num >= sl->max) {
1008 sl->max += 2;
1009 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1010 if (!new_list)
1011 return ENOMEM;
1012 sl->list = new_list;
1013 }
1014 sl->list[sl->num] = malloc(strlen(str)+1);
1015 if (sl->list[sl->num] == 0)
1016 return ENOMEM;
1017 strcpy(sl->list[sl->num], str);
1018 sl->num++;
1019 sl->list[sl->num] = 0;
1020 return 0;
1021}
1022
1023static void print_str_list(char **list)
1024{
1025 char **cpp;
1026
1027 for (cpp = list; *cpp; cpp++) {
1028 printf("'%s'", *cpp);
1029 if (cpp[1])
1030 fputs(", ", stdout);
1031 }
1032 fputc('\n', stdout);
1033}
1034
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001035/*
1036 * Return TRUE if the profile has the given subsection
1037 */
Theodore Ts'o577c7732013-05-19 20:03:48 -04001038static int profile_has_subsection(profile_t prof, const char *section,
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001039 const char *subsection)
1040{
1041 void *state;
1042 const char *names[4];
1043 char *name;
1044 int ret = 0;
1045
1046 names[0] = section;
1047 names[1] = subsection;
1048 names[2] = 0;
1049
Theodore Ts'o577c7732013-05-19 20:03:48 -04001050 if (profile_iterator_create(prof, names,
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001051 PROFILE_ITER_LIST_SECTION |
1052 PROFILE_ITER_RELATIONS_ONLY, &state))
1053 return 0;
1054
1055 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1056 free(name);
1057 ret = 1;
1058 }
1059
1060 profile_iterator_free(&state);
1061 return ret;
1062}
1063
Theodore Ts'o3d438362008-02-19 08:32:58 -05001064static char **parse_fs_type(const char *fs_type,
1065 const char *usage_types,
Theodore Ts'o577c7732013-05-19 20:03:48 -04001066 struct ext2_super_block *sb,
Theodore Ts'o493024e2010-06-13 14:00:00 -04001067 blk64_t fs_blocks_count,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001068 char *progname)
1069{
1070 const char *ext_type = 0;
1071 char *parse_str;
1072 char *profile_type = 0;
1073 char *cp, *t;
1074 const char *size_type;
1075 struct str_list list;
Theodore Ts'o493024e2010-06-13 14:00:00 -04001076 unsigned long long meg;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001077 int is_hurd = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001078
1079 if (init_list(&list))
1080 return 0;
1081
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001082 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1083 !strcasecmp(creator_os, "hurd")))
1084 is_hurd = 1;
1085
Theodore Ts'o3d438362008-02-19 08:32:58 -05001086 if (fs_type)
1087 ext_type = fs_type;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001088 else if (is_hurd)
1089 ext_type = "ext2";
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001090 else if (!strcmp(program_name, "mke3fs"))
1091 ext_type = "ext3";
Eric Sandeen237b7b22012-02-15 15:11:35 -05001092 else if (!strcmp(program_name, "mke4fs"))
1093 ext_type = "ext4";
Theodore Ts'o3d438362008-02-19 08:32:58 -05001094 else if (progname) {
1095 ext_type = strrchr(progname, '/');
1096 if (ext_type)
1097 ext_type++;
1098 else
1099 ext_type = progname;
1100
1101 if (!strncmp(ext_type, "mkfs.", 5)) {
1102 ext_type += 5;
1103 if (ext_type[0] == 0)
1104 ext_type = 0;
1105 } else
1106 ext_type = 0;
1107 }
1108
1109 if (!ext_type) {
1110 profile_get_string(profile, "defaults", "fs_type", 0,
1111 "ext2", &profile_type);
1112 ext_type = profile_type;
1113 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1114 ext_type = "ext3";
1115 }
1116
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001117
1118 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1119 strcmp(ext_type, "ext2")) {
1120 printf(_("\nYour mke2fs.conf file does not define the "
1121 "%s filesystem type.\n"), ext_type);
1122 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1123 !strcmp(ext_type, "ext4dev")) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001124 printf("%s", _("You probably need to install an "
1125 "updated mke2fs.conf file.\n\n"));
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001126 }
1127 if (!force) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001128 printf("%s", _("Aborting...\n"));
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001129 exit(1);
Theodore Ts'obad89b22008-08-22 21:57:29 -04001130 }
1131 }
1132
Theodore Ts'o577c7732013-05-19 20:03:48 -04001133 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
Theodore Ts'o493024e2010-06-13 14:00:00 -04001134 if (fs_blocks_count < 3 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001135 size_type = "floppy";
Theodore Ts'o493024e2010-06-13 14:00:00 -04001136 else if (fs_blocks_count < 512 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001137 size_type = "small";
Namhyung Kim22d8ce52010-11-29 17:55:13 +09001138 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001139 size_type = "default";
Namhyung Kim22d8ce52010-11-29 17:55:13 +09001140 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1141 size_type = "big";
1142 else
1143 size_type = "huge";
Theodore Ts'o3d438362008-02-19 08:32:58 -05001144
1145 if (!usage_types)
1146 usage_types = size_type;
1147
Eric Sandeen4d5cf8b2011-09-16 15:49:19 -05001148 parse_str = malloc(strlen(usage_types)+1);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001149 if (!parse_str) {
Darrick J. Wong80a18062013-12-12 13:06:44 -05001150 free(profile_type);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001151 free(list.list);
1152 return 0;
1153 }
Eric Sandeen4d5cf8b2011-09-16 15:49:19 -05001154 strcpy(parse_str, usage_types);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001155
1156 if (ext_type)
1157 push_string(&list, ext_type);
1158 cp = parse_str;
1159 while (1) {
1160 t = strchr(cp, ',');
1161 if (t)
1162 *t = '\0';
1163
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001164 if (*cp) {
Theodore Ts'o0f7479b2010-12-22 18:31:36 -05001165 if (profile_has_subsection(profile, "fs_types", cp))
1166 push_string(&list, cp);
1167 else if (strcmp(cp, "default") != 0)
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001168 fprintf(stderr,
1169 _("\nWarning: the fs_type %s is not "
Theodore Ts'o0f7479b2010-12-22 18:31:36 -05001170 "defined in mke2fs.conf\n\n"),
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001171 cp);
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001172 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001173 if (t)
1174 cp = t+1;
Theodore Ts'o577c7732013-05-19 20:03:48 -04001175 else
Theodore Ts'o3d438362008-02-19 08:32:58 -05001176 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001177 }
1178 free(parse_str);
Jim Meyering45e338f2009-02-23 18:07:50 +01001179 free(profile_type);
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001180 if (is_hurd)
1181 push_string(&list, "hurd");
Theodore Ts'o3d438362008-02-19 08:32:58 -05001182 return (list.list);
1183}
1184
Theodore Ts'o577c7732013-05-19 20:03:48 -04001185static char *get_string_from_profile(char **types, const char *opt,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001186 const char *def_val)
1187{
1188 char *ret = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001189 int i;
1190
Theodore Ts'o577c7732013-05-19 20:03:48 -04001191 for (i=0; types[i]; i++);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001192 for (i-=1; i >=0 ; i--) {
Theodore Ts'o577c7732013-05-19 20:03:48 -04001193 profile_get_string(profile, "fs_types", types[i],
Theodore Ts'o3d438362008-02-19 08:32:58 -05001194 opt, 0, &ret);
1195 if (ret)
1196 return ret;
1197 }
1198 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1199 return (ret);
1200}
1201
Theodore Ts'o577c7732013-05-19 20:03:48 -04001202static int get_int_from_profile(char **types, const char *opt, int def_val)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001203{
1204 int ret;
1205 char **cpp;
1206
1207 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
Theodore Ts'o577c7732013-05-19 20:03:48 -04001208 for (cpp = types; *cpp; cpp++)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001209 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1210 return ret;
1211}
1212
Theodore Ts'o577c7732013-05-19 20:03:48 -04001213static double get_double_from_profile(char **types, const char *opt,
Aditya Kalid3859af2011-05-10 14:51:31 -07001214 double def_val)
1215{
1216 double ret;
1217 char **cpp;
1218
1219 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
Theodore Ts'o577c7732013-05-19 20:03:48 -04001220 for (cpp = types; *cpp; cpp++)
Aditya Kalid3859af2011-05-10 14:51:31 -07001221 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1222 return ret;
1223}
1224
Theodore Ts'o577c7732013-05-19 20:03:48 -04001225static int get_bool_from_profile(char **types, const char *opt, int def_val)
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001226{
1227 int ret;
1228 char **cpp;
1229
1230 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
Theodore Ts'o577c7732013-05-19 20:03:48 -04001231 for (cpp = types; *cpp; cpp++)
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001232 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1233 return ret;
1234}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001235
Theodore Ts'od48bc602007-07-04 14:10:46 -04001236extern const char *mke2fs_default_profile;
1237static const char *default_files[] = { "<default>", 0 };
1238
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001239#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1240/*
1241 * Sets the geometry of a device (stripe/stride), and returns the
1242 * device's alignment offset, if any, or a negative error.
1243 */
Theodore Ts'o1599b472010-11-22 10:50:42 -05001244static int get_device_geometry(const char *file,
1245 struct ext2_super_block *fs_param,
1246 int psector_size)
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001247{
1248 int rc = -1;
1249 int blocksize;
1250 blkid_probe pr;
1251 blkid_topology tp;
1252 unsigned long min_io;
1253 unsigned long opt_io;
Eric Sandeen13b0b122010-01-23 21:50:45 -06001254 struct stat statbuf;
1255
1256 /* Nothing to do for a regular file */
1257 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1258 return 0;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001259
1260 pr = blkid_new_probe_from_filename(file);
1261 if (!pr)
1262 goto out;
1263
1264 tp = blkid_probe_get_topology(pr);
1265 if (!tp)
1266 goto out;
1267
1268 min_io = blkid_topology_get_minimum_io_size(tp);
1269 opt_io = blkid_topology_get_optimal_io_size(tp);
1270 blocksize = EXT2_BLOCK_SIZE(fs_param);
Theodore Ts'o1599b472010-11-22 10:50:42 -05001271 if ((min_io == 0) && (psector_size > blocksize))
1272 min_io = psector_size;
1273 if ((opt_io == 0) && min_io)
1274 opt_io = min_io;
1275 if ((opt_io == 0) && (psector_size > blocksize))
1276 opt_io = psector_size;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001277
Eric Sandeend5687822011-04-04 15:11:52 -04001278 /* setting stripe/stride to blocksize is pointless */
1279 if (min_io > blocksize)
1280 fs_param->s_raid_stride = min_io / blocksize;
1281 if (opt_io > blocksize)
1282 fs_param->s_raid_stripe_width = opt_io / blocksize;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001283
1284 rc = blkid_topology_get_alignment_offset(tp);
1285out:
1286 blkid_free_probe(pr);
1287 return rc;
1288}
1289#endif
1290
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291static void PRS(int argc, char *argv[])
1292{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001293 int b, c;
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001294 int cluster_size = 0;
Eric Sandeen79e62402008-07-06 18:36:56 -04001295 char *tmp, **cpp;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001296 int blocksize = 0;
1297 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001298 int inode_size = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001299 unsigned long flex_bg_size = 0;
Aditya Kalid3859af2011-05-10 14:51:31 -07001300 double reserved_ratio = -1.0;
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001301 int lsector_size = 0, psector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001302 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001303 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001304 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001305 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001306 char * extended_opts = 0;
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001307 char * fs_type = 0;
1308 char * usage_types = 0;
Jose R. Santos02d6f472010-06-13 13:00:00 -04001309 blk64_t dev_size;
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001310 /*
1311 * NOTE: A few words about fs_blocks_count and blocksize:
1312 *
1313 * Initially, blocksize is set to zero, which implies 1024.
1314 * If -b is specified, blocksize is updated to the user's value.
1315 *
1316 * Next, the device size or the user's "blocks" command line argument
1317 * is used to set fs_blocks_count; the units are blocksize.
1318 *
1319 * Later, if blocksize hasn't been set and the profile specifies a
1320 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1321 * appropriately. Note the change in units!
1322 *
1323 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1324 */
Theodore Ts'o493024e2010-06-13 14:00:00 -04001325 blk64_t fs_blocks_count = 0;
Theodore Ts'o756df352002-03-07 20:52:12 -05001326#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001327 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001328#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001329 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001330 int s_opt = -1, r_opt = -1;
1331 char *fs_features = 0;
1332 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001333 char *newpath;
1334 int pathlen = sizeof(PATH_SET) + 1;
1335
1336 if (oldpath)
1337 pathlen += strlen(oldpath);
1338 newpath = malloc(pathlen);
Namhyung Kim9ec68af2010-12-16 18:40:40 +09001339 if (!newpath) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001340 fprintf(stderr, "%s",
1341 _("Couldn't allocate memory for new PATH.\n"));
Namhyung Kim9ec68af2010-12-16 18:40:40 +09001342 exit(1);
1343 }
Theodore Ts'o642935c2006-11-14 23:38:17 -05001344 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001345
Theodore Ts'o3839e651997-04-26 13:21:57 +00001346 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001347 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001348 strcat (newpath, ":");
1349 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001350 }
1351 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001352
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001353 tmp = getenv("MKE2FS_SYNC");
1354 if (tmp)
1355 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001356
1357 /* Determine the system page size if possible */
1358#ifdef HAVE_SYSCONF
1359#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1360#define _SC_PAGESIZE _SC_PAGE_SIZE
1361#endif
1362#ifdef _SC_PAGESIZE
1363 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001364 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001365 sys_page_size = sysval;
1366#endif /* _SC_PAGESIZE */
1367#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001368
1369 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1370 config_fn[0] = tmp;
1371 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001372 retval = profile_init(config_fn, &profile);
1373 if (retval == ENOENT) {
Namhyung Kim9ec68af2010-12-16 18:40:40 +09001374 retval = profile_init(default_files, &profile);
1375 if (retval)
1376 goto profile_error;
1377 retval = profile_set_default(profile, mke2fs_default_profile);
1378 if (retval)
1379 goto profile_error;
1380 } else if (retval) {
1381profile_error:
1382 fprintf(stderr, _("Couldn't init profile successfully"
1383 " (error: %ld).\n"), retval);
1384 exit(1);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001385 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001386
Theodore Ts'o3839e651997-04-26 13:21:57 +00001387 setbuf(stdout, NULL);
1388 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001389 add_error_table(&et_ext2_error_table);
1390 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001391 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1392 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001393
Theodore Ts'o756df352002-03-07 20:52:12 -05001394#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001395 if (uname(&ut)) {
1396 perror("uname");
1397 exit(1);
1398 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001399 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001400 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001401 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001402#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001403
1404 if (argc && *argv) {
1405 program_name = get_progname(*argv);
1406
1407 /* If called as mkfs.ext3, create a journal inode */
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001408 if (!strcmp(program_name, "mkfs.ext3") ||
1409 !strcmp(program_name, "mke3fs"))
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001410 journal_size = -1;
1411 }
1412
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001413 while ((c = getopt (argc, argv,
Theodore Ts'o37c8db72012-03-22 16:00:49 -04001414 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001415 switch (c) {
1416 case 'b':
Theodore Ts'o86a985e2013-01-14 19:03:11 -05001417 blocksize = parse_num_blocks2(optarg, -1);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001418 b = (blocksize > 0) ? blocksize : -blocksize;
1419 if (b < EXT2_MIN_BLOCK_SIZE ||
Theodore Ts'o86a985e2013-01-14 19:03:11 -05001420 b > EXT2_MAX_BLOCK_SIZE) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001421 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001422 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001423 exit(1);
1424 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001425 if (blocksize > 4096)
1426 fprintf(stderr, _("Warning: blocksize %d not "
1427 "usable on most systems.\n"),
1428 blocksize);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001429 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001430 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001431 int_log2(blocksize >>
1432 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001433 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001434 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001435 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001436 break;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001437 case 'C':
Theodore Ts'o86a985e2013-01-14 19:03:11 -05001438 cluster_size = parse_num_blocks2(optarg, -1);
Theodore Ts'o4b20ea22013-01-14 17:28:00 -05001439 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
Theodore Ts'o86a985e2013-01-14 19:03:11 -05001440 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001441 com_err(program_name, 0,
Eric Sandeen28e2cb92011-10-04 17:12:11 -05001442 _("invalid cluster size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001443 optarg);
1444 exit(1);
1445 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001446 break;
Theodore Ts'o37c8db72012-03-22 16:00:49 -04001447 case 'D':
1448 direct_io = 1;
1449 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001450 case 'R':
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001451 com_err(program_name, 0, "%s",
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001452 _("'-R' is deprecated, use '-E' instead"));
1453 /* fallthrough */
1454 case 'E':
1455 extended_opts = optarg;
1456 break;
1457 case 'F':
1458 force++;
1459 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001460 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001461 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001462 if (*tmp) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001463 com_err(program_name, 0, "%s",
1464 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001465 exit(1);
1466 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001467 if ((fs_param.s_blocks_per_group % 8) != 0) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001468 com_err(program_name, 0, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001469 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001470 exit(1);
1471 }
1472 break;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001473 case 'G':
1474 flex_bg_size = strtoul(optarg, &tmp, 0);
1475 if (*tmp) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001476 com_err(program_name, 0, "%s",
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001477 _("Illegal number for flex_bg size"));
1478 exit(1);
1479 }
Theodore Ts'od5314502010-05-10 21:49:58 -04001480 if (flex_bg_size < 1 ||
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001481 (flex_bg_size & (flex_bg_size-1)) != 0) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001482 com_err(program_name, 0, "%s",
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001483 _("flex_bg size must be a power of 2"));
1484 exit(1);
1485 }
1486 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001487 case 'i':
1488 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001489 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1490 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001491 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001492 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001493 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001494 optarg, EXT2_MIN_BLOCK_SIZE,
Tim Smalle447ba32010-07-30 20:43:11 -04001495 EXT2_MAX_BLOCK_SIZE * 1024);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001496 exit(1);
1497 }
1498 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001499 case 'I':
1500 inode_size = strtoul(optarg, &tmp, 0);
1501 if (*tmp) {
1502 com_err(program_name, 0,
1503 _("invalid inode size - %s"), optarg);
1504 exit(1);
1505 }
1506 break;
1507 case 'j':
1508 if (!journal_size)
1509 journal_size = -1;
1510 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001511 case 'J':
1512 parse_journal_opts(optarg);
1513 break;
Eric Sandeen5827d242009-10-07 16:49:17 -05001514 case 'K':
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001515 fprintf(stderr, "%s",
1516 _("Warning: -K option is deprecated and "
1517 "should not be used anymore. Use "
1518 "\'-E nodiscard\' extended option "
1519 "instead!\n"));
Eric Sandeen5827d242009-10-07 16:49:17 -05001520 discard = 0;
1521 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001522 case 'l':
Darrick J. Wong80a18062013-12-12 13:06:44 -05001523 bad_blocks_filename = realloc(bad_blocks_filename,
1524 strlen(optarg) + 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001525 if (!bad_blocks_filename) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001526 com_err(program_name, ENOMEM, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001527 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001528 exit(1);
1529 }
1530 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001531 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001532 case 'L':
1533 volume_label = optarg;
1534 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001535 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001536 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o8d822452009-03-06 02:23:59 -05001537 if ( *tmp || reserved_ratio > 50 ||
1538 reserved_ratio < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001539 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001540 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001541 optarg);
1542 exit(1);
1543 }
1544 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001545 case 'M':
1546 mount_dir = optarg;
1547 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001548 case 'n':
1549 noaction++;
1550 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001551 case 'N':
1552 num_inodes = strtoul(optarg, &tmp, 0);
1553 if (*tmp) {
1554 com_err(program_name, 0,
1555 _("bad num inodes - %s"), optarg);
1556 exit(1);
1557 }
1558 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001559 case 'o':
1560 creator_os = optarg;
1561 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001562 case 'O':
1563 fs_features = optarg;
1564 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001565 case 'q':
1566 quiet = 1;
1567 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001568 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001569 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001570 if (*tmp) {
1571 com_err(program_name, 0,
1572 _("bad revision level - %s"), optarg);
1573 exit(1);
1574 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001575 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001576 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001577 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001578 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001579 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001580 case 'S':
1581 super_only = 1;
1582 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001583 case 't':
Eric Sandeen9f7c3af2011-09-16 15:49:32 -05001584 if (fs_type) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001585 com_err(program_name, 0, "%s",
Eric Sandeen9f7c3af2011-09-16 15:49:32 -05001586 _("The -t option may only be used once"));
1587 exit(1);
1588 }
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001589 fs_type = strdup(optarg);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001590 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001591 case 'T':
Eric Sandeen9f7c3af2011-09-16 15:49:32 -05001592 if (usage_types) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001593 com_err(program_name, 0, "%s",
Eric Sandeen9f7c3af2011-09-16 15:49:32 -05001594 _("The -T option may only be used once"));
1595 exit(1);
1596 }
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001597 usage_types = strdup(optarg);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001598 break;
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001599 case 'U':
1600 fs_uuid = optarg;
1601 break;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001602 case 'v':
1603 verbose = 1;
1604 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001605 case 'V':
1606 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001607 show_version_only++;
1608 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001609 default:
1610 usage();
1611 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001612 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001613 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001614 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001615 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001616
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001617 if (!quiet || show_version_only)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001618 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
Theodore Ts'o38798572003-04-16 15:29:39 -04001619 E2FSPROGS_DATE);
1620
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001621 if (show_version_only) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001622 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001623 error_message(EXT2_ET_BASE));
1624 exit(0);
1625 }
1626
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001627 /*
1628 * If there's no blocksize specified and there is a journal
1629 * device, use it to figure out the blocksize
1630 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001631 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001632 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001633 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001634
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001635#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001636 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1637 io_ptr = test_io_manager;
1638 test_io_backing_manager = unix_io_manager;
1639 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001640#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001641 io_ptr = unix_io_manager;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001642 retval = ext2fs_open(journal_device,
1643 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001644 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001645 if (retval) {
1646 com_err(program_name, retval,
1647 _("while trying to open journal device %s\n"),
1648 journal_device);
1649 exit(1);
1650 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001651 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001652 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001653 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001654 "minimum blocksize %d\n"), jfs->blocksize,
1655 -blocksize);
1656 exit(1);
1657 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001658 blocksize = jfs->blocksize;
Theodore Ts'of8df04b2008-07-06 20:57:17 -04001659 printf(_("Using journal device's blocksize: %d\n"), blocksize);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001660 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001661 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1662 ext2fs_close(jfs);
1663 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001664
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001665 if (optind < argc) {
Theodore Ts'o493024e2010-06-13 14:00:00 -04001666 fs_blocks_count = parse_num_blocks2(argv[optind++],
1667 fs_param.s_log_block_size);
1668 if (!fs_blocks_count) {
Eric Sandeen792cce32010-08-12 14:40:44 -04001669 com_err(program_name, 0,
Theodore Ts'o9d92a202010-09-24 22:40:21 -04001670 _("invalid blocks '%s' on device '%s'"),
Eric Sandeen792cce32010-08-12 14:40:44 -04001671 argv[optind - 1], device_name);
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001672 exit(1);
1673 }
1674 }
1675 if (optind < argc)
1676 usage();
1677
Theodore Ts'o74becf31997-04-26 14:37:06 +00001678 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001679 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001680 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001681
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001682 /* Determine the size of the device (if possible) */
Theodore Ts'o493024e2010-06-13 14:00:00 -04001683 if (noaction && fs_blocks_count) {
1684 dev_size = fs_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001685 retval = 0;
Theodore Ts'o493024e2010-06-13 14:00:00 -04001686 } else
Jose R. Santos02d6f472010-06-13 13:00:00 -04001687 retval = ext2fs_get_device_size2(device_name,
1688 EXT2_BLOCK_SIZE(&fs_param),
1689 &dev_size);
Jose R. Santos02d6f472010-06-13 13:00:00 -04001690
Theodore Ts'oa789d841998-03-30 01:20:55 +00001691 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001692 com_err(program_name, retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001693 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001694 exit(1);
1695 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001696 if (!fs_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001697 if (retval == EXT2_ET_UNIMPLEMENTED) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001698 com_err(program_name, 0, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001699 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001700 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001701 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001702 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001703 } else {
1704 if (dev_size == 0) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001705 com_err(program_name, 0, "%s",
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001706 _("Device size reported to be zero. "
1707 "Invalid partition specified, or\n\t"
1708 "partition table wasn't reread "
1709 "after running fdisk, due to\n\t"
1710 "a modified partition being busy "
1711 "and in use. You may need to reboot\n\t"
1712 "to re-read your partition table.\n"
1713 ));
1714 exit(1);
1715 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001716 fs_blocks_count = dev_size;
1717 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1718 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001719 EXT2_BLOCK_SIZE(&fs_param))-1));
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001720 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001721 } else if (!force && (fs_blocks_count > dev_size)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001722 com_err(program_name, 0, "%s",
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001723 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001724 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001725 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001726
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001727 if (!fs_type)
1728 profile_get_string(profile, "devices", device_name,
1729 "fs_type", 0, &fs_type);
1730 if (!usage_types)
1731 profile_get_string(profile, "devices", device_name,
1732 "usage_types", 0, &usage_types);
1733
Theodore Ts'o493024e2010-06-13 14:00:00 -04001734 /*
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001735 * We have the file system (or device) size, so we can now
1736 * determine the appropriate file system types so the fs can
1737 * be appropriately configured.
1738 */
1739 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1740 fs_blocks_count ? fs_blocks_count : dev_size,
1741 argv[0]);
1742 if (!fs_types) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001743 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001744 exit(1);
1745 }
1746
1747 /* Figure out what features should be enabled */
1748
1749 tmp = NULL;
1750 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1751 tmp = get_string_from_profile(fs_types, "base_features",
1752 "sparse_super,filetype,resize_inode,dir_index");
1753 edit_feature(tmp, &fs_param.s_feature_compat);
1754 free(tmp);
1755
Eric Sandeen6a426c92011-02-17 15:56:17 -06001756 /* And which mount options as well */
1757 tmp = get_string_from_profile(fs_types, "default_mntopts",
1758 "acl,user_xattr");
1759 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1760 if (tmp)
1761 free(tmp);
1762
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001763 for (cpp = fs_types; *cpp; cpp++) {
1764 tmp = NULL;
1765 profile_get_string(profile, "fs_types", *cpp,
1766 "features", "", &tmp);
1767 if (tmp && *tmp)
1768 edit_feature(tmp, &fs_param.s_feature_compat);
1769 if (tmp)
1770 free(tmp);
1771 }
1772 tmp = get_string_from_profile(fs_types, "default_features",
1773 "");
1774 }
1775 edit_feature(fs_features ? fs_features : tmp,
1776 &fs_param.s_feature_compat);
1777 if (tmp)
1778 free(tmp);
1779
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001780 /* Get the hardware sector sizes, if available */
1781 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1782 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001783 com_err(program_name, retval, "%s",
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001784 _("while trying to determine hardware sector size"));
1785 exit(1);
1786 }
1787 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1788 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001789 com_err(program_name, retval, "%s",
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001790 _("while trying to determine physical sector size"));
1791 exit(1);
1792 }
1793
1794 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1795 if (tmp != NULL)
1796 lsector_size = atoi(tmp);
1797 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1798 if (tmp != NULL)
1799 psector_size = atoi(tmp);
1800
1801 /* Older kernels may not have physical/logical distinction */
1802 if (!psector_size)
1803 psector_size = lsector_size;
1804
1805 if (blocksize <= 0) {
1806 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1807
1808 if (use_bsize == -1) {
1809 use_bsize = sys_page_size;
1810 if ((linux_version_code < (2*65536 + 6*256)) &&
1811 (use_bsize > 4096))
1812 use_bsize = 4096;
1813 }
1814 if (lsector_size && use_bsize < lsector_size)
1815 use_bsize = lsector_size;
1816 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1817 use_bsize = -blocksize;
1818 blocksize = use_bsize;
1819 fs_blocks_count /= (blocksize / 1024);
1820 } else {
1821 if (blocksize < lsector_size) { /* Impossible */
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001822 com_err(program_name, EINVAL, "%s",
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001823 _("while setting blocksize; too small "
1824 "for device\n"));
1825 exit(1);
1826 } else if ((blocksize < psector_size) &&
1827 (psector_size <= sys_page_size)) { /* Suboptimal */
1828 fprintf(stderr, _("Warning: specified blocksize %d is "
1829 "less than device physical sectorsize %d\n"),
1830 blocksize, psector_size);
1831 }
1832 }
1833
1834 fs_param.s_log_block_size =
1835 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1836
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001837 /*
Theodore Ts'o493024e2010-06-13 14:00:00 -04001838 * We now need to do a sanity check of fs_blocks_count for
1839 * 32-bit vs 64-bit block number support.
1840 */
Theodore Ts'o493024e2010-06-13 14:00:00 -04001841 if ((fs_blocks_count > MAX_32_NUM) &&
1842 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1843 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1844 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1845 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1846 }
1847 if ((fs_blocks_count > MAX_32_NUM) &&
1848 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1849 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1850 "too big to be expressed\n\t"
1851 "in 32 bits using a blocksize of %d.\n"),
1852 program_name, fs_blocks_count, device_name,
1853 EXT2_BLOCK_SIZE(&fs_param));
1854 exit(1);
1855 }
1856
1857 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1858
Theodore Ts'ob4d51052008-07-06 20:24:29 -04001859 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1860 fs_types[0] = strdup("journal");
1861 fs_types[1] = 0;
1862 }
1863
1864 if (verbose) {
1865 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1866 print_str_list(fs_types);
1867 }
1868
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001869 if (r_opt == EXT2_GOOD_OLD_REV &&
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001870 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
Theodore Ts'o92fb8542008-07-18 21:08:56 -04001871 fs_param.s_feature_ro_compat)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001872 fprintf(stderr, "%s", _("Filesystem features not supported "
1873 "with revision 0 filesystems\n"));
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001874 exit(1);
1875 }
1876
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001877 if (s_opt > 0) {
1878 if (r_opt == EXT2_GOOD_OLD_REV) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001879 fprintf(stderr, "%s",
1880 _("Sparse superblocks not supported "
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001881 "with revision 0 filesystems\n"));
1882 exit(1);
1883 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001884 fs_param.s_feature_ro_compat |=
1885 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001886 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001887 fs_param.s_feature_ro_compat &=
1888 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1889
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001890 if (journal_size != 0) {
1891 if (r_opt == EXT2_GOOD_OLD_REV) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001892 fprintf(stderr, "%s", _("Journals not supported with "
1893 "revision 0 filesystems\n"));
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001894 exit(1);
1895 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001896 fs_param.s_feature_compat |=
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001897 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001898 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001899
Aditya Kalid3859af2011-05-10 14:51:31 -07001900 /* Get reserved_ratio from profile if not specified on cmd line. */
1901 if (reserved_ratio < 0.0) {
1902 reserved_ratio = get_double_from_profile(
1903 fs_types, "reserved_ratio", 5.0);
1904 if (reserved_ratio > 50 || reserved_ratio < 0) {
1905 com_err(program_name, 0,
1906 _("invalid reserved blocks percent - %lf"),
1907 reserved_ratio);
1908 exit(1);
1909 }
1910 }
1911
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001912 if (fs_param.s_feature_incompat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001913 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001914 reserved_ratio = 0;
1915 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1916 fs_param.s_feature_compat = 0;
1917 fs_param.s_feature_ro_compat = 0;
1918 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001919
Darrick J. Wongd11f92a2013-10-11 21:20:22 -04001920 /* Check the user's mkfs options for 64bit */
1921 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1922 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001923 printf("%s", _("Extents MUST be enabled for a 64-bit "
1924 "filesystem. Pass -O extents to rectify.\n"));
Darrick J. Wongd11f92a2013-10-11 21:20:22 -04001925 exit(1);
1926 }
1927
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001928 /* Set first meta blockgroup via an environment variable */
1929 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001930 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001931 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001932 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001933 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001934 if (!cluster_size)
1935 cluster_size = get_int_from_profile(fs_types,
1936 "cluster_size",
Theodore Ts'ob12a0bc2011-06-16 10:11:06 -04001937 blocksize*16);
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001938 fs_param.s_log_cluster_size =
1939 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
Theodore Ts'o4b20ea22013-01-14 17:28:00 -05001940 if (fs_param.s_log_cluster_size &&
1941 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001942 com_err(program_name, 0, "%s",
Theodore Ts'o4b20ea22013-01-14 17:28:00 -05001943 _("The cluster size may not be "
1944 "smaller than the block size.\n"));
1945 exit(1);
1946 }
Zheng Liu9cf69bb2013-01-13 17:08:13 +08001947 } else if (cluster_size) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05001948 com_err(program_name, 0, "%s",
Zheng Liu9cf69bb2013-01-13 17:08:13 +08001949 _("specifying a cluster size requires the "
1950 "bigalloc feature"));
1951 exit(1);
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001952 } else
1953 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1954
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001955 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001956 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1957 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001958 if (inode_ratio < blocksize)
1959 inode_ratio = blocksize;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001960 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1961 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001962 }
1963
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001964#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
Theodore Ts'o1599b472010-11-22 10:50:42 -05001965 retval = get_device_geometry(device_name, &fs_param, psector_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001966 if (retval < 0) {
1967 fprintf(stderr,
Eric Sandeen13b0b122010-01-23 21:50:45 -06001968 _("warning: Unable to get device geometry for %s\n"),
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001969 device_name);
1970 } else if (retval) {
1971 printf(_("%s alignment is offset by %lu bytes.\n"),
1972 device_name, retval);
1973 printf(_("This may result in very poor performance, "
1974 "(re)-partitioning suggested.\n"));
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001975 }
1976#endif
1977
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001978 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001979
Theodore Ts'o49cdefd2012-12-28 18:26:12 -05001980 /*
1981 * Initialize s_desc_size so that the parse_extended_opts()
1982 * can correctly handle "-E resize=NNN" if the 64-bit option
1983 * is set.
1984 */
1985 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
1986 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
1987
Yury V. Zaytsev45792c12011-09-15 23:08:52 -04001988 /* This check should happen beyond the last assignment to blocksize */
1989 if (blocksize > sys_page_size) {
1990 if (!force) {
1991 com_err(program_name, 0,
1992 _("%d-byte blocks too big for system (max %d)"),
1993 blocksize, sys_page_size);
1994 proceed_question();
1995 }
1996 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1997 "(max %d), forced to continue\n"),
1998 blocksize, sys_page_size);
1999 }
2000
Theodore Ts'o210fd2c2010-10-01 10:47:38 -04002001 lazy_itable_init = 0;
2002 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2003 lazy_itable_init = 1;
2004
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002005 lazy_itable_init = get_bool_from_profile(fs_types,
Theodore Ts'o210fd2c2010-10-01 10:47:38 -04002006 "lazy_itable_init",
2007 lazy_itable_init);
Lukas Czerner7fe5ff32010-11-18 14:38:41 +01002008 discard = get_bool_from_profile(fs_types, "discard" , discard);
Andreas Dilger6c546892011-06-11 12:17:29 -04002009 journal_flags |= get_bool_from_profile(fs_types,
2010 "lazy_journal_init", 0) ?
2011 EXT2_MKJOURNAL_LAZYINIT : 0;
Theodore Ts'o304e11c2012-04-05 12:30:02 -07002012 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002013
Theodore Ts'ob8182052014-01-28 12:58:56 -05002014 if (!journal_location_string)
2015 journal_location_string = get_string_from_profile(fs_types,
2016 "journal_location", "");
2017 if ((journal_location == ~0ULL) && journal_location_string &&
2018 *journal_location_string)
2019 journal_location = parse_num_blocks2(journal_location_string,
2020 fs_param.s_log_block_size);
2021 free(journal_location_string);
2022
Theodore Ts'o2d363582008-05-14 18:09:37 -04002023 /* Get options from profile */
2024 for (cpp = fs_types; *cpp; cpp++) {
2025 tmp = NULL;
2026 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2027 if (tmp && *tmp)
2028 parse_extended_opts(&fs_param, tmp);
Jim Meyering45e338f2009-02-23 18:07:50 +01002029 free(tmp);
Theodore Ts'o2d363582008-05-14 18:09:37 -04002030 }
2031
Theodore Ts'oc6a44132005-01-05 11:12:20 -05002032 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002033 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002034
Zheng Liuf45011d2013-01-06 20:25:17 +08002035 /* Can't support bigalloc feature without extents feature */
2036 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2037 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002038 com_err(program_name, 0, "%s",
Zheng Liuf45011d2013-01-06 20:25:17 +08002039 _("Can't support bigalloc feature without "
2040 "extents feature"));
2041 exit(1);
2042 }
2043
Theodore Ts'ocecfb4c2013-10-17 21:49:16 -07002044 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2045 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002046 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2047 "features are not compatible.\n"
2048 "They can not be both enabled "
2049 "simultaneously.\n"));
Theodore Ts'ocecfb4c2013-10-17 21:49:16 -07002050 exit(1);
2051 }
2052
Theodore Ts'o447da242013-03-31 20:29:46 -04002053 if (!quiet &&
2054 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002055 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2056 "still under development\n"
Theodore Ts'oa713a7f2013-01-21 19:07:38 -05002057 "See https://ext4.wiki.kernel.org/"
2058 "index.php/Bigalloc for more information\n\n"));
2059
Theodore Ts'o447da242013-03-31 20:29:46 -04002060 if (!quiet &&
2061 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002062 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2063 "still under development\n"
Theodore Ts'oa713a7f2013-01-21 19:07:38 -05002064 "See https://ext4.wiki.kernel.org/"
2065 "index.php/Quota for more information\n\n"));
2066
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002067 /* Since sparse_super is the default, we would only have a problem
2068 * here if it was explicitly disabled.
2069 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002070 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2071 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002072 com_err(program_name, 0, "%s",
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002073 _("reserved online resize blocks not supported "
2074 "on non-sparse filesystem"));
2075 exit(1);
2076 }
2077
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002078 if (fs_param.s_blocks_per_group) {
2079 if (fs_param.s_blocks_per_group < 256 ||
2080 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002081 com_err(program_name, 0, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002082 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00002083 exit(1);
2084 }
2085 }
2086
Theodore Ts'o2a3b1c62013-01-14 17:30:23 -05002087 /*
2088 * If the bigalloc feature is enabled, then the -g option will
2089 * specify the number of clusters per group.
2090 */
2091 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2092 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2093 fs_param.s_blocks_per_group = 0;
2094 }
2095
Theodore Ts'o3d438362008-02-19 08:32:58 -05002096 if (inode_size == 0)
2097 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Theodore Ts'o9ba40002008-04-22 08:27:01 -04002098 if (!flex_bg_size && (fs_param.s_feature_incompat &
2099 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2100 flex_bg_size = get_int_from_profile(fs_types,
2101 "flex_bg_size", 16);
2102 if (flex_bg_size) {
2103 if (!(fs_param.s_feature_incompat &
2104 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002105 com_err(program_name, 0, "%s",
Theodore Ts'o9ba40002008-04-22 08:27:01 -04002106 _("Flex_bg feature not enabled, so "
2107 "flex_bg size may not be specified"));
2108 exit(1);
2109 }
2110 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2111 }
Andreas Dilger067911a2006-07-15 22:08:20 -04002112
2113 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06002114 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002115 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06002116 inode_size & (inode_size - 1)) {
2117 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04002118 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06002119 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04002120 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06002121 exit(1);
2122 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002123 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06002124 }
2125
Eric Sandeenf3358642006-09-12 14:56:17 -04002126 /* Make sure number of inodes specified will fit in 32 bits */
2127 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04002128 unsigned long long n;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002129 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
Theodore Ts'o493024e2010-06-13 14:00:00 -04002130 if (n > MAX_32_NUM) {
Jose R. Santos02d6f472010-06-13 13:00:00 -04002131 if (fs_param.s_feature_incompat &
2132 EXT4_FEATURE_INCOMPAT_64BIT)
Theodore Ts'o493024e2010-06-13 14:00:00 -04002133 num_inodes = MAX_32_NUM;
Jose R. Santos02d6f472010-06-13 13:00:00 -04002134 else {
2135 com_err(program_name, 0,
Theodore Ts'o84888e52011-10-08 13:32:00 -04002136 _("too many inodes (%llu), raise "
Jose R. Santos02d6f472010-06-13 13:00:00 -04002137 "inode ratio?"), n);
2138 exit(1);
2139 }
Eric Sandeenf3358642006-09-12 14:56:17 -04002140 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04002141 } else if (num_inodes > MAX_32_NUM) {
Eric Sandeenf3358642006-09-12 14:56:17 -04002142 com_err(program_name, 0,
2143 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04002144 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04002145 exit(1);
2146 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00002147 /*
2148 * Calculate number of inodes based on the inode ratio
2149 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002150 fs_param.s_inodes_count = num_inodes ? num_inodes :
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002151 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002152
Theodore Ts'o577c7732013-05-19 20:03:48 -04002153 if ((((unsigned long long)fs_param.s_inodes_count) *
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002154 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002155 ((ext2fs_blocks_count(&fs_param)) *
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002156 EXT2_BLOCK_SIZE(&fs_param))) {
2157 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2158 "(%u) too big for a\n\t"
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002159 "filesystem with %llu blocks, "
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002160 "specify higher inode_ratio (-i)\n\t"
2161 "or lower inode count (-N).\n"),
2162 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002163 fs_param.s_inodes_count,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002164 (unsigned long long) ext2fs_blocks_count(&fs_param));
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002165 exit(1);
2166 }
2167
Theodore Ts'o3839e651997-04-26 13:21:57 +00002168 /*
2169 * Calculate number of blocks to reserve
2170 */
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002171 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2172 ext2fs_blocks_count(&fs_param) / 100.0);
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04002173
2174 free(fs_type);
2175 free(usage_types);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002176}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002177
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302178static int should_do_undo(const char *name)
2179{
2180 errcode_t retval;
2181 io_channel channel;
2182 __u16 s_magic;
2183 struct ext2_super_block super;
2184 io_manager manager = unix_io_manager;
2185 int csum_flag, force_undo;
2186
2187 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2188 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2189 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2190 if (!force_undo && (!csum_flag || !lazy_itable_init))
2191 return 0;
2192
2193 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2194 if (retval) {
2195 /*
2196 * We don't handle error cases instead we
2197 * declare that the file system doesn't exist
2198 * and let the rest of mke2fs take care of
2199 * error
2200 */
2201 retval = 0;
2202 goto open_err_out;
2203 }
2204
2205 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -04002206 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302207 if (retval) {
2208 retval = 0;
2209 goto err_out;
2210 }
2211
2212#if defined(WORDS_BIGENDIAN)
2213 s_magic = ext2fs_swab16(super.s_magic);
2214#else
2215 s_magic = super.s_magic;
2216#endif
2217
2218 if (s_magic == EXT2_SUPER_MAGIC)
2219 retval = 1;
2220
2221err_out:
2222 io_channel_close(channel);
2223
2224open_err_out:
2225
2226 return retval;
2227}
2228
2229static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2230{
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002231 errcode_t retval = ENOMEM;
Eric Sandeen30295f12011-09-16 15:49:40 -05002232 char *tdb_dir = NULL, *tdb_file = NULL;
Theodore Ts'o577c7732013-05-19 20:03:48 -04002233 char *dev_name, *tmp_name;
Eric Sandeen30295f12011-09-16 15:49:40 -05002234 int free_tdb_dir = 0;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302235
2236 /*
2237 * Configuration via a conf file would be
2238 * nice
2239 */
2240 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
Eric Sandeen30295f12011-09-16 15:49:40 -05002241 if (!tdb_dir) {
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302242 profile_get_string(profile, "defaults",
2243 "undo_dir", 0, "/var/lib/e2fsprogs",
2244 &tdb_dir);
Eric Sandeen30295f12011-09-16 15:49:40 -05002245 free_tdb_dir = 1;
2246 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302247
2248 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
Darrick J. Wong80a18062013-12-12 13:06:44 -05002249 access(tdb_dir, W_OK)) {
2250 if (free_tdb_dir)
2251 free(tdb_dir);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302252 return 0;
Darrick J. Wong80a18062013-12-12 13:06:44 -05002253 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302254
2255 tmp_name = strdup(name);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002256 if (!tmp_name)
2257 goto errout;
Theodore Ts'o577c7732013-05-19 20:03:48 -04002258 dev_name = basename(tmp_name);
2259 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002260 if (!tdb_file) {
2261 free(tmp_name);
2262 goto errout;
2263 }
Theodore Ts'o577c7732013-05-19 20:03:48 -04002264 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002265 free(tmp_name);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302266
Theodore Ts'o250bf9a2014-01-10 21:39:27 -05002267 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2268 retval = errno;
2269 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302270 }
2271
2272 set_undo_io_backing_manager(*io_ptr);
2273 *io_ptr = undo_io_manager;
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002274 retval = set_undo_io_backup_file(tdb_file);
2275 if (retval)
2276 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302277 printf(_("Overwriting existing filesystem; this can be undone "
2278 "using the command:\n"
2279 " e2undo %s %s\n\n"), tdb_file, name);
Eric Sandeen79e62402008-07-06 18:36:56 -04002280
Eric Sandeen30295f12011-09-16 15:49:40 -05002281 if (free_tdb_dir)
2282 free(tdb_dir);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04002283 free(tdb_file);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002284 return 0;
2285
2286errout:
Eric Sandeen30295f12011-09-16 15:49:40 -05002287 if (free_tdb_dir)
2288 free(tdb_dir);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002289 free(tdb_file);
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002290 com_err(program_name, retval, "%s",
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002291 _("while trying to setup undo file\n"));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302292 return retval;
2293}
2294
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002295static int mke2fs_discard_device(ext2_filsys fs)
2296{
2297 struct ext2fs_numeric_progress_struct progress;
2298 blk64_t blocks = ext2fs_blocks_count(fs->super);
2299 blk64_t count = DISCARD_STEP_MB;
Lukas Czernerd2bfdc72011-09-15 23:44:59 -04002300 blk64_t cur;
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002301 int retval = 0;
2302
Lukas Czernerd2bfdc72011-09-15 23:44:59 -04002303 /*
2304 * Let's try if discard really works on the device, so
2305 * we do not print numeric progress resulting in failure
2306 * afterwards.
2307 */
2308 retval = io_channel_discard(fs->io, 0, fs->blocksize);
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002309 if (retval)
2310 return retval;
Lukas Czernerd2bfdc72011-09-15 23:44:59 -04002311 cur = fs->blocksize;
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002312
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002313 count *= (1024 * 1024);
2314 count /= fs->blocksize;
2315
2316 ext2fs_numeric_progress_init(fs, &progress,
2317 _("Discarding device blocks: "),
2318 blocks);
2319 while (cur < blocks) {
Theodore Ts'oc498cb12012-09-22 21:26:48 -04002320 ext2fs_numeric_progress_update(fs, &progress, cur);
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002321
2322 if (cur + count > blocks)
2323 count = blocks - cur;
2324
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002325 retval = io_channel_discard(fs->io, cur, count);
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002326 if (retval)
2327 break;
2328 cur += count;
2329 }
2330
2331 if (retval) {
2332 ext2fs_numeric_progress_close(fs, &progress,
2333 _("failed - "));
2334 if (!quiet)
2335 printf("%s\n",error_message(retval));
2336 } else
2337 ext2fs_numeric_progress_close(fs, &progress,
2338 _("done \n"));
2339
2340 return retval;
2341}
2342
Andreas Dilger96367ad2011-06-15 22:17:38 -04002343static void fix_cluster_bg_counts(ext2_filsys fs)
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002344{
Theodore Ts'o6e49e6a2014-01-19 19:44:45 -05002345 blk64_t block, num_blocks, last_block, next;
2346 blk64_t tot_free = 0;
2347 errcode_t retval;
2348 dgrp_t group = 0;
2349 int grp_free = 0;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002350
Theodore Ts'o6e49e6a2014-01-19 19:44:45 -05002351 num_blocks = ext2fs_blocks_count(fs->super);
2352 last_block = ext2fs_group_last_block2(fs, group);
2353 block = fs->super->s_first_data_block;
2354 while (block < num_blocks) {
2355 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
2356 block, last_block, &next);
2357 if (retval == 0)
2358 block = next;
2359 else {
2360 block = last_block + 1;
2361 goto next_bg;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002362 }
Theodore Ts'o6e49e6a2014-01-19 19:44:45 -05002363
2364 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
2365 block, last_block, &next);
2366 if (retval)
2367 next = last_block + 1;
2368 grp_free += EXT2FS_NUM_B2C(fs, next - block);
2369 tot_free += next - block;
2370 block = next;
2371
2372 if (block > last_block) {
2373 next_bg:
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002374 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2375 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002376 grp_free = 0;
2377 group++;
Theodore Ts'o6e49e6a2014-01-19 19:44:45 -05002378 last_block = ext2fs_group_last_block2(fs, group);
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002379 }
2380 }
Theodore Ts'o6e49e6a2014-01-19 19:44:45 -05002381 ext2fs_free_blocks_count_set(fs->super, tot_free);
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002382}
2383
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002384static int create_quota_inodes(ext2_filsys fs)
2385{
2386 quota_ctx_t qctx;
2387
Aditya Kalia86d55d2011-11-14 10:55:54 -05002388 quota_init_context(&qctx, fs, -1);
2389 quota_compute_usage(qctx);
Aditya Kalid678fef2011-11-14 10:55:54 -05002390 quota_write_inode(qctx, quotatype);
Aditya Kalia86d55d2011-11-14 10:55:54 -05002391 quota_release_context(&qctx);
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002392
Aditya Kalia86d55d2011-11-14 10:55:54 -05002393 return 0;
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002394}
2395
Theodore Ts'o3839e651997-04-26 13:21:57 +00002396int main (int argc, char *argv[])
2397{
2398 errcode_t retval = 0;
2399 ext2_filsys fs;
2400 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002401 unsigned int journal_blocks;
Theodore Ts'o14b283a2011-09-28 22:45:12 -04002402 unsigned int i, checkinterval;
2403 int max_mnt_count;
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002404 int val, hash_alg;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002405 int flags;
2406 int old_bitmaps;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002407 io_manager io_ptr;
Theodore Ts'o88ee0232013-12-30 23:03:09 -05002408 char opt_string[40];
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002409 char *hash_alg_str;
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002410 int itable_zeroed = 0;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002411
2412#ifdef ENABLE_NLS
2413 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05002414 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002415 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2416 textdomain(NLS_CAT_NAME);
Theodore Ts'o9d4507c2011-10-05 01:00:30 -04002417 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002418#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00002419 PRS(argc, argv);
2420
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002421#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04002422 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2423 io_ptr = test_io_manager;
2424 test_io_backing_manager = unix_io_manager;
2425 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002426#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04002427 io_ptr = unix_io_manager;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002428
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302429 if (should_do_undo(device_name)) {
2430 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2431 if (retval)
2432 exit(1);
2433 }
2434
Theodore Ts'o3839e651997-04-26 13:21:57 +00002435 /*
2436 * Initialize the superblock....
2437 */
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002438 flags = EXT2_FLAG_EXCLUSIVE;
Theodore Ts'o37c8db72012-03-22 16:00:49 -04002439 if (direct_io)
2440 flags |= EXT2_FLAG_DIRECT_IO;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002441 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2442 &old_bitmaps);
2443 if (!old_bitmaps)
2444 flags |= EXT2_FLAG_64BITS;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002445 /*
2446 * By default, we print how many inode tables or block groups
2447 * or whatever we've written so far. The quiet flag disables
2448 * this, along with a lot of other output.
2449 */
2450 if (!quiet)
2451 flags |= EXT2_FLAG_PRINT_PROGRESS;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002452 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002453 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002454 com_err(device_name, retval, "%s",
2455 _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00002456 exit(1);
2457 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002458
Ashish Sangwanfd73a1d2013-05-11 09:12:13 +05302459 /* Calculate journal blocks */
2460 if (!journal_device && ((journal_size) ||
2461 (fs_param.s_feature_compat &
2462 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2463 journal_blocks = figure_journal_size(journal_size, fs);
2464
Eric Sandeen5827d242009-10-07 16:49:17 -05002465 /* Can't undo discard ... */
Andreas Dilger8185ab92011-06-07 10:22:29 -06002466 if (!noaction && discard && (io_ptr != undo_io_manager)) {
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002467 retval = mke2fs_discard_device(fs);
Lukas Czernerd8665992010-11-18 03:38:37 +00002468 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002469 if (verbose)
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002470 printf("%s",
2471 _("Discard succeeded and will return "
2472 "0s - skipping inode table wipe\n"));
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002473 lazy_itable_init = 1;
2474 itable_zeroed = 1;
2475 }
2476 }
Eric Sandeen5827d242009-10-07 16:49:17 -05002477
Theodore Ts'o88ee0232013-12-30 23:03:09 -05002478 sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302479 32768 : fs->blocksize * 8);
Theodore Ts'o88ee0232013-12-30 23:03:09 -05002480 io_channel_set_options(fs->io, opt_string);
2481 if (offset) {
2482 sprintf(opt_string, "offset=%llu", offset);
2483 io_channel_set_options(fs->io, opt_string);
2484 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00002485
Theodore Ts'o6cb27402008-01-26 19:06:35 -05002486 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2487 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2488
Theodore Ts'ob7c5b402009-03-05 19:40:20 -05002489 if ((fs_param.s_feature_incompat &
2490 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2491 (fs_param.s_feature_ro_compat &
2492 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2493 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2494 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2495 fs->super->s_kbytes_written = 1;
2496
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002497 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00002498 * Wipe out the old on-disk superblock
2499 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04002500 if (!noaction)
2501 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00002502
2503 /*
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05002504 * Parse or generate a UUID for the filesystem
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002505 */
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05002506 if (fs_uuid) {
2507 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2508 com_err(device_name, 0, "could not parse UUID: %s\n",
2509 fs_uuid);
2510 exit(1);
2511 }
2512 } else
2513 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002514
2515 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04002516 * Initialize the directory index variables
2517 */
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002518 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2519 "half_md4");
2520 hash_alg = e2p_string2hash(hash_alg_str);
Theodore Ts'o5a2db042010-12-01 18:49:26 -05002521 free(hash_alg_str);
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002522 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2523 EXT2_HASH_HALF_MD4;
Theodore Ts'o843049c2002-09-22 15:37:40 -04002524 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2525
2526 /*
Eric Sandeen3daf5922011-02-17 15:55:15 -06002527 * Periodic checks can be enabled/disabled via config file.
2528 * Note we override the kernel include file's idea of what the default
2529 * check interval (never) should be. It's a good idea to check at
2530 * least *occasionally*, specially since servers will never rarely get
2531 * to reboot, since Linux is so robust these days. :-)
2532 *
2533 * 180 days (six months) seems like a good value.
Theodore Ts'o44c09c02001-01-14 17:02:09 +00002534 */
Eric Sandeen3daf5922011-02-17 15:55:15 -06002535#ifdef EXT2_DFL_CHECKINTERVAL
2536#undef EXT2_DFL_CHECKINTERVAL
2537#endif
2538#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2539
2540 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2541 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2542 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2543 /*
2544 * Add "jitter" to the superblock's check interval so that we
2545 * don't check all the filesystems at the same time. We use a
2546 * kludgy hack of using the UUID to derive a random jitter value
2547 */
2548 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2549 val += fs->super->s_uuid[i];
2550 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
Theodore Ts'o14b283a2011-09-28 22:45:12 -04002551 } else
2552 fs->super->s_max_mnt_count = -1;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00002553
2554 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002555 * Override the creator OS, if applicable
2556 */
2557 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002558 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002559 exit(1);
2560 }
2561
2562 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00002563 * For the Hurd, we will turn off filetype since it doesn't
2564 * support it.
2565 */
2566 if (fs->super->s_creator_os == EXT2_OS_HURD)
2567 fs->super->s_feature_incompat &=
2568 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2569
2570 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002571 * Set the volume label...
2572 */
2573 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00002574 memset(fs->super->s_volume_name, 0,
2575 sizeof(fs->super->s_volume_name));
2576 strncpy(fs->super->s_volume_name, volume_label,
2577 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002578 }
2579
2580 /*
2581 * Set the last mount directory
2582 */
2583 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00002584 memset(fs->super->s_last_mounted, 0,
2585 sizeof(fs->super->s_last_mounted));
2586 strncpy(fs->super->s_last_mounted, mount_dir,
2587 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002588 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002589
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002590 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00002591 show_stats(fs);
2592
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002593 if (noaction)
2594 exit(0);
2595
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002596 if (fs->super->s_feature_incompat &
2597 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2598 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06002599 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002600 }
2601
Theodore Ts'o3839e651997-04-26 13:21:57 +00002602 if (bad_blocks_filename)
2603 read_bb_file(fs, &bb_list, bad_blocks_filename);
2604 if (cflag)
2605 test_disk(fs, &bb_list);
2606
2607 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05002608 fs->stride = fs_stride = fs->super->s_raid_stride;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002609 if (!quiet)
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002610 printf("%s", _("Allocating group tables: "));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002611 retval = ext2fs_allocate_tables(fs);
2612 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002613 com_err(program_name, retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002614 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002615 exit(1);
2616 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002617 if (!quiet)
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002618 printf("%s", _("done \n"));
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002619
2620 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2621 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002622 com_err(program_name, retval, "%s",
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002623 _("\n\twhile converting subcluster bitmap"));
2624 exit(1);
2625 }
2626
Theodore Ts'of3db3561997-04-26 13:34:30 +00002627 if (super_only) {
2628 fs->super->s_state |= EXT2_ERROR_FS;
2629 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
Theodore Ts'oba9e0af2012-02-16 23:16:34 -05002630 /*
2631 * The command "mke2fs -S" is used to recover
2632 * corrupted file systems, so do not mark any of the
2633 * inodes as unused; we want e2fsck to consider all
2634 * inodes as potentially containing recoverable data.
2635 */
2636 if (fs->super->s_feature_ro_compat &
2637 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Theodore Ts'o30ac1ce2012-02-27 00:51:39 -05002638 for (i = 0; i < fs->group_desc_count; i++)
Theodore Ts'oba9e0af2012-02-16 23:16:34 -05002639 ext2fs_bg_itable_unused_set(fs, i, 0);
2640 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002641 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06002642 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Jose R. Santos02d6f472010-06-13 13:00:00 -04002643 blk64_t rsv = 65536 / fs->blocksize;
2644 blk64_t blocks = ext2fs_blocks_count(fs->super);
2645 blk64_t start;
2646 blk64_t ret_blk;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002647
2648#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04002649 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002650#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06002651
2652 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002653 * Wipe out any old MD RAID (or other) metadata at the end
2654 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06002655 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002656 */
Andreas Dilger59f27242001-08-30 15:39:04 -06002657 start = (blocks & ~(rsv - 1));
2658 if (start > rsv)
2659 start -= rsv;
2660 if (start > 0)
Jose R. Santos02d6f472010-06-13 13:00:00 -04002661 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302662 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06002663
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002664 if (retval) {
2665 com_err(program_name, retval,
Jose R. Santos02d6f472010-06-13 13:00:00 -04002666 _("while zeroing block %llu at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002667 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002668 }
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002669 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002670 create_root_dir(fs);
2671 create_lost_and_found(fs);
2672 reserve_inodes(fs);
2673 create_bad_block_inode(fs, bb_list);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002674 if (fs->super->s_feature_compat &
Theodore Ts'oea774312005-01-28 11:45:28 -05002675 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2676 retval = ext2fs_create_resize_inode(fs);
2677 if (retval) {
2678 com_err("ext2fs_create_resize_inode", retval,
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002679 "%s",
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002680 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05002681 exit(1);
2682 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002683 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002684 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002685
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002686 if (journal_device) {
2687 ext2_filsys jfs;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002688
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002689 if (!force)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002690 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00002691 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002692
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002693 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2694 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2695 fs->blocksize, unix_io_manager, &jfs);
2696 if (retval) {
2697 com_err(program_name, retval,
2698 _("while trying to open journal device %s\n"),
2699 journal_device);
2700 exit(1);
2701 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002702 if (!quiet) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002703 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002704 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002705 fflush(stdout);
2706 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002707 retval = ext2fs_add_journal_device(fs, jfs);
2708 if(retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002709 com_err (program_name, retval,
2710 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002711 journal_device);
2712 exit(1);
2713 }
2714 if (!quiet)
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002715 printf("%s", _("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002716 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06002717 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002718 } else if ((journal_size) ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002719 (fs_param.s_feature_compat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002720 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'oa620bad2009-03-31 07:42:24 -04002721 if (super_only) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002722 printf("%s", _("Skipping journal creation in super-only mode\n"));
Theodore Ts'oa620bad2009-03-31 07:42:24 -04002723 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2724 goto no_journal;
2725 }
2726
Theodore Ts'o93345d12001-02-17 06:09:50 +00002727 if (!journal_blocks) {
2728 fs->super->s_feature_compat &=
2729 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2730 goto no_journal;
2731 }
2732 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002733 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00002734 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002735 fflush(stdout);
2736 }
Theodore Ts'ob8182052014-01-28 12:58:56 -05002737 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
2738 journal_location,
2739 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002740 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002741 com_err(program_name, retval, "%s",
2742 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002743 exit(1);
2744 }
2745 if (!quiet)
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002746 printf("%s", _("done\n"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002747 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002748no_journal:
Andreas Dilger0f5eba72011-09-24 13:48:55 -04002749 if (!super_only &&
2750 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2751 retval = ext2fs_mmp_init(fs);
2752 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002753 fprintf(stderr, "%s",
2754 _("\nError while enabling multiple "
2755 "mount protection feature."));
Andreas Dilger0f5eba72011-09-24 13:48:55 -04002756 exit(1);
2757 }
2758 if (!quiet)
2759 printf(_("Multiple mount protection is enabled "
2760 "with update interval %d seconds.\n"),
2761 fs->super->s_mmp_update_interval);
2762 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002763
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002764 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2765 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2766 fix_cluster_bg_counts(fs);
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002767 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2768 EXT4_FEATURE_RO_COMPAT_QUOTA))
2769 create_quota_inodes(fs);
2770
Theodore Ts'o3839e651997-04-26 13:21:57 +00002771 if (!quiet)
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002772 printf("%s", _("Writing superblocks and "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002773 "filesystem accounting information: "));
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002774 checkinterval = fs->super->s_checkinterval;
2775 max_mnt_count = fs->super->s_max_mnt_count;
2776 retval = ext2fs_close(fs);
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002777 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002778 fprintf(stderr, "%s",
Theodore Ts'o66938372002-03-08 00:14:46 -05002779 _("\nWarning, had trouble writing out superblocks."));
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002780 } else if (!quiet) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -05002781 printf("%s", _("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002782 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002783 print_check_message(max_mnt_count, checkinterval);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002784 }
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002785
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002786 remove_error_table(&et_ext2_error_table);
2787 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002788 profile_release(profile);
Theodore Ts'o5a2db042010-12-01 18:49:26 -05002789 for (i=0; fs_types[i]; i++)
2790 free(fs_types[i]);
2791 free(fs_types);
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002792 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002793}