blob: 7ec8cc2f3cef6c2602030f03269bf390fb9d1d5e [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
79const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000080const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
82/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000083int cflag;
84int verbose;
85int quiet;
86int super_only;
Lukas Czerner7fe5ff32010-11-18 14:38:41 +010087int discard = 1; /* attempt to discard device before fs creation */
Theodore Ts'o37c8db72012-03-22 16:00:49 -040088int direct_io;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000089int force;
90int noaction;
91int journal_size;
92int journal_flags;
Lukas Czerner7fe5ff32010-11-18 14:38:41 +010093int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000094char *bad_blocks_filename;
95__u32 fs_stride;
Aditya Kalid678fef2011-11-14 10:55:54 -050096int quotatype = -1; /* Initialize both user and group quotas by default */
Theodore Ts'o3839e651997-04-26 13:21:57 +000097
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050098struct ext2_super_block fs_param;
Theodore Ts'ob0afdda2009-01-20 13:18:23 -050099char *fs_uuid = NULL;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000100char *creator_os;
101char *volume_label;
102char *mount_dir;
103char *journal_device;
104int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530105char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500107profile_t profile;
108
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400109int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -0400110int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400111
Theodore Ts'o63985322001-01-03 17:02:13 +0000112static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500114 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Eric Sandeen28e2cb92011-10-04 17:12:11 -0500115 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500116 "[-J journal-options]\n"
Yongqiang Yang8dbcbe12012-01-23 11:46:43 -0500117 "\t[-G flex-group-size] [-N number-of-inodes]\n"
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400118 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
119 "\t[-g blocks-per-group] [-L volume-label] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400120 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500121 "[-r fs-revision] [-E extended-option[,...]]\n"
Mike Frysinger65794cf2012-01-09 21:17:57 -0500122 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
Theodore Ts'o37c8db72012-03-22 16:00:49 -0400123 "[-jnqvDFKSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 program_name);
125 exit(1);
126}
127
Jose R. Santos02d6f472010-06-13 13:00:00 -0400128static int int_log2(unsigned long long arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129{
130 int l = 0;
131
132 arg >>= 1;
133 while (arg) {
134 l++;
135 arg >>= 1;
136 }
137 return l;
138}
139
Jose R. Santos02d6f472010-06-13 13:00:00 -0400140static int int_log10(unsigned long long arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000141{
142 int l;
143
144 for (l=0; arg ; l++)
145 arg = arg / 10;
146 return l;
147}
148
Theodore Ts'od99225e2004-09-25 07:40:12 -0400149static int parse_version_number(const char *s)
150{
151 int major, minor, rev;
152 char *endptr;
153 const char *cp = s;
154
155 if (!s)
156 return 0;
157 major = strtol(cp, &endptr, 10);
158 if (cp == endptr || *endptr != '.')
159 return 0;
160 cp = endptr + 1;
161 minor = strtol(cp, &endptr, 10);
162 if (cp == endptr || *endptr != '.')
163 return 0;
164 cp = endptr + 1;
165 rev = strtol(cp, &endptr, 10);
166 if (cp == endptr)
167 return 0;
168 return ((((major * 256) + minor) * 256) + rev);
169}
170
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000171/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 * Helper function for read_bb_file and test_disk
173 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500174static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175{
Theodore Ts'o66938372002-03-08 00:14:46 -0500176 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 return;
178}
179
180/*
181 * Reads the bad blocks list from a file
182 */
183static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
184 const char *bad_blocks_file)
185{
186 FILE *f;
187 errcode_t retval;
188
189 f = fopen(bad_blocks_file, "r");
190 if (!f) {
191 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000192 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 exit(1);
194 }
195 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
196 fclose (f);
197 if (retval) {
198 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000199 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 exit(1);
201 }
202}
203
204/*
205 * Runs the badblocks program to test the disk
206 */
207static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
208{
209 FILE *f;
210 errcode_t retval;
211 char buf[1024];
212
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400213 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500214 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400215 fs->device_name, ext2fs_blocks_count(fs->super)-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000216 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000217 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 f = popen(buf, "r");
219 if (!f) {
220 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400221 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 exit(1);
223 }
224 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000225 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 if (retval) {
227 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000228 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 exit(1);
230 }
231}
232
233static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
234{
Theodore Ts'o54434922003-12-07 01:28:50 -0500235 dgrp_t i;
236 blk_t j;
237 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 blk_t blk;
239 badblocks_iterate bb_iter;
240 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000241 blk_t group_block;
242 int group;
243 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244
245 if (!bb_list)
246 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400247
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 /*
249 * The primary superblock and group descriptors *must* be
250 * good; if not, abort.
251 */
252 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
253 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000254 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000255 fprintf(stderr, _("Block %d in primary "
256 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400257 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000258 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500260 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261 exit(1);
262 }
263 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000264
265 /*
266 * See if any of the bad blocks are showing up in the backup
267 * superblocks and/or group descriptors. If so, issue a
268 * warning and adjust the block counts appropriately.
269 */
270 group_block = fs->super->s_first_data_block +
271 fs->super->s_blocks_per_group;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400272
Theodore Ts'of3db3561997-04-26 13:34:30 +0000273 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000274 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000275 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000276 if (ext2fs_badblocks_list_test(bb_list,
277 group_block + j)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400278 if (!group_bad)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000279 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500280_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000281" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282 group_block);
283 group_bad++;
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400284 group = ext2fs_group_of_blk2(fs, group_block+j);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400285 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400286 ext2fs_group_desc_csum_set(fs, group);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400287 ext2fs_free_blocks_count_add(fs->super, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 }
289 }
290 group_block += fs->super->s_blocks_per_group;
291 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400292
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293 /*
294 * Mark all the bad blocks as used...
295 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000296 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000298 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000299 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 exit(1);
301 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400302 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -0400303 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000304 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305}
306
Eric Sandeen6fcd6f82010-08-20 16:41:14 -0500307static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000308{
309 errcode_t retval;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400310 blk64_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500311 dgrp_t i;
Theodore Ts'oe1632422010-12-20 10:42:57 -0500312 int num;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400313 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000314
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400315 ext2fs_numeric_progress_init(fs, &progress,
316 _("Writing inode tables: "),
317 fs->group_desc_count);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000318
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'oc498cb12012-09-22 21:26:48 -0400320 ext2fs_numeric_progress_update(fs, &progress, i);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400321
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400322 blk = ext2fs_inode_table_loc(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000323 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000324
Theodore Ts'oe1632422010-12-20 10:42:57 -0500325 if (lazy_flag)
326 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
327 ext2fs_bg_itable_unused(fs, i)) *
328 EXT2_INODE_SIZE(fs->super),
329 EXT2_BLOCK_SIZE(fs->super));
Eric Sandeen6fcd6f82010-08-20 16:41:14 -0500330 if (!lazy_flag || itable_zeroed) {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500331 /* The kernel doesn't need to zero the itable blocks */
Eric Sandeene633b582009-10-25 21:41:32 -0400332 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400333 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000334 }
Jose R. Santos02d6f472010-06-13 13:00:00 -0400335 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400336 if (retval) {
337 fprintf(stderr, _("\nCould not write %d "
Jose R. Santos02d6f472010-06-13 13:00:00 -0400338 "blocks in inode table starting at %llu: %s\n"),
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400339 num, blk, error_message(retval));
340 exit(1);
341 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000342 if (sync_kludge) {
343 if (sync_kludge == 1)
344 sync();
345 else if ((i % sync_kludge) == 0)
346 sync();
347 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 }
Jose R. Santos02d6f472010-06-13 13:00:00 -0400349 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400350 ext2fs_numeric_progress_close(fs, &progress,
351 _("done \n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352}
353
354static void create_root_dir(ext2_filsys fs)
355{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400356 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000357 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400358 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359
360 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
361 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000362 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000363 exit(1);
364 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000365 if (geteuid()) {
366 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
367 if (retval) {
368 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000369 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000370 exit(1);
371 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400372 uid = getuid();
373 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500374 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400375 if (uid) {
376 gid = getgid();
377 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500378 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400379 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500380 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000381 if (retval) {
382 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000383 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000384 exit(1);
385 }
386 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387}
388
389static void create_lost_and_found(ext2_filsys fs)
390{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400391 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000393 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 const char *name = "lost+found";
395 int i;
396
Theodore Ts'o6a525062001-12-24 09:40:00 -0500397 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000398 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
399 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000400 com_err("ext2fs_mkdir", retval,
401 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402 exit(1);
403 }
404
405 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
406 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000407 com_err("ext2_lookup", retval,
408 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409 exit(1);
410 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400411
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100413 /* Ensure that lost+found is at least 2 blocks, so we always
414 * test large empty blocks for big-block filesystems. */
415 if ((lpf_size += fs->blocksize) >= 16*1024 &&
416 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000417 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 retval = ext2fs_expand_dir(fs, ino);
419 if (retval) {
420 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000421 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 exit(1);
423 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500424 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425}
426
427static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
428{
429 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400430
Valerie Aurora Henson463e7322009-08-05 00:17:56 -0400431 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400432 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433 retval = ext2fs_update_bb_inode(fs, bb_list);
434 if (retval) {
435 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000436 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 exit(1);
438 }
439
440}
441
442static void reserve_inodes(ext2_filsys fs)
443{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000444 ext2_ino_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400446 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
447 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 ext2fs_mark_ib_dirty(fs);
449}
450
Theodore Ts'o756df352002-03-07 20:52:12 -0500451#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500452#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500453#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500454
Theodore Ts'o04a96852001-08-30 21:55:26 -0400455static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000456{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400457 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000458 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500459 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000460
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400461 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600462 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500463 printf(_("Out of memory erasing sectors %d-%d\n"),
464 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600465 exit(1);
466 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500467
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500468 if (sect == 0) {
469 /* Check for a BSD disklabel, and don't erase it if so */
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400470 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500471 if (retval)
472 fprintf(stderr,
473 _("Warning: could not read block 0: %s\n"),
474 error_message(retval));
475 else {
476 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
477 if ((*magic == BSD_DISKMAGIC) ||
478 (*magic == BSD_MAGICDISK))
479 return;
480 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500481 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500482
Theodore Ts'o70988102002-07-14 08:00:00 -0400483 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000484 io_channel_set_blksize(fs->io, 512);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400485 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000486 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400487 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000488 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500489 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
490 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000491}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000492
493static void create_journal_dev(ext2_filsys fs)
494{
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400495 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000496 errcode_t retval;
497 char *buf;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400498 blk64_t blk, err_blk;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530499 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000500
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000501 retval = ext2fs_create_journal_superblock(fs,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400502 ext2fs_blocks_count(fs->super), 0, &buf);
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000503 if (retval) {
504 com_err("create_journal_dev", retval,
505 _("while initializing journal superblock"));
506 exit(1);
507 }
Andreas Dilger6c546892011-06-11 12:17:29 -0400508
509 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
510 goto write_superblock;
511
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400512 ext2fs_numeric_progress_init(fs, &progress,
513 _("Zeroing journal device: "),
514 ext2fs_blocks_count(fs->super));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530515 blk = 0;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400516 count = ext2fs_blocks_count(fs->super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530517 while (count > 0) {
518 if (count > 1024)
519 c = 1024;
520 else
521 c = count;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400522 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530523 if (retval) {
524 com_err("create_journal_dev", retval,
525 _("while zeroing journal device "
Jose R. Santos02d6f472010-06-13 13:00:00 -0400526 "(block %llu, count %d)"),
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530527 err_blk, err_count);
528 exit(1);
529 }
530 blk += c;
531 count -= c;
Theodore Ts'oc498cb12012-09-22 21:26:48 -0400532 ext2fs_numeric_progress_update(fs, &progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000533 }
Jose R. Santos02d6f472010-06-13 13:00:00 -0400534 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000535
Andreas Dilger6c546892011-06-11 12:17:29 -0400536 ext2fs_numeric_progress_close(fs, &progress, NULL);
537write_superblock:
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400538 retval = io_channel_write_blk64(fs->io,
539 fs->super->s_first_data_block+1,
540 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000541 if (retval) {
542 com_err("create_journal_dev", retval,
543 _("while writing journal superblock"));
544 exit(1);
545 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000546}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000547
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548static void show_stats(ext2_filsys fs)
549{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000550 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000551 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500552 char *os;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400553 blk64_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500554 dgrp_t i;
555 int need, col_left;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400556
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400557 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
558 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
559 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000560
561 memset(buf, 0, sizeof(buf));
562 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000563 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o74e12112010-12-21 18:12:12 -0500564 os = e2p_os2string(fs->super->s_creator_os);
565 if (os)
566 printf(_("OS type: %s\n"), os);
Theodore Ts'o63253942005-03-19 01:13:22 -0500567 free(os);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000568 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000569 s->s_log_block_size);
Theodore Ts'o412376e2011-02-25 21:43:54 -0500570 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
571 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
572 printf(_("Cluster size=%u (log=%u)\n"),
Theodore Ts'o1da5ef72011-06-04 10:20:47 -0400573 fs->blocksize << fs->cluster_ratio_bits,
574 s->s_log_cluster_size);
Theodore Ts'o412376e2011-02-25 21:43:54 -0500575 else
Theodore Ts'o1da5ef72011-06-04 10:20:47 -0400576 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
Theodore Ts'o412376e2011-02-25 21:43:54 -0500577 s->s_log_cluster_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -0500578 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
579 s->s_raid_stride, s->s_raid_stripe_width);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400580 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
581 ext2fs_blocks_count(s));
582 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
583 ext2fs_r_blocks_count(s),
584 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000585 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500586 if (s->s_reserved_gdt_blocks)
587 printf(_("Maximum filesystem blocks=%lu\n"),
588 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200589 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000590 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000591 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000592 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000593 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'o412376e2011-02-25 21:43:54 -0500594 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
595 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
596 printf(_("%u blocks per group, %u clusters per group\n"),
597 s->s_blocks_per_group, s->s_clusters_per_group);
598 else
599 printf(_("%u blocks per group, %u fragments per group\n"),
600 s->s_blocks_per_group, s->s_clusters_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000601 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602
603 if (fs->group_desc_count == 1) {
604 printf("\n");
605 return;
606 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500607
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000608 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 group_block = s->s_first_data_block;
610 col_left = 0;
611 for (i = 1; i < fs->group_desc_count; i++) {
612 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000613 if (!ext2fs_bg_has_super(fs, i))
614 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000615 if (i != 1)
616 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000617 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000618 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000620 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000622 col_left -= need;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400623 printf("%llu", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 }
625 printf("\n\n");
626}
627
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000628/*
629 * Set the S_CREATOR_OS field. Return true if OS is known,
630 * otherwise, 0.
631 */
632static int set_os(struct ext2_super_block *sb, char *os)
633{
634 if (isdigit (*os))
635 sb->s_creator_os = atoi (os);
636 else if (strcasecmp(os, "linux") == 0)
637 sb->s_creator_os = EXT2_OS_LINUX;
638 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
639 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500640 else if (strcasecmp(os, "freebsd") == 0)
641 sb->s_creator_os = EXT2_OS_FREEBSD;
642 else if (strcasecmp(os, "lites") == 0)
643 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000644 else
645 return 0;
646 return 1;
647}
648
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000649#define PATH_SET "PATH=/sbin"
650
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400651static void parse_extended_opts(struct ext2_super_block *param,
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500652 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000653{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400654 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000655 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500656 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000657
658 len = strlen(opts);
659 buf = malloc(len+1);
660 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500661 fprintf(stderr,
662 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000663 exit(1);
664 }
665 strcpy(buf, opts);
666 for (token = buf; token && *token; token = next) {
667 p = strchr(token, ',');
668 next = 0;
669 if (p) {
670 *p = 0;
671 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500672 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000673 arg = strchr(token, '=');
674 if (arg) {
675 *arg = 0;
676 arg++;
677 }
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400678 if (strcmp(token, "mmp_update_interval") == 0) {
679 if (!arg) {
680 r_usage++;
681 badopt = token;
682 continue;
683 }
684 param->s_mmp_update_interval = strtoul(arg, &p, 0);
685 if (*p) {
686 fprintf(stderr,
687 _("Invalid mmp_update_interval: %s\n"),
688 arg);
689 r_usage++;
690 continue;
691 }
692 } else if (strcmp(token, "stride") == 0) {
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000693 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500694 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500695 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000696 continue;
697 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500698 param->s_raid_stride = strtoul(arg, &p, 0);
Theodore Ts'o5b734a02011-07-04 20:22:19 -0400699 if (*p) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000700 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400701 _("Invalid stride parameter: %s\n"),
702 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500703 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000704 continue;
705 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500706 } else if (strcmp(token, "stripe-width") == 0 ||
707 strcmp(token, "stripe_width") == 0) {
708 if (!arg) {
709 r_usage++;
710 badopt = token;
711 continue;
712 }
713 param->s_raid_stripe_width = strtoul(arg, &p, 0);
Theodore Ts'o5b734a02011-07-04 20:22:19 -0400714 if (*p) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500715 fprintf(stderr,
716 _("Invalid stripe-width parameter: %s\n"),
717 arg);
718 r_usage++;
719 continue;
720 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500721 } else if (!strcmp(token, "resize")) {
Jose R. Santos02d6f472010-06-13 13:00:00 -0400722 blk64_t resize;
723 unsigned long bpg, rsv_groups;
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500724 unsigned long group_desc_count, desc_blocks;
725 unsigned int gdpb, blocksize;
726 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500727
728 if (!arg) {
729 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500730 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500731 continue;
732 }
733
Jose R. Santos02d6f472010-06-13 13:00:00 -0400734 resize = parse_num_blocks2(arg,
735 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500736
737 if (resize == 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400738 fprintf(stderr,
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500739 _("Invalid resize parameter: %s\n"),
740 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500741 r_usage++;
742 continue;
743 }
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400744 if (resize <= ext2fs_blocks_count(param)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400745 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400746 _("The resize maximum must be greater "
747 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500748 r_usage++;
749 continue;
750 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500751
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500752 blocksize = EXT2_BLOCK_SIZE(param);
753 bpg = param->s_blocks_per_group;
754 if (!bpg)
755 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200756 gdpb = EXT2_DESC_PER_BLOCK(param);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400757 group_desc_count = (__u32) ext2fs_div64_ceil(
758 ext2fs_blocks_count(param), bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500759 desc_blocks = (group_desc_count +
760 gdpb - 1) / gdpb;
Jose R. Santos02d6f472010-06-13 13:00:00 -0400761 rsv_groups = ext2fs_div64_ceil(resize, bpg);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400762 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500763 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500764 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500765 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
766
767 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400768 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400769 fprintf(stderr,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400770 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400771 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400772 exit(1);
773 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500774 param->s_feature_compat |=
775 EXT2_FEATURE_COMPAT_RESIZE_INODE;
776
777 param->s_reserved_gdt_blocks = rsv_gdb;
778 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500779 } else if (!strcmp(token, "test_fs")) {
780 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400781 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400782 if (arg)
783 lazy_itable_init = strtoul(arg, &p, 0);
784 else
785 lazy_itable_init = 1;
Andreas Dilger6c546892011-06-11 12:17:29 -0400786 } else if (!strcmp(token, "lazy_journal_init")) {
787 if (arg)
788 journal_flags |= strtoul(arg, &p, 0) ?
789 EXT2_MKJOURNAL_LAZYINIT : 0;
790 else
791 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
Lukas Czerner0bc85df2010-11-18 14:38:39 +0100792 } else if (!strcmp(token, "discard")) {
793 discard = 1;
794 } else if (!strcmp(token, "nodiscard")) {
795 discard = 0;
Aditya Kalid678fef2011-11-14 10:55:54 -0500796 } else if (!strcmp(token, "quotatype")) {
797 if (!arg) {
798 r_usage++;
799 badopt = token;
800 continue;
801 }
802 if (!strncmp(arg, "usr", 3)) {
803 quotatype = 0;
804 } else if (!strncmp(arg, "grp", 3)) {
805 quotatype = 1;
806 } else {
807 fprintf(stderr,
808 _("Invalid quotatype parameter: %s\n"),
809 arg);
810 r_usage++;
811 continue;
812 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500813 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500814 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500815 badopt = token;
816 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000817 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500818 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500819 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400820 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000821 "and may take an argument which\n"
822 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400823 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500824 "\tstride=<RAID per-disk data chunk in blocks>\n"
825 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400826 "\tresize=<resize maximum size in blocks>\n"
827 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
Andreas Dilger6c546892011-06-11 12:17:29 -0400828 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
Lukas Czerner0bc85df2010-11-18 14:38:39 +0100829 "\ttest_fs\n"
830 "\tdiscard\n"
Aditya Kalid678fef2011-11-14 10:55:54 -0500831 "\tnodiscard\n"
832 "\tquotatype=<usr OR grp>\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400833 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400834 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000835 exit(1);
836 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500837 if (param->s_raid_stride &&
838 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
839 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
840 "multiple of stride %u.\n\n"),
841 param->s_raid_stripe_width, param->s_raid_stride);
842
Brian Behlendorf21400382007-05-31 11:30:47 -0400843 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400844}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000845
Theodore Ts'o896938d1999-10-23 01:04:50 +0000846static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500847 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400848 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500849 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400850 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500851 EXT2_FEATURE_COMPAT_EXT_ATTR,
852 /* Incompat */
853 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500854 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400855 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500856 EXT2_FEATURE_INCOMPAT_META_BG|
Jose R. Santos02d6f472010-06-13 13:00:00 -0400857 EXT4_FEATURE_INCOMPAT_FLEX_BG|
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400858 EXT4_FEATURE_INCOMPAT_MMP |
Jose R. Santos02d6f472010-06-13 13:00:00 -0400859 EXT4_FEATURE_INCOMPAT_64BIT,
Theodore Ts'o558df542008-02-27 15:01:19 -0500860 /* R/O compat */
861 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400862 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
863 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
864 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500865 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -0400866 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
Aditya Kali1f5d7a82011-07-20 11:40:04 -0700867 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
Theodore Ts'o7becb202011-11-14 10:40:43 -0500868#ifdef CONFIG_QUOTA
869 EXT4_FEATURE_RO_COMPAT_QUOTA|
870#endif
871 0
Theodore Ts'o896938d1999-10-23 01:04:50 +0000872};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000873
874
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500875static void syntax_err_report(const char *filename, long err, int line_num)
876{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400877 fprintf(stderr,
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500878 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
879 filename, line_num, error_message(err));
880 exit(1);
881}
882
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200883static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500884
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400885static void edit_feature(const char *str, __u32 *compat_array)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500886{
887 if (!str)
888 return;
889
890 if (e2p_edit_feature(str, compat_array, ok_features)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400891 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500892 str);
893 exit(1);
894 }
895}
896
Eric Sandeen6a426c92011-02-17 15:56:17 -0600897static void edit_mntopts(const char *str, __u32 *mntopts)
898{
899 if (!str)
900 return;
901
902 if (e2p_edit_mntopts(str, mntopts, ~0)) {
903 fprintf(stderr, _("Invalid mount option set: %s\n"),
904 str);
905 exit(1);
906 }
907}
908
Theodore Ts'o3d438362008-02-19 08:32:58 -0500909struct str_list {
910 char **list;
911 int num;
912 int max;
913};
914
915static errcode_t init_list(struct str_list *sl)
916{
917 sl->num = 0;
918 sl->max = 0;
919 sl->list = malloc((sl->max+1) * sizeof(char *));
920 if (!sl->list)
921 return ENOMEM;
922 sl->list[0] = 0;
923 return 0;
924}
925
926static errcode_t push_string(struct str_list *sl, const char *str)
927{
928 char **new_list;
929
930 if (sl->num >= sl->max) {
931 sl->max += 2;
932 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
933 if (!new_list)
934 return ENOMEM;
935 sl->list = new_list;
936 }
937 sl->list[sl->num] = malloc(strlen(str)+1);
938 if (sl->list[sl->num] == 0)
939 return ENOMEM;
940 strcpy(sl->list[sl->num], str);
941 sl->num++;
942 sl->list[sl->num] = 0;
943 return 0;
944}
945
946static void print_str_list(char **list)
947{
948 char **cpp;
949
950 for (cpp = list; *cpp; cpp++) {
951 printf("'%s'", *cpp);
952 if (cpp[1])
953 fputs(", ", stdout);
954 }
955 fputc('\n', stdout);
956}
957
Theodore Ts'o2ee45442010-12-01 18:28:35 -0500958/*
959 * Return TRUE if the profile has the given subsection
960 */
961static int profile_has_subsection(profile_t profile, const char *section,
962 const char *subsection)
963{
964 void *state;
965 const char *names[4];
966 char *name;
967 int ret = 0;
968
969 names[0] = section;
970 names[1] = subsection;
971 names[2] = 0;
972
973 if (profile_iterator_create(profile, names,
974 PROFILE_ITER_LIST_SECTION |
975 PROFILE_ITER_RELATIONS_ONLY, &state))
976 return 0;
977
978 if ((profile_iterator(&state, &name, 0) == 0) && name) {
979 free(name);
980 ret = 1;
981 }
982
983 profile_iterator_free(&state);
984 return ret;
985}
986
Theodore Ts'o3d438362008-02-19 08:32:58 -0500987static char **parse_fs_type(const char *fs_type,
988 const char *usage_types,
989 struct ext2_super_block *fs_param,
Theodore Ts'o493024e2010-06-13 14:00:00 -0400990 blk64_t fs_blocks_count,
Theodore Ts'o3d438362008-02-19 08:32:58 -0500991 char *progname)
992{
993 const char *ext_type = 0;
994 char *parse_str;
995 char *profile_type = 0;
996 char *cp, *t;
997 const char *size_type;
998 struct str_list list;
Theodore Ts'o493024e2010-06-13 14:00:00 -0400999 unsigned long long meg;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001000 int is_hurd = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001001
1002 if (init_list(&list))
1003 return 0;
1004
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001005 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1006 !strcasecmp(creator_os, "hurd")))
1007 is_hurd = 1;
1008
Theodore Ts'o3d438362008-02-19 08:32:58 -05001009 if (fs_type)
1010 ext_type = fs_type;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001011 else if (is_hurd)
1012 ext_type = "ext2";
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001013 else if (!strcmp(program_name, "mke3fs"))
1014 ext_type = "ext3";
Eric Sandeen237b7b22012-02-15 15:11:35 -05001015 else if (!strcmp(program_name, "mke4fs"))
1016 ext_type = "ext4";
Theodore Ts'o3d438362008-02-19 08:32:58 -05001017 else if (progname) {
1018 ext_type = strrchr(progname, '/');
1019 if (ext_type)
1020 ext_type++;
1021 else
1022 ext_type = progname;
1023
1024 if (!strncmp(ext_type, "mkfs.", 5)) {
1025 ext_type += 5;
1026 if (ext_type[0] == 0)
1027 ext_type = 0;
1028 } else
1029 ext_type = 0;
1030 }
1031
1032 if (!ext_type) {
1033 profile_get_string(profile, "defaults", "fs_type", 0,
1034 "ext2", &profile_type);
1035 ext_type = profile_type;
1036 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1037 ext_type = "ext3";
1038 }
1039
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001040
1041 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1042 strcmp(ext_type, "ext2")) {
1043 printf(_("\nYour mke2fs.conf file does not define the "
1044 "%s filesystem type.\n"), ext_type);
1045 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1046 !strcmp(ext_type, "ext4dev")) {
Theodore Ts'obad89b22008-08-22 21:57:29 -04001047 printf(_("You probably need to install an updated "
1048 "mke2fs.conf file.\n\n"));
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001049 }
1050 if (!force) {
1051 printf(_("Aborting...\n"));
1052 exit(1);
Theodore Ts'obad89b22008-08-22 21:57:29 -04001053 }
1054 }
1055
Theodore Ts'o3d438362008-02-19 08:32:58 -05001056 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
Theodore Ts'o493024e2010-06-13 14:00:00 -04001057 if (fs_blocks_count < 3 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001058 size_type = "floppy";
Theodore Ts'o493024e2010-06-13 14:00:00 -04001059 else if (fs_blocks_count < 512 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001060 size_type = "small";
Namhyung Kim22d8ce52010-11-29 17:55:13 +09001061 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001062 size_type = "default";
Namhyung Kim22d8ce52010-11-29 17:55:13 +09001063 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1064 size_type = "big";
1065 else
1066 size_type = "huge";
Theodore Ts'o3d438362008-02-19 08:32:58 -05001067
1068 if (!usage_types)
1069 usage_types = size_type;
1070
Eric Sandeen4d5cf8b2011-09-16 15:49:19 -05001071 parse_str = malloc(strlen(usage_types)+1);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001072 if (!parse_str) {
1073 free(list.list);
1074 return 0;
1075 }
Eric Sandeen4d5cf8b2011-09-16 15:49:19 -05001076 strcpy(parse_str, usage_types);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001077
1078 if (ext_type)
1079 push_string(&list, ext_type);
1080 cp = parse_str;
1081 while (1) {
1082 t = strchr(cp, ',');
1083 if (t)
1084 *t = '\0';
1085
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001086 if (*cp) {
Theodore Ts'o0f7479b2010-12-22 18:31:36 -05001087 if (profile_has_subsection(profile, "fs_types", cp))
1088 push_string(&list, cp);
1089 else if (strcmp(cp, "default") != 0)
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001090 fprintf(stderr,
1091 _("\nWarning: the fs_type %s is not "
Theodore Ts'o0f7479b2010-12-22 18:31:36 -05001092 "defined in mke2fs.conf\n\n"),
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001093 cp);
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001094 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001095 if (t)
1096 cp = t+1;
1097 else {
1098 cp = "";
1099 break;
1100 }
1101 }
1102 free(parse_str);
Jim Meyering45e338f2009-02-23 18:07:50 +01001103 free(profile_type);
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001104 if (is_hurd)
1105 push_string(&list, "hurd");
Theodore Ts'o3d438362008-02-19 08:32:58 -05001106 return (list.list);
1107}
1108
1109static char *get_string_from_profile(char **fs_types, const char *opt,
1110 const char *def_val)
1111{
1112 char *ret = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001113 int i;
1114
1115 for (i=0; fs_types[i]; i++);
1116 for (i-=1; i >=0 ; i--) {
1117 profile_get_string(profile, "fs_types", fs_types[i],
1118 opt, 0, &ret);
1119 if (ret)
1120 return ret;
1121 }
1122 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1123 return (ret);
1124}
1125
1126static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1127{
1128 int ret;
1129 char **cpp;
1130
1131 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1132 for (cpp = fs_types; *cpp; cpp++)
1133 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1134 return ret;
1135}
1136
Aditya Kalid3859af2011-05-10 14:51:31 -07001137static double get_double_from_profile(char **fs_types, const char *opt,
1138 double def_val)
1139{
1140 double ret;
1141 char **cpp;
1142
1143 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1144 for (cpp = fs_types; *cpp; cpp++)
1145 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1146 return ret;
1147}
1148
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001149static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1150{
1151 int ret;
1152 char **cpp;
1153
1154 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1155 for (cpp = fs_types; *cpp; cpp++)
1156 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1157 return ret;
1158}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001159
Theodore Ts'od48bc602007-07-04 14:10:46 -04001160extern const char *mke2fs_default_profile;
1161static const char *default_files[] = { "<default>", 0 };
1162
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001163#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1164/*
1165 * Sets the geometry of a device (stripe/stride), and returns the
1166 * device's alignment offset, if any, or a negative error.
1167 */
Theodore Ts'o1599b472010-11-22 10:50:42 -05001168static int get_device_geometry(const char *file,
1169 struct ext2_super_block *fs_param,
1170 int psector_size)
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001171{
1172 int rc = -1;
1173 int blocksize;
1174 blkid_probe pr;
1175 blkid_topology tp;
1176 unsigned long min_io;
1177 unsigned long opt_io;
Eric Sandeen13b0b122010-01-23 21:50:45 -06001178 struct stat statbuf;
1179
1180 /* Nothing to do for a regular file */
1181 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1182 return 0;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001183
1184 pr = blkid_new_probe_from_filename(file);
1185 if (!pr)
1186 goto out;
1187
1188 tp = blkid_probe_get_topology(pr);
1189 if (!tp)
1190 goto out;
1191
1192 min_io = blkid_topology_get_minimum_io_size(tp);
1193 opt_io = blkid_topology_get_optimal_io_size(tp);
1194 blocksize = EXT2_BLOCK_SIZE(fs_param);
Theodore Ts'o1599b472010-11-22 10:50:42 -05001195 if ((min_io == 0) && (psector_size > blocksize))
1196 min_io = psector_size;
1197 if ((opt_io == 0) && min_io)
1198 opt_io = min_io;
1199 if ((opt_io == 0) && (psector_size > blocksize))
1200 opt_io = psector_size;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001201
Eric Sandeend5687822011-04-04 15:11:52 -04001202 /* setting stripe/stride to blocksize is pointless */
1203 if (min_io > blocksize)
1204 fs_param->s_raid_stride = min_io / blocksize;
1205 if (opt_io > blocksize)
1206 fs_param->s_raid_stripe_width = opt_io / blocksize;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001207
1208 rc = blkid_topology_get_alignment_offset(tp);
1209out:
1210 blkid_free_probe(pr);
1211 return rc;
1212}
1213#endif
1214
Theodore Ts'o3839e651997-04-26 13:21:57 +00001215static void PRS(int argc, char *argv[])
1216{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001217 int b, c;
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001218 int cluster_size = 0;
Eric Sandeen79e62402008-07-06 18:36:56 -04001219 char *tmp, **cpp;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001220 int blocksize = 0;
1221 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001222 int inode_size = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001223 unsigned long flex_bg_size = 0;
Aditya Kalid3859af2011-05-10 14:51:31 -07001224 double reserved_ratio = -1.0;
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001225 int lsector_size = 0, psector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001226 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001227 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001228 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001229 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001230 char * extended_opts = 0;
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001231 char * fs_type = 0;
1232 char * usage_types = 0;
Jose R. Santos02d6f472010-06-13 13:00:00 -04001233 blk64_t dev_size;
Theodore Ts'o493024e2010-06-13 14:00:00 -04001234 blk64_t fs_blocks_count = 0;
Theodore Ts'o756df352002-03-07 20:52:12 -05001235#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001236 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001237#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001238 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001239 int s_opt = -1, r_opt = -1;
1240 char *fs_features = 0;
1241 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001242 char *newpath;
1243 int pathlen = sizeof(PATH_SET) + 1;
1244
1245 if (oldpath)
1246 pathlen += strlen(oldpath);
1247 newpath = malloc(pathlen);
Namhyung Kim9ec68af2010-12-16 18:40:40 +09001248 if (!newpath) {
1249 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1250 exit(1);
1251 }
Theodore Ts'o642935c2006-11-14 23:38:17 -05001252 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001253
Theodore Ts'o3839e651997-04-26 13:21:57 +00001254 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001255 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001256 strcat (newpath, ":");
1257 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001258 }
1259 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001260
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001261 tmp = getenv("MKE2FS_SYNC");
1262 if (tmp)
1263 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001264
1265 /* Determine the system page size if possible */
1266#ifdef HAVE_SYSCONF
1267#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1268#define _SC_PAGESIZE _SC_PAGE_SIZE
1269#endif
1270#ifdef _SC_PAGESIZE
1271 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001272 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001273 sys_page_size = sysval;
1274#endif /* _SC_PAGESIZE */
1275#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001276
1277 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1278 config_fn[0] = tmp;
1279 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001280 retval = profile_init(config_fn, &profile);
1281 if (retval == ENOENT) {
Namhyung Kim9ec68af2010-12-16 18:40:40 +09001282 retval = profile_init(default_files, &profile);
1283 if (retval)
1284 goto profile_error;
1285 retval = profile_set_default(profile, mke2fs_default_profile);
1286 if (retval)
1287 goto profile_error;
1288 } else if (retval) {
1289profile_error:
1290 fprintf(stderr, _("Couldn't init profile successfully"
1291 " (error: %ld).\n"), retval);
1292 exit(1);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001293 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001294
Theodore Ts'o3839e651997-04-26 13:21:57 +00001295 setbuf(stdout, NULL);
1296 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001297 add_error_table(&et_ext2_error_table);
1298 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001299 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1300 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001301
Theodore Ts'o756df352002-03-07 20:52:12 -05001302#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001303 if (uname(&ut)) {
1304 perror("uname");
1305 exit(1);
1306 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001307 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001308 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001309 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001310#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001311
1312 if (argc && *argv) {
1313 program_name = get_progname(*argv);
1314
1315 /* If called as mkfs.ext3, create a journal inode */
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001316 if (!strcmp(program_name, "mkfs.ext3") ||
1317 !strcmp(program_name, "mke3fs"))
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001318 journal_size = -1;
1319 }
1320
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001321 while ((c = getopt (argc, argv,
Theodore Ts'o37c8db72012-03-22 16:00:49 -04001322 "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 +00001323 switch (c) {
1324 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001325 blocksize = strtol(optarg, &tmp, 0);
1326 b = (blocksize > 0) ? blocksize : -blocksize;
1327 if (b < EXT2_MIN_BLOCK_SIZE ||
1328 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001329 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001330 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001331 exit(1);
1332 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001333 if (blocksize > 4096)
1334 fprintf(stderr, _("Warning: blocksize %d not "
1335 "usable on most systems.\n"),
1336 blocksize);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001337 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001338 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001339 int_log2(blocksize >>
1340 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001341 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001342 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001343 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001344 break;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001345 case 'C':
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001346 cluster_size = strtoul(optarg, &tmp, 0);
1347 if (cluster_size < EXT2_MIN_CLUSTER_SIZE ||
1348 cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001349 com_err(program_name, 0,
Eric Sandeen28e2cb92011-10-04 17:12:11 -05001350 _("invalid cluster size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001351 optarg);
1352 exit(1);
1353 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001354 break;
Theodore Ts'o37c8db72012-03-22 16:00:49 -04001355 case 'D':
1356 direct_io = 1;
1357 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001358 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001359 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001360 if (*tmp) {
1361 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001362 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001363 exit(1);
1364 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001365 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001366 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001367 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001368 exit(1);
1369 }
1370 break;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001371 case 'G':
1372 flex_bg_size = strtoul(optarg, &tmp, 0);
1373 if (*tmp) {
1374 com_err(program_name, 0,
1375 _("Illegal number for flex_bg size"));
1376 exit(1);
1377 }
Theodore Ts'od5314502010-05-10 21:49:58 -04001378 if (flex_bg_size < 1 ||
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001379 (flex_bg_size & (flex_bg_size-1)) != 0) {
1380 com_err(program_name, 0,
1381 _("flex_bg size must be a power of 2"));
1382 exit(1);
1383 }
1384 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001385 case 'i':
1386 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001387 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1388 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001389 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001390 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001391 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001392 optarg, EXT2_MIN_BLOCK_SIZE,
Tim Smalle447ba32010-07-30 20:43:11 -04001393 EXT2_MAX_BLOCK_SIZE * 1024);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001394 exit(1);
1395 }
1396 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001397 case 'J':
1398 parse_journal_opts(optarg);
1399 break;
Eric Sandeen5827d242009-10-07 16:49:17 -05001400 case 'K':
Lukas Czerner0bc85df2010-11-18 14:38:39 +01001401 fprintf(stderr, _("Warning: -K option is deprecated and "
1402 "should not be used anymore. Use "
1403 "\'-E nodiscard\' extended option "
1404 "instead!\n"));
Eric Sandeen5827d242009-10-07 16:49:17 -05001405 discard = 0;
1406 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001407 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001408 if (!journal_size)
1409 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001410 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001411 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001412 bad_blocks_filename = malloc(strlen(optarg)+1);
1413 if (!bad_blocks_filename) {
1414 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001415 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001416 exit(1);
1417 }
1418 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001419 break;
1420 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001421 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o8d822452009-03-06 02:23:59 -05001422 if ( *tmp || reserved_ratio > 50 ||
1423 reserved_ratio < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001424 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001425 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001426 optarg);
1427 exit(1);
1428 }
1429 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001430 case 'n':
1431 noaction++;
1432 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001433 case 'o':
1434 creator_os = optarg;
1435 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001436 case 'q':
1437 quiet = 1;
1438 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001439 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001440 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001441 if (*tmp) {
1442 com_err(program_name, 0,
1443 _("bad revision level - %s"), optarg);
1444 exit(1);
1445 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001446 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001447 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001448 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001449 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001450 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001451 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001452 inode_size = strtoul(optarg, &tmp, 0);
1453 if (*tmp) {
1454 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001455 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001456 exit(1);
1457 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001458 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001459 case 'v':
1460 verbose = 1;
1461 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001462 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001463 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001464 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001465 case 'L':
1466 volume_label = optarg;
1467 break;
1468 case 'M':
1469 mount_dir = optarg;
1470 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001471 case 'N':
1472 num_inodes = strtoul(optarg, &tmp, 0);
1473 if (*tmp) {
1474 com_err(program_name, 0,
1475 _("bad num inodes - %s"), optarg);
1476 exit(1);
1477 }
1478 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001479 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001480 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001481 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001482 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001483 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001484 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001485 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001486 case 'S':
1487 super_only = 1;
1488 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001489 case 't':
Eric Sandeen9f7c3af2011-09-16 15:49:32 -05001490 if (fs_type) {
1491 com_err(program_name, 0,
1492 _("The -t option may only be used once"));
1493 exit(1);
1494 }
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001495 fs_type = strdup(optarg);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001496 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001497 case 'T':
Eric Sandeen9f7c3af2011-09-16 15:49:32 -05001498 if (usage_types) {
1499 com_err(program_name, 0,
1500 _("The -T option may only be used once"));
1501 exit(1);
1502 }
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001503 usage_types = strdup(optarg);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001504 break;
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001505 case 'U':
1506 fs_uuid = optarg;
1507 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001508 case 'V':
1509 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001510 show_version_only++;
1511 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001512 default:
1513 usage();
1514 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001515 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001516 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001517 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001518 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001519
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001520 if (!quiet || show_version_only)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001521 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
Theodore Ts'o38798572003-04-16 15:29:39 -04001522 E2FSPROGS_DATE);
1523
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001524 if (show_version_only) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001525 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001526 error_message(EXT2_ET_BASE));
1527 exit(0);
1528 }
1529
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001530 /*
1531 * If there's no blocksize specified and there is a journal
1532 * device, use it to figure out the blocksize
1533 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001534 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001535 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001536 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001537
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001538#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001539 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1540 io_ptr = test_io_manager;
1541 test_io_backing_manager = unix_io_manager;
1542 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001543#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001544 io_ptr = unix_io_manager;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001545 retval = ext2fs_open(journal_device,
1546 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001547 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001548 if (retval) {
1549 com_err(program_name, retval,
1550 _("while trying to open journal device %s\n"),
1551 journal_device);
1552 exit(1);
1553 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001554 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001555 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001556 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001557 "minimum blocksize %d\n"), jfs->blocksize,
1558 -blocksize);
1559 exit(1);
1560 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001561 blocksize = jfs->blocksize;
Theodore Ts'of8df04b2008-07-06 20:57:17 -04001562 printf(_("Using journal device's blocksize: %d\n"), blocksize);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001563 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001564 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1565 ext2fs_close(jfs);
1566 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001567
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001568 if (optind < argc) {
Theodore Ts'o493024e2010-06-13 14:00:00 -04001569 fs_blocks_count = parse_num_blocks2(argv[optind++],
1570 fs_param.s_log_block_size);
1571 if (!fs_blocks_count) {
Eric Sandeen792cce32010-08-12 14:40:44 -04001572 com_err(program_name, 0,
Theodore Ts'o9d92a202010-09-24 22:40:21 -04001573 _("invalid blocks '%s' on device '%s'"),
Eric Sandeen792cce32010-08-12 14:40:44 -04001574 argv[optind - 1], device_name);
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001575 exit(1);
1576 }
1577 }
1578 if (optind < argc)
1579 usage();
1580
Theodore Ts'o74becf31997-04-26 14:37:06 +00001581 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001582 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001583 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001584
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001585 /* Determine the size of the device (if possible) */
Theodore Ts'o493024e2010-06-13 14:00:00 -04001586 if (noaction && fs_blocks_count) {
1587 dev_size = fs_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001588 retval = 0;
Theodore Ts'o493024e2010-06-13 14:00:00 -04001589 } else
Jose R. Santos02d6f472010-06-13 13:00:00 -04001590 retval = ext2fs_get_device_size2(device_name,
1591 EXT2_BLOCK_SIZE(&fs_param),
1592 &dev_size);
Jose R. Santos02d6f472010-06-13 13:00:00 -04001593
Theodore Ts'oa789d841998-03-30 01:20:55 +00001594 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1595 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001596 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001597 exit(1);
1598 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001599 if (!fs_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001600 if (retval == EXT2_ET_UNIMPLEMENTED) {
1601 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001602 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001603 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001604 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001605 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001606 } else {
1607 if (dev_size == 0) {
1608 com_err(program_name, 0,
1609 _("Device size reported to be zero. "
1610 "Invalid partition specified, or\n\t"
1611 "partition table wasn't reread "
1612 "after running fdisk, due to\n\t"
1613 "a modified partition being busy "
1614 "and in use. You may need to reboot\n\t"
1615 "to re-read your partition table.\n"
1616 ));
1617 exit(1);
1618 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001619 fs_blocks_count = dev_size;
1620 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1621 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001622 EXT2_BLOCK_SIZE(&fs_param))-1));
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001623 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001624 } else if (!force && (fs_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001625 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001626 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001627 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001628 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001629
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04001630 if (!fs_type)
1631 profile_get_string(profile, "devices", device_name,
1632 "fs_type", 0, &fs_type);
1633 if (!usage_types)
1634 profile_get_string(profile, "devices", device_name,
1635 "usage_types", 0, &usage_types);
1636
Theodore Ts'o493024e2010-06-13 14:00:00 -04001637 /*
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001638 * We have the file system (or device) size, so we can now
1639 * determine the appropriate file system types so the fs can
1640 * be appropriately configured.
1641 */
1642 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1643 fs_blocks_count ? fs_blocks_count : dev_size,
1644 argv[0]);
1645 if (!fs_types) {
1646 fprintf(stderr, _("Failed to parse fs types list\n"));
1647 exit(1);
1648 }
1649
1650 /* Figure out what features should be enabled */
1651
1652 tmp = NULL;
1653 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1654 tmp = get_string_from_profile(fs_types, "base_features",
1655 "sparse_super,filetype,resize_inode,dir_index");
1656 edit_feature(tmp, &fs_param.s_feature_compat);
1657 free(tmp);
1658
Eric Sandeen6a426c92011-02-17 15:56:17 -06001659 /* And which mount options as well */
1660 tmp = get_string_from_profile(fs_types, "default_mntopts",
1661 "acl,user_xattr");
1662 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1663 if (tmp)
1664 free(tmp);
1665
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001666 for (cpp = fs_types; *cpp; cpp++) {
1667 tmp = NULL;
1668 profile_get_string(profile, "fs_types", *cpp,
1669 "features", "", &tmp);
1670 if (tmp && *tmp)
1671 edit_feature(tmp, &fs_param.s_feature_compat);
1672 if (tmp)
1673 free(tmp);
1674 }
1675 tmp = get_string_from_profile(fs_types, "default_features",
1676 "");
1677 }
1678 edit_feature(fs_features ? fs_features : tmp,
1679 &fs_param.s_feature_compat);
1680 if (tmp)
1681 free(tmp);
1682
1683 /*
Theodore Ts'o493024e2010-06-13 14:00:00 -04001684 * We now need to do a sanity check of fs_blocks_count for
1685 * 32-bit vs 64-bit block number support.
1686 */
1687 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1688 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1689 blocksize = 4096;
1690 fs_param.s_log_block_size = 2;
1691 }
1692 if ((fs_blocks_count > MAX_32_NUM) &&
1693 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1694 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1695 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1696 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1697 }
1698 if ((fs_blocks_count > MAX_32_NUM) &&
1699 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1700 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1701 "too big to be expressed\n\t"
1702 "in 32 bits using a blocksize of %d.\n"),
1703 program_name, fs_blocks_count, device_name,
1704 EXT2_BLOCK_SIZE(&fs_param));
1705 exit(1);
1706 }
1707
1708 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1709
Theodore Ts'ob4d51052008-07-06 20:24:29 -04001710 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1711 fs_types[0] = strdup("journal");
1712 fs_types[1] = 0;
1713 }
1714
1715 if (verbose) {
1716 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1717 print_str_list(fs_types);
1718 }
1719
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001720 if (r_opt == EXT2_GOOD_OLD_REV &&
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001721 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
Theodore Ts'o92fb8542008-07-18 21:08:56 -04001722 fs_param.s_feature_ro_compat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001723 fprintf(stderr, _("Filesystem features not supported "
1724 "with revision 0 filesystems\n"));
1725 exit(1);
1726 }
1727
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001728 if (s_opt > 0) {
1729 if (r_opt == EXT2_GOOD_OLD_REV) {
1730 fprintf(stderr, _("Sparse superblocks not supported "
1731 "with revision 0 filesystems\n"));
1732 exit(1);
1733 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001734 fs_param.s_feature_ro_compat |=
1735 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001736 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001737 fs_param.s_feature_ro_compat &=
1738 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1739
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001740 if (journal_size != 0) {
1741 if (r_opt == EXT2_GOOD_OLD_REV) {
1742 fprintf(stderr, _("Journals not supported "
1743 "with revision 0 filesystems\n"));
1744 exit(1);
1745 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001746 fs_param.s_feature_compat |=
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001747 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001748 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001749
Aditya Kalid3859af2011-05-10 14:51:31 -07001750 /* Get reserved_ratio from profile if not specified on cmd line. */
1751 if (reserved_ratio < 0.0) {
1752 reserved_ratio = get_double_from_profile(
1753 fs_types, "reserved_ratio", 5.0);
1754 if (reserved_ratio > 50 || reserved_ratio < 0) {
1755 com_err(program_name, 0,
1756 _("invalid reserved blocks percent - %lf"),
1757 reserved_ratio);
1758 exit(1);
1759 }
1760 }
1761
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001762 if (fs_param.s_feature_incompat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001763 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001764 reserved_ratio = 0;
1765 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1766 fs_param.s_feature_compat = 0;
1767 fs_param.s_feature_ro_compat = 0;
1768 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001769
1770 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1771 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1772 fprintf(stderr, _("The resize_inode and meta_bg features "
1773 "are not compatible.\n"
1774 "They can not be both enabled "
1775 "simultaneously.\n"));
1776 exit(1);
1777 }
1778
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001779 /* Set first meta blockgroup via an environment variable */
1780 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001781 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001782 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001783 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001784
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001785 /* Get the hardware sector sizes, if available */
1786 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001787 if (retval) {
1788 com_err(program_name, retval,
1789 _("while trying to determine hardware sector size"));
1790 exit(1);
1791 }
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001792 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1793 if (retval) {
1794 com_err(program_name, retval,
1795 _("while trying to determine physical sector size"));
1796 exit(1);
1797 }
Theodore Ts'o92eb5a32010-11-22 11:09:00 -05001798
1799 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1800 lsector_size = atoi(tmp);
1801 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1802 psector_size = atoi(tmp);
1803
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001804 /* Older kernels may not have physical/logical distinction */
1805 if (!psector_size)
1806 psector_size = lsector_size;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001807
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001808 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001809 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001810
1811 if (use_bsize == -1) {
1812 use_bsize = sys_page_size;
1813 if ((linux_version_code < (2*65536 + 6*256)) &&
1814 (use_bsize > 4096))
1815 use_bsize = 4096;
1816 }
Theodore Ts'o2b21a0d2010-11-22 11:14:35 -05001817 if (lsector_size && use_bsize < lsector_size)
1818 use_bsize = lsector_size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001819 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1820 use_bsize = -blocksize;
1821 blocksize = use_bsize;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001822 ext2fs_blocks_count_set(&fs_param,
1823 ext2fs_blocks_count(&fs_param) /
1824 (blocksize / 1024));
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001825 } else {
Theodore Ts'of89f54a2010-11-21 09:56:53 -05001826 if (blocksize < lsector_size) { /* Impossible */
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001827 com_err(program_name, EINVAL,
1828 _("while setting blocksize; too small "
1829 "for device\n"));
1830 exit(1);
Theodore Ts'of89f54a2010-11-21 09:56:53 -05001831 } else if ((blocksize < psector_size) &&
1832 (psector_size <= sys_page_size)) { /* Suboptimal */
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001833 fprintf(stderr, _("Warning: specified blocksize %d is "
Theodore Ts'of89f54a2010-11-21 09:56:53 -05001834 "less than device physical sectorsize %d\n"),
1835 blocksize, psector_size);
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001836 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001837 }
1838
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001839 fs_param.s_log_block_size =
1840 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1841 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001842 if (!cluster_size)
1843 cluster_size = get_int_from_profile(fs_types,
1844 "cluster_size",
Theodore Ts'ob12a0bc2011-06-16 10:11:06 -04001845 blocksize*16);
Theodore Ts'o2d34a252011-06-14 14:30:22 -04001846 fs_param.s_log_cluster_size =
1847 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001848 } else
1849 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1850
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001851 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001852 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1853 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001854 if (inode_ratio < blocksize)
1855 inode_ratio = blocksize;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04001856 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1857 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001858 }
1859
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001860#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
Theodore Ts'o1599b472010-11-22 10:50:42 -05001861 retval = get_device_geometry(device_name, &fs_param, psector_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001862 if (retval < 0) {
1863 fprintf(stderr,
Eric Sandeen13b0b122010-01-23 21:50:45 -06001864 _("warning: Unable to get device geometry for %s\n"),
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001865 device_name);
1866 } else if (retval) {
1867 printf(_("%s alignment is offset by %lu bytes.\n"),
1868 device_name, retval);
1869 printf(_("This may result in very poor performance, "
1870 "(re)-partitioning suggested.\n"));
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001871 }
1872#endif
1873
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001874 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001875
Yury V. Zaytsev45792c12011-09-15 23:08:52 -04001876 /* This check should happen beyond the last assignment to blocksize */
1877 if (blocksize > sys_page_size) {
1878 if (!force) {
1879 com_err(program_name, 0,
1880 _("%d-byte blocks too big for system (max %d)"),
1881 blocksize, sys_page_size);
1882 proceed_question();
1883 }
1884 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1885 "(max %d), forced to continue\n"),
1886 blocksize, sys_page_size);
1887 }
1888
Theodore Ts'o210fd2c2010-10-01 10:47:38 -04001889 lazy_itable_init = 0;
1890 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1891 lazy_itable_init = 1;
1892
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001893 lazy_itable_init = get_bool_from_profile(fs_types,
Theodore Ts'o210fd2c2010-10-01 10:47:38 -04001894 "lazy_itable_init",
1895 lazy_itable_init);
Lukas Czerner7fe5ff32010-11-18 14:38:41 +01001896 discard = get_bool_from_profile(fs_types, "discard" , discard);
Andreas Dilger6c546892011-06-11 12:17:29 -04001897 journal_flags |= get_bool_from_profile(fs_types,
1898 "lazy_journal_init", 0) ?
1899 EXT2_MKJOURNAL_LAZYINIT : 0;
Theodore Ts'o304e11c2012-04-05 12:30:02 -07001900 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001901
Theodore Ts'o2d363582008-05-14 18:09:37 -04001902 /* Get options from profile */
1903 for (cpp = fs_types; *cpp; cpp++) {
1904 tmp = NULL;
1905 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1906 if (tmp && *tmp)
1907 parse_extended_opts(&fs_param, tmp);
Jim Meyering45e338f2009-02-23 18:07:50 +01001908 free(tmp);
Theodore Ts'o2d363582008-05-14 18:09:37 -04001909 }
1910
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001911 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001912 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001913
1914 /* Since sparse_super is the default, we would only have a problem
1915 * here if it was explicitly disabled.
1916 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001917 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1918 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001919 com_err(program_name, 0,
1920 _("reserved online resize blocks not supported "
1921 "on non-sparse filesystem"));
1922 exit(1);
1923 }
1924
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001925 if (fs_param.s_blocks_per_group) {
1926 if (fs_param.s_blocks_per_group < 256 ||
1927 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001928 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001929 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001930 exit(1);
1931 }
1932 }
1933
Theodore Ts'o3d438362008-02-19 08:32:58 -05001934 if (inode_size == 0)
1935 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001936 if (!flex_bg_size && (fs_param.s_feature_incompat &
1937 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1938 flex_bg_size = get_int_from_profile(fs_types,
1939 "flex_bg_size", 16);
1940 if (flex_bg_size) {
1941 if (!(fs_param.s_feature_incompat &
1942 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1943 com_err(program_name, 0,
1944 _("Flex_bg feature not enabled, so "
1945 "flex_bg size may not be specified"));
1946 exit(1);
1947 }
1948 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1949 }
Andreas Dilger067911a2006-07-15 22:08:20 -04001950
1951 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001952 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001953 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001954 inode_size & (inode_size - 1)) {
1955 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001956 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001957 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001958 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001959 exit(1);
1960 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001961 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001962 }
1963
Eric Sandeenf3358642006-09-12 14:56:17 -04001964 /* Make sure number of inodes specified will fit in 32 bits */
1965 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001966 unsigned long long n;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001967 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
Theodore Ts'o493024e2010-06-13 14:00:00 -04001968 if (n > MAX_32_NUM) {
Jose R. Santos02d6f472010-06-13 13:00:00 -04001969 if (fs_param.s_feature_incompat &
1970 EXT4_FEATURE_INCOMPAT_64BIT)
Theodore Ts'o493024e2010-06-13 14:00:00 -04001971 num_inodes = MAX_32_NUM;
Jose R. Santos02d6f472010-06-13 13:00:00 -04001972 else {
1973 com_err(program_name, 0,
Theodore Ts'o84888e52011-10-08 13:32:00 -04001974 _("too many inodes (%llu), raise "
Jose R. Santos02d6f472010-06-13 13:00:00 -04001975 "inode ratio?"), n);
1976 exit(1);
1977 }
Eric Sandeenf3358642006-09-12 14:56:17 -04001978 }
Theodore Ts'o493024e2010-06-13 14:00:00 -04001979 } else if (num_inodes > MAX_32_NUM) {
Eric Sandeenf3358642006-09-12 14:56:17 -04001980 com_err(program_name, 0,
1981 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001982 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001983 exit(1);
1984 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001985 /*
1986 * Calculate number of inodes based on the inode ratio
1987 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001988 fs_param.s_inodes_count = num_inodes ? num_inodes :
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001989 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001990
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001991 if ((((long long)fs_param.s_inodes_count) *
1992 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001993 ((ext2fs_blocks_count(&fs_param)) *
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001994 EXT2_BLOCK_SIZE(&fs_param))) {
1995 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1996 "(%u) too big for a\n\t"
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001997 "filesystem with %llu blocks, "
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001998 "specify higher inode_ratio (-i)\n\t"
1999 "or lower inode count (-N).\n"),
2000 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002001 fs_param.s_inodes_count,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002002 (unsigned long long) ext2fs_blocks_count(&fs_param));
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002003 exit(1);
2004 }
2005
Theodore Ts'o3839e651997-04-26 13:21:57 +00002006 /*
2007 * Calculate number of blocks to reserve
2008 */
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002009 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2010 ext2fs_blocks_count(&fs_param) / 100.0);
Theodore Ts'o4c2b28a2011-06-14 14:17:56 -04002011
2012 free(fs_type);
2013 free(usage_types);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002014}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002015
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302016static int should_do_undo(const char *name)
2017{
2018 errcode_t retval;
2019 io_channel channel;
2020 __u16 s_magic;
2021 struct ext2_super_block super;
2022 io_manager manager = unix_io_manager;
2023 int csum_flag, force_undo;
2024
2025 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2026 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2027 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2028 if (!force_undo && (!csum_flag || !lazy_itable_init))
2029 return 0;
2030
2031 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2032 if (retval) {
2033 /*
2034 * We don't handle error cases instead we
2035 * declare that the file system doesn't exist
2036 * and let the rest of mke2fs take care of
2037 * error
2038 */
2039 retval = 0;
2040 goto open_err_out;
2041 }
2042
2043 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -04002044 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302045 if (retval) {
2046 retval = 0;
2047 goto err_out;
2048 }
2049
2050#if defined(WORDS_BIGENDIAN)
2051 s_magic = ext2fs_swab16(super.s_magic);
2052#else
2053 s_magic = super.s_magic;
2054#endif
2055
2056 if (s_magic == EXT2_SUPER_MAGIC)
2057 retval = 1;
2058
2059err_out:
2060 io_channel_close(channel);
2061
2062open_err_out:
2063
2064 return retval;
2065}
2066
2067static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2068{
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002069 errcode_t retval = ENOMEM;
Eric Sandeen30295f12011-09-16 15:49:40 -05002070 char *tdb_dir = NULL, *tdb_file = NULL;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302071 char *device_name, *tmp_name;
Eric Sandeen30295f12011-09-16 15:49:40 -05002072 int free_tdb_dir = 0;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302073
2074 /*
2075 * Configuration via a conf file would be
2076 * nice
2077 */
2078 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
Eric Sandeen30295f12011-09-16 15:49:40 -05002079 if (!tdb_dir) {
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302080 profile_get_string(profile, "defaults",
2081 "undo_dir", 0, "/var/lib/e2fsprogs",
2082 &tdb_dir);
Eric Sandeen30295f12011-09-16 15:49:40 -05002083 free_tdb_dir = 1;
2084 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302085
2086 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2087 access(tdb_dir, W_OK))
2088 return 0;
2089
2090 tmp_name = strdup(name);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002091 if (!tmp_name)
2092 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302093 device_name = basename(tmp_name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04002094 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002095 if (!tdb_file) {
2096 free(tmp_name);
2097 goto errout;
2098 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302099 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002100 free(tmp_name);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302101
2102 if (!access(tdb_file, F_OK)) {
2103 if (unlink(tdb_file) < 0) {
2104 retval = errno;
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002105 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302106 }
2107 }
2108
2109 set_undo_io_backing_manager(*io_ptr);
2110 *io_ptr = undo_io_manager;
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002111 retval = set_undo_io_backup_file(tdb_file);
2112 if (retval)
2113 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302114 printf(_("Overwriting existing filesystem; this can be undone "
2115 "using the command:\n"
2116 " e2undo %s %s\n\n"), tdb_file, name);
Eric Sandeen79e62402008-07-06 18:36:56 -04002117
Eric Sandeen30295f12011-09-16 15:49:40 -05002118 if (free_tdb_dir)
2119 free(tdb_dir);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04002120 free(tdb_file);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002121 return 0;
2122
2123errout:
Eric Sandeen30295f12011-09-16 15:49:40 -05002124 if (free_tdb_dir)
2125 free(tdb_dir);
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002126 free(tdb_file);
2127 com_err(program_name, retval,
2128 _("while trying to setup undo file\n"));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302129 return retval;
2130}
2131
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002132static int mke2fs_discard_device(ext2_filsys fs)
2133{
2134 struct ext2fs_numeric_progress_struct progress;
2135 blk64_t blocks = ext2fs_blocks_count(fs->super);
2136 blk64_t count = DISCARD_STEP_MB;
Lukas Czernerd2bfdc72011-09-15 23:44:59 -04002137 blk64_t cur;
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002138 int retval = 0;
2139
Lukas Czernerd2bfdc72011-09-15 23:44:59 -04002140 /*
2141 * Let's try if discard really works on the device, so
2142 * we do not print numeric progress resulting in failure
2143 * afterwards.
2144 */
2145 retval = io_channel_discard(fs->io, 0, fs->blocksize);
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002146 if (retval)
2147 return retval;
Lukas Czernerd2bfdc72011-09-15 23:44:59 -04002148 cur = fs->blocksize;
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002149
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002150 count *= (1024 * 1024);
2151 count /= fs->blocksize;
2152
2153 ext2fs_numeric_progress_init(fs, &progress,
2154 _("Discarding device blocks: "),
2155 blocks);
2156 while (cur < blocks) {
Theodore Ts'oc498cb12012-09-22 21:26:48 -04002157 ext2fs_numeric_progress_update(fs, &progress, cur);
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002158
2159 if (cur + count > blocks)
2160 count = blocks - cur;
2161
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002162 retval = io_channel_discard(fs->io, cur, count);
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002163 if (retval)
2164 break;
2165 cur += count;
2166 }
2167
2168 if (retval) {
2169 ext2fs_numeric_progress_close(fs, &progress,
2170 _("failed - "));
2171 if (!quiet)
2172 printf("%s\n",error_message(retval));
2173 } else
2174 ext2fs_numeric_progress_close(fs, &progress,
2175 _("done \n"));
2176
2177 return retval;
2178}
2179
Andreas Dilger96367ad2011-06-15 22:17:38 -04002180static void fix_cluster_bg_counts(ext2_filsys fs)
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002181{
2182 blk64_t cluster, num_clusters, tot_free;
2183 int grp_free, num_free, group, num;
2184
2185 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2186 tot_free = num_free = num = group = grp_free = 0;
2187 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2188 cluster < num_clusters; cluster++) {
2189 if (!ext2fs_test_block_bitmap2(fs->block_map,
2190 EXT2FS_C2B(fs, cluster))) {
2191 grp_free++;
2192 tot_free++;
2193 }
2194 num++;
2195 if ((num == fs->super->s_clusters_per_group) ||
2196 (cluster == num_clusters-1)) {
2197 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2198 ext2fs_group_desc_csum_set(fs, group);
2199 num = 0;
2200 grp_free = 0;
2201 group++;
2202 }
2203 }
Theodore Ts'ofe75afb2011-06-16 01:38:43 -04002204 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002205}
2206
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002207static int create_quota_inodes(ext2_filsys fs)
2208{
2209 quota_ctx_t qctx;
2210
Aditya Kalia86d55d2011-11-14 10:55:54 -05002211 quota_init_context(&qctx, fs, -1);
2212 quota_compute_usage(qctx);
Aditya Kalid678fef2011-11-14 10:55:54 -05002213 quota_write_inode(qctx, quotatype);
Aditya Kalia86d55d2011-11-14 10:55:54 -05002214 quota_release_context(&qctx);
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002215
Aditya Kalia86d55d2011-11-14 10:55:54 -05002216 return 0;
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002217}
2218
Theodore Ts'o3839e651997-04-26 13:21:57 +00002219int main (int argc, char *argv[])
2220{
2221 errcode_t retval = 0;
2222 ext2_filsys fs;
2223 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002224 unsigned int journal_blocks;
Theodore Ts'o14b283a2011-09-28 22:45:12 -04002225 unsigned int i, checkinterval;
2226 int max_mnt_count;
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002227 int val, hash_alg;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002228 int flags;
2229 int old_bitmaps;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002230 io_manager io_ptr;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302231 char tdb_string[40];
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002232 char *hash_alg_str;
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002233 int itable_zeroed = 0;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002234
2235#ifdef ENABLE_NLS
2236 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05002237 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002238 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2239 textdomain(NLS_CAT_NAME);
Theodore Ts'o9d4507c2011-10-05 01:00:30 -04002240 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002241#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00002242 PRS(argc, argv);
2243
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002244#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04002245 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2246 io_ptr = test_io_manager;
2247 test_io_backing_manager = unix_io_manager;
2248 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002249#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04002250 io_ptr = unix_io_manager;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002251
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302252 if (should_do_undo(device_name)) {
2253 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2254 if (retval)
2255 exit(1);
2256 }
2257
Theodore Ts'o3839e651997-04-26 13:21:57 +00002258 /*
2259 * Initialize the superblock....
2260 */
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002261 flags = EXT2_FLAG_EXCLUSIVE;
Theodore Ts'o37c8db72012-03-22 16:00:49 -04002262 if (direct_io)
2263 flags |= EXT2_FLAG_DIRECT_IO;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002264 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2265 &old_bitmaps);
2266 if (!old_bitmaps)
2267 flags |= EXT2_FLAG_64BITS;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002268 /*
2269 * By default, we print how many inode tables or block groups
2270 * or whatever we've written so far. The quiet flag disables
2271 * this, along with a lot of other output.
2272 */
2273 if (!quiet)
2274 flags |= EXT2_FLAG_PRINT_PROGRESS;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04002275 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002276 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002277 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00002278 exit(1);
2279 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002280
Eric Sandeen5827d242009-10-07 16:49:17 -05002281 /* Can't undo discard ... */
Andreas Dilger8185ab92011-06-07 10:22:29 -06002282 if (!noaction && discard && (io_ptr != undo_io_manager)) {
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002283 retval = mke2fs_discard_device(fs);
Lukas Czernerd8665992010-11-18 03:38:37 +00002284 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002285 if (verbose)
2286 printf(_("Discard succeeded and will return 0s "
2287 " - skipping inode table wipe\n"));
2288 lazy_itable_init = 1;
2289 itable_zeroed = 1;
2290 }
2291 }
Eric Sandeen5827d242009-10-07 16:49:17 -05002292
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302293 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2294 32768 : fs->blocksize * 8);
2295 io_channel_set_options(fs->io, tdb_string);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002296
Theodore Ts'o6cb27402008-01-26 19:06:35 -05002297 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2298 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2299
Theodore Ts'ob7c5b402009-03-05 19:40:20 -05002300 if ((fs_param.s_feature_incompat &
2301 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2302 (fs_param.s_feature_ro_compat &
2303 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2304 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2305 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2306 fs->super->s_kbytes_written = 1;
2307
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002308 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00002309 * Wipe out the old on-disk superblock
2310 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04002311 if (!noaction)
2312 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00002313
2314 /*
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05002315 * Parse or generate a UUID for the filesystem
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002316 */
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05002317 if (fs_uuid) {
2318 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2319 com_err(device_name, 0, "could not parse UUID: %s\n",
2320 fs_uuid);
2321 exit(1);
2322 }
2323 } else
2324 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002325
2326 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04002327 * Initialize the directory index variables
2328 */
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002329 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2330 "half_md4");
2331 hash_alg = e2p_string2hash(hash_alg_str);
Theodore Ts'o5a2db042010-12-01 18:49:26 -05002332 free(hash_alg_str);
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002333 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2334 EXT2_HASH_HALF_MD4;
Theodore Ts'o843049c2002-09-22 15:37:40 -04002335 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2336
2337 /*
Eric Sandeen3daf5922011-02-17 15:55:15 -06002338 * Periodic checks can be enabled/disabled via config file.
2339 * Note we override the kernel include file's idea of what the default
2340 * check interval (never) should be. It's a good idea to check at
2341 * least *occasionally*, specially since servers will never rarely get
2342 * to reboot, since Linux is so robust these days. :-)
2343 *
2344 * 180 days (six months) seems like a good value.
Theodore Ts'o44c09c02001-01-14 17:02:09 +00002345 */
Eric Sandeen3daf5922011-02-17 15:55:15 -06002346#ifdef EXT2_DFL_CHECKINTERVAL
2347#undef EXT2_DFL_CHECKINTERVAL
2348#endif
2349#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2350
2351 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2352 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2353 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2354 /*
2355 * Add "jitter" to the superblock's check interval so that we
2356 * don't check all the filesystems at the same time. We use a
2357 * kludgy hack of using the UUID to derive a random jitter value
2358 */
2359 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2360 val += fs->super->s_uuid[i];
2361 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
Theodore Ts'o14b283a2011-09-28 22:45:12 -04002362 } else
2363 fs->super->s_max_mnt_count = -1;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00002364
2365 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002366 * Override the creator OS, if applicable
2367 */
2368 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002369 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002370 exit(1);
2371 }
2372
2373 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00002374 * For the Hurd, we will turn off filetype since it doesn't
2375 * support it.
2376 */
2377 if (fs->super->s_creator_os == EXT2_OS_HURD)
2378 fs->super->s_feature_incompat &=
2379 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2380
2381 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002382 * Set the volume label...
2383 */
2384 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00002385 memset(fs->super->s_volume_name, 0,
2386 sizeof(fs->super->s_volume_name));
2387 strncpy(fs->super->s_volume_name, volume_label,
2388 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002389 }
2390
2391 /*
2392 * Set the last mount directory
2393 */
2394 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00002395 memset(fs->super->s_last_mounted, 0,
2396 sizeof(fs->super->s_last_mounted));
2397 strncpy(fs->super->s_last_mounted, mount_dir,
2398 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002399 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002400
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002401 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00002402 show_stats(fs);
2403
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002404 if (noaction)
2405 exit(0);
2406
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002407 if (fs->super->s_feature_incompat &
2408 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2409 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06002410 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002411 }
2412
Theodore Ts'o3839e651997-04-26 13:21:57 +00002413 if (bad_blocks_filename)
2414 read_bb_file(fs, &bb_list, bad_blocks_filename);
2415 if (cflag)
2416 test_disk(fs, &bb_list);
2417
2418 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05002419 fs->stride = fs_stride = fs->super->s_raid_stride;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002420 if (!quiet)
2421 printf(_("Allocating group tables: "));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002422 retval = ext2fs_allocate_tables(fs);
2423 if (retval) {
2424 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002425 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002426 exit(1);
2427 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002428 if (!quiet)
2429 printf(_("done \n"));
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002430
2431 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2432 if (retval) {
2433 com_err(program_name, retval,
2434 _("\n\twhile converting subcluster bitmap"));
2435 exit(1);
2436 }
2437
Theodore Ts'of3db3561997-04-26 13:34:30 +00002438 if (super_only) {
2439 fs->super->s_state |= EXT2_ERROR_FS;
2440 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
Theodore Ts'oba9e0af2012-02-16 23:16:34 -05002441 /*
2442 * The command "mke2fs -S" is used to recover
2443 * corrupted file systems, so do not mark any of the
2444 * inodes as unused; we want e2fsck to consider all
2445 * inodes as potentially containing recoverable data.
2446 */
2447 if (fs->super->s_feature_ro_compat &
2448 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Theodore Ts'o30ac1ce2012-02-27 00:51:39 -05002449 for (i = 0; i < fs->group_desc_count; i++)
Theodore Ts'oba9e0af2012-02-16 23:16:34 -05002450 ext2fs_bg_itable_unused_set(fs, i, 0);
2451 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002452 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06002453 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Jose R. Santos02d6f472010-06-13 13:00:00 -04002454 blk64_t rsv = 65536 / fs->blocksize;
2455 blk64_t blocks = ext2fs_blocks_count(fs->super);
2456 blk64_t start;
2457 blk64_t ret_blk;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002458
2459#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04002460 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002461#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06002462
2463 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002464 * Wipe out any old MD RAID (or other) metadata at the end
2465 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06002466 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002467 */
Andreas Dilger59f27242001-08-30 15:39:04 -06002468 start = (blocks & ~(rsv - 1));
2469 if (start > rsv)
2470 start -= rsv;
2471 if (start > 0)
Jose R. Santos02d6f472010-06-13 13:00:00 -04002472 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302473 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06002474
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002475 if (retval) {
2476 com_err(program_name, retval,
Jose R. Santos02d6f472010-06-13 13:00:00 -04002477 _("while zeroing block %llu at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002478 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002479 }
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002480 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002481 create_root_dir(fs);
2482 create_lost_and_found(fs);
2483 reserve_inodes(fs);
2484 create_bad_block_inode(fs, bb_list);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002485 if (fs->super->s_feature_compat &
Theodore Ts'oea774312005-01-28 11:45:28 -05002486 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2487 retval = ext2fs_create_resize_inode(fs);
2488 if (retval) {
2489 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002490 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05002491 exit(1);
2492 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002493 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002494 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002495
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002496 if (journal_device) {
2497 ext2_filsys jfs;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002498
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002499 if (!force)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002500 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00002501 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002502
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002503 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2504 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2505 fs->blocksize, unix_io_manager, &jfs);
2506 if (retval) {
2507 com_err(program_name, retval,
2508 _("while trying to open journal device %s\n"),
2509 journal_device);
2510 exit(1);
2511 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002512 if (!quiet) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002513 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002514 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002515 fflush(stdout);
2516 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002517 retval = ext2fs_add_journal_device(fs, jfs);
2518 if(retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002519 com_err (program_name, retval,
2520 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002521 journal_device);
2522 exit(1);
2523 }
2524 if (!quiet)
2525 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002526 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06002527 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002528 } else if ((journal_size) ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002529 (fs_param.s_feature_compat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002530 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002531 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002532
Theodore Ts'oa620bad2009-03-31 07:42:24 -04002533 if (super_only) {
2534 printf(_("Skipping journal creation in super-only mode\n"));
2535 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2536 goto no_journal;
2537 }
2538
Theodore Ts'o93345d12001-02-17 06:09:50 +00002539 if (!journal_blocks) {
2540 fs->super->s_feature_compat &=
2541 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2542 goto no_journal;
2543 }
2544 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002545 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00002546 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002547 fflush(stdout);
2548 }
Theodore Ts'o63985322001-01-03 17:02:13 +00002549 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2550 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002551 if (retval) {
2552 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00002553 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002554 exit(1);
2555 }
2556 if (!quiet)
2557 printf(_("done\n"));
2558 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002559no_journal:
Andreas Dilger0f5eba72011-09-24 13:48:55 -04002560 if (!super_only &&
2561 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2562 retval = ext2fs_mmp_init(fs);
2563 if (retval) {
2564 fprintf(stderr, _("\nError while enabling multiple "
2565 "mount protection feature."));
2566 exit(1);
2567 }
2568 if (!quiet)
2569 printf(_("Multiple mount protection is enabled "
2570 "with update interval %d seconds.\n"),
2571 fs->super->s_mmp_update_interval);
2572 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002573
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002574 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2575 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2576 fix_cluster_bg_counts(fs);
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002577 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2578 EXT4_FEATURE_RO_COMPAT_QUOTA))
2579 create_quota_inodes(fs);
2580
Theodore Ts'o3839e651997-04-26 13:21:57 +00002581 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002582 printf(_("Writing superblocks and "
2583 "filesystem accounting information: "));
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002584 checkinterval = fs->super->s_checkinterval;
2585 max_mnt_count = fs->super->s_max_mnt_count;
2586 retval = ext2fs_close(fs);
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002587 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05002588 fprintf(stderr,
2589 _("\nWarning, had trouble writing out superblocks."));
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002590 } else if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002591 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002592 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002593 print_check_message(max_mnt_count, checkinterval);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002594 }
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002595
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002596 remove_error_table(&et_ext2_error_table);
2597 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002598 profile_release(profile);
Theodore Ts'o5a2db042010-12-01 18:49:26 -05002599 for (i=0; fs_types[i]; i++)
2600 free(fs_types[i]);
2601 free(fs_types);
Lukas Czernerfaa2dcd2011-09-13 22:24:11 -04002602 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002603}