blob: ee73d39eb37a803a07d343ac2073cdcde70ff301 [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'oa418d3a1997-04-26 14:00:26 +000021#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <string.h>
Theodore Ts'od0c53772009-07-18 18:41:51 -040023#include <strings.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <fcntl.h>
25#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050027#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000028#include <sys/utsname.h>
29#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000030#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000031#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000032#else
33extern char *optarg;
34extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000035#endif
36#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#endif
39#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000040#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000041#endif
42#ifdef HAVE_ERRNO_H
43#include <errno.h>
44#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <sys/types.h>
Eric Sandeen13b0b122010-01-23 21:50:45 -060047#include <sys/stat.h>
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053048#include <libgen.h>
Theodore Ts'o36585792008-06-07 22:07:50 -040049#include <limits.h>
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -050050#include <blkid/blkid.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000051
Theodore Ts'o54c637d2001-05-14 11:45:38 +000052#include "ext2fs/ext2_fs.h"
JP Abgralle0ed7402014-03-19 19:08:39 -070053#include "ext2fs/ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000055#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000056#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000058#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050059#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050060#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000061#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000062#include "nls-enable.h"
JP Abgralle0ed7402014-03-19 19:08:39 -070063#include "quota/mkquota.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
65#define STRIDE_LENGTH 8
66
JP Abgralle0ed7402014-03-19 19:08:39 -070067#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
68
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000069#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000070#define ZAP_BOOTBLOCK
71#endif
72
Bernhard Rosenkränzer18a14442013-12-31 14:43:20 +010073#ifndef ROOT_SYSCONFDIR
74#define ROOT_SYSCONFDIR "/etc"
75#endif
76
JP Abgralle0ed7402014-03-19 19:08:39 -070077#define DISCARD_STEP_MB (2048)
78
Theodore Ts'o3839e651997-04-26 13:21:57 +000079extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000080extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
JP Abgralle0ed7402014-03-19 19:08:39 -070082static const char * program_name = "mke2fs";
83static const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
85/* Command line options */
JP Abgralle0ed7402014-03-19 19:08:39 -070086static int cflag;
87static int verbose;
88static int quiet;
89static int super_only;
90static int discard = 1; /* attempt to discard device before fs creation */
91static int direct_io;
92static int force;
93static int noaction;
94static uid_t root_uid;
95static gid_t root_gid;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000096int journal_size;
97int journal_flags;
JP Abgralle0ed7402014-03-19 19:08:39 -070098static int lazy_itable_init;
99static char *bad_blocks_filename = NULL;
100static __u32 fs_stride;
101static int quotatype = -1; /* Initialize both user and group quotas by default */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102
JP Abgralle0ed7402014-03-19 19:08:39 -0700103static struct ext2_super_block fs_param;
104static char *fs_uuid = NULL;
105static char *creator_os;
106static char *volume_label;
107static char *mount_dir;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000108char *journal_device;
JP Abgralle0ed7402014-03-19 19:08:39 -0700109static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
110static char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111
JP Abgralle0ed7402014-03-19 19:08:39 -0700112static profile_t profile;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500113
JP Abgralle0ed7402014-03-19 19:08:39 -0700114static int sys_page_size = 4096;
115static int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400116
Theodore Ts'o63985322001-01-03 17:02:13 +0000117static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500119 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
JP Abgralle0ed7402014-03-19 19:08:39 -0700120 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500121 "[-J journal-options]\n"
JP Abgralle0ed7402014-03-19 19:08:39 -0700122 "\t[-G flex-group-size] [-N number-of-inodes]\n"
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400123 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
124 "\t[-g blocks-per-group] [-L volume-label] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400125 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500126 "[-r fs-revision] [-E extended-option[,...]]\n"
JP Abgralle0ed7402014-03-19 19:08:39 -0700127 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
128 "[-jnqvDFKSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129 program_name);
130 exit(1);
131}
132
JP Abgralle0ed7402014-03-19 19:08:39 -0700133static int int_log2(unsigned long long arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134{
135 int l = 0;
136
137 arg >>= 1;
138 while (arg) {
139 l++;
140 arg >>= 1;
141 }
142 return l;
143}
144
JP Abgralle0ed7402014-03-19 19:08:39 -0700145static int int_log10(unsigned long long arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000146{
147 int l;
148
149 for (l=0; arg ; l++)
150 arg = arg / 10;
151 return l;
152}
153
JP Abgralle0ed7402014-03-19 19:08:39 -0700154#ifdef __linux__
Theodore Ts'od99225e2004-09-25 07:40:12 -0400155static int parse_version_number(const char *s)
156{
157 int major, minor, rev;
158 char *endptr;
159 const char *cp = s;
160
161 if (!s)
162 return 0;
163 major = strtol(cp, &endptr, 10);
164 if (cp == endptr || *endptr != '.')
165 return 0;
166 cp = endptr + 1;
167 minor = strtol(cp, &endptr, 10);
168 if (cp == endptr || *endptr != '.')
169 return 0;
170 cp = endptr + 1;
171 rev = strtol(cp, &endptr, 10);
172 if (cp == endptr)
173 return 0;
174 return ((((major * 256) + minor) * 256) + rev);
175}
JP Abgralle0ed7402014-03-19 19:08:39 -0700176#endif
Theodore Ts'od99225e2004-09-25 07:40:12 -0400177
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000178/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 * Helper function for read_bb_file and test_disk
180 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500181static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182{
Theodore Ts'o66938372002-03-08 00:14:46 -0500183 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 return;
185}
186
187/*
188 * Reads the bad blocks list from a file
189 */
190static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
191 const char *bad_blocks_file)
192{
193 FILE *f;
194 errcode_t retval;
195
196 f = fopen(bad_blocks_file, "r");
197 if (!f) {
198 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000199 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 exit(1);
201 }
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
203 fclose (f);
204 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700205 com_err("ext2fs_read_bb_FILE", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000206 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 exit(1);
208 }
209}
210
211/*
212 * Runs the badblocks program to test the disk
213 */
214static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
215{
216 FILE *f;
217 errcode_t retval;
218 char buf[1024];
219
JP Abgralle0ed7402014-03-19 19:08:39 -0700220 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500221 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
JP Abgralle0ed7402014-03-19 19:08:39 -0700222 fs->device_name, ext2fs_blocks_count(fs->super)-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000224 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 f = popen(buf, "r");
226 if (!f) {
227 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400228 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 exit(1);
230 }
231 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000232 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700234 com_err("ext2fs_read_bb_FILE", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000235 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 exit(1);
237 }
238}
239
240static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
241{
Theodore Ts'o54434922003-12-07 01:28:50 -0500242 dgrp_t i;
243 blk_t j;
244 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 blk_t blk;
246 badblocks_iterate bb_iter;
247 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000248 blk_t group_block;
249 int group;
250 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251
252 if (!bb_list)
253 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400254
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255 /*
256 * The primary superblock and group descriptors *must* be
257 * good; if not, abort.
258 */
259 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
260 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000261 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000262 fprintf(stderr, _("Block %d in primary "
263 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400264 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000265 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500267 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268 exit(1);
269 }
270 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000271
272 /*
273 * See if any of the bad blocks are showing up in the backup
274 * superblocks and/or group descriptors. If so, issue a
275 * warning and adjust the block counts appropriately.
276 */
277 group_block = fs->super->s_first_data_block +
278 fs->super->s_blocks_per_group;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400279
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000281 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000283 if (ext2fs_badblocks_list_test(bb_list,
284 group_block + j)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400285 if (!group_bad)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500287_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000288" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 group_block);
290 group_bad++;
JP Abgralle0ed7402014-03-19 19:08:39 -0700291 group = ext2fs_group_of_blk2(fs, group_block+j);
292 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400293 ext2fs_group_desc_csum_set(fs, group);
JP Abgralle0ed7402014-03-19 19:08:39 -0700294 ext2fs_free_blocks_count_add(fs->super, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000295 }
296 }
297 group_block += fs->super->s_blocks_per_group;
298 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400299
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 /*
301 * Mark all the bad blocks as used...
302 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000303 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700305 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000306 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 exit(1);
308 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400309 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
JP Abgralle0ed7402014-03-19 19:08:39 -0700310 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000311 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312}
313
Eric Sandeen6fcd6f82010-08-20 16:41:14 -0500314static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000315{
316 errcode_t retval;
JP Abgralle0ed7402014-03-19 19:08:39 -0700317 blk64_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500318 dgrp_t i;
JP Abgralle0ed7402014-03-19 19:08:39 -0700319 int num;
320 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000321
JP Abgralle0ed7402014-03-19 19:08:39 -0700322 ext2fs_numeric_progress_init(fs, &progress,
323 _("Writing inode tables: "),
324 fs->group_desc_count);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000325
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326 for (i = 0; i < fs->group_desc_count; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700327 ext2fs_numeric_progress_update(fs, &progress, i);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400328
JP Abgralle0ed7402014-03-19 19:08:39 -0700329 blk = ext2fs_inode_table_loc(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000330 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000331
JP Abgralle0ed7402014-03-19 19:08:39 -0700332 if (lazy_flag)
333 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
334 ext2fs_bg_itable_unused(fs, i)) *
335 EXT2_INODE_SIZE(fs->super),
336 EXT2_BLOCK_SIZE(fs->super));
Eric Sandeen6fcd6f82010-08-20 16:41:14 -0500337 if (!lazy_flag || itable_zeroed) {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500338 /* The kernel doesn't need to zero the itable blocks */
JP Abgralle0ed7402014-03-19 19:08:39 -0700339 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400340 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000341 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700342 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400343 if (retval) {
344 fprintf(stderr, _("\nCould not write %d "
JP Abgralle0ed7402014-03-19 19:08:39 -0700345 "blocks in inode table starting at %llu: %s\n"),
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400346 num, blk, error_message(retval));
347 exit(1);
348 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000349 if (sync_kludge) {
350 if (sync_kludge == 1)
351 sync();
352 else if ((i % sync_kludge) == 0)
353 sync();
354 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000355 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700356 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
357 ext2fs_numeric_progress_close(fs, &progress,
358 _("done \n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359}
360
361static void create_root_dir(ext2_filsys fs)
362{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400363 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000364 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365
366 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
367 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700368 com_err("ext2fs_mkdir", retval, "%s",
369 _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 exit(1);
371 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700372 if (root_uid != 0 || root_gid != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000373 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
374 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700375 com_err("ext2fs_read_inode", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000376 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000377 exit(1);
378 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700379
380 inode.i_uid = root_uid;
381 ext2fs_set_i_uid_high(inode, root_uid >> 16);
382 inode.i_gid = root_gid;
383 ext2fs_set_i_gid_high(inode, root_gid >> 16);
384
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500385 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000386 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700387 com_err("ext2fs_write_inode", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000388 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000389 exit(1);
390 }
391 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392}
393
394static void create_lost_and_found(ext2_filsys fs)
395{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400396 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000398 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399 const char *name = "lost+found";
400 int i;
401
Theodore Ts'o6a525062001-12-24 09:40:00 -0500402 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
404 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700405 com_err("ext2fs_mkdir", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000406 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407 exit(1);
408 }
409
410 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
411 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700412 com_err("ext2_lookup", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000413 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 exit(1);
415 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400416
Theodore Ts'o3839e651997-04-26 13:21:57 +0000417 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100418 /* Ensure that lost+found is at least 2 blocks, so we always
419 * test large empty blocks for big-block filesystems. */
420 if ((lpf_size += fs->blocksize) >= 16*1024 &&
421 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000422 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 retval = ext2fs_expand_dir(fs, ino);
424 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700425 com_err("ext2fs_expand_dir", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000426 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 exit(1);
428 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500429 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430}
431
432static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
433{
434 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400435
JP Abgralle0ed7402014-03-19 19:08:39 -0700436 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400437 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 retval = ext2fs_update_bb_inode(fs, bb_list);
439 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700440 com_err("ext2fs_update_bb_inode", retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000441 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 exit(1);
443 }
444
445}
446
447static void reserve_inodes(ext2_filsys fs)
448{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000449 ext2_ino_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400451 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
452 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 ext2fs_mark_ib_dirty(fs);
454}
455
Theodore Ts'o756df352002-03-07 20:52:12 -0500456#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500457#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500458#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500459
Theodore Ts'o04a96852001-08-30 21:55:26 -0400460static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000461{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400462 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000463 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500464 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000465
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400466 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600467 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500468 printf(_("Out of memory erasing sectors %d-%d\n"),
469 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600470 exit(1);
471 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500472
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500473 if (sect == 0) {
474 /* Check for a BSD disklabel, and don't erase it if so */
JP Abgralle0ed7402014-03-19 19:08:39 -0700475 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500476 if (retval)
477 fprintf(stderr,
478 _("Warning: could not read block 0: %s\n"),
479 error_message(retval));
480 else {
481 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
482 if ((*magic == BSD_DISKMAGIC) ||
483 (*magic == BSD_MAGICDISK))
484 return;
485 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500486 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500487
Theodore Ts'o70988102002-07-14 08:00:00 -0400488 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000489 io_channel_set_blksize(fs->io, 512);
JP Abgralle0ed7402014-03-19 19:08:39 -0700490 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000491 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400492 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000493 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500494 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
495 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000496}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000497
498static void create_journal_dev(ext2_filsys fs)
499{
JP Abgralle0ed7402014-03-19 19:08:39 -0700500 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000501 errcode_t retval;
502 char *buf;
JP Abgralle0ed7402014-03-19 19:08:39 -0700503 blk64_t blk, err_blk;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530504 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000505
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000506 retval = ext2fs_create_journal_superblock(fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700507 ext2fs_blocks_count(fs->super), 0, &buf);
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000508 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700509 com_err("create_journal_dev", retval, "%s",
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000510 _("while initializing journal superblock"));
511 exit(1);
512 }
Andreas Dilger6c546892011-06-11 12:17:29 -0400513
JP Abgralle0ed7402014-03-19 19:08:39 -0700514 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
515 goto write_superblock;
516
517 ext2fs_numeric_progress_init(fs, &progress,
518 _("Zeroing journal device: "),
519 ext2fs_blocks_count(fs->super));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530520 blk = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700521 count = ext2fs_blocks_count(fs->super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530522 while (count > 0) {
523 if (count > 1024)
524 c = 1024;
525 else
526 c = count;
JP Abgralle0ed7402014-03-19 19:08:39 -0700527 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530528 if (retval) {
529 com_err("create_journal_dev", retval,
530 _("while zeroing journal device "
JP Abgralle0ed7402014-03-19 19:08:39 -0700531 "(block %llu, count %d)"),
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530532 err_blk, err_count);
533 exit(1);
534 }
535 blk += c;
536 count -= c;
JP Abgralle0ed7402014-03-19 19:08:39 -0700537 ext2fs_numeric_progress_update(fs, &progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000538 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700539 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000540
JP Abgralle0ed7402014-03-19 19:08:39 -0700541 ext2fs_numeric_progress_close(fs, &progress, NULL);
542write_superblock:
543 retval = io_channel_write_blk64(fs->io,
544 fs->super->s_first_data_block+1,
545 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000546 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700547 com_err("create_journal_dev", retval, "%s",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000548 _("while writing journal superblock"));
549 exit(1);
550 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000551}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000552
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553static void show_stats(ext2_filsys fs)
554{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000555 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000556 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500557 char *os;
JP Abgralle0ed7402014-03-19 19:08:39 -0700558 blk64_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500559 dgrp_t i;
560 int need, col_left;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400561
JP Abgralle0ed7402014-03-19 19:08:39 -0700562 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
563 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
564 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000565
566 memset(buf, 0, sizeof(buf));
567 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000568 printf(_("Filesystem label=%s\n"), buf);
JP Abgralle0ed7402014-03-19 19:08:39 -0700569 os = e2p_os2string(fs->super->s_creator_os);
570 if (os)
571 printf(_("OS type: %s\n"), os);
Theodore Ts'o63253942005-03-19 01:13:22 -0500572 free(os);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000573 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000574 s->s_log_block_size);
JP Abgralle0ed7402014-03-19 19:08:39 -0700575 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
576 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
577 printf(_("Cluster size=%u (log=%u)\n"),
578 fs->blocksize << fs->cluster_ratio_bits,
579 s->s_log_cluster_size);
580 else
581 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
582 s->s_log_cluster_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -0500583 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
584 s->s_raid_stride, s->s_raid_stripe_width);
JP Abgralle0ed7402014-03-19 19:08:39 -0700585 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
586 ext2fs_blocks_count(s));
587 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
588 ext2fs_r_blocks_count(s),
589 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000590 printf(_("First data block=%u\n"), s->s_first_data_block);
JP Abgralle0ed7402014-03-19 19:08:39 -0700591 if (root_uid != 0 || root_gid != 0)
592 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500593 if (s->s_reserved_gdt_blocks)
594 printf(_("Maximum filesystem blocks=%lu\n"),
595 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200596 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000597 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000598 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000599 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000600 printf(_("%u block group\n"), fs->group_desc_count);
JP Abgralle0ed7402014-03-19 19:08:39 -0700601 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
602 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
603 printf(_("%u blocks per group, %u clusters per group\n"),
604 s->s_blocks_per_group, s->s_clusters_per_group);
605 else
606 printf(_("%u blocks per group, %u fragments per group\n"),
607 s->s_blocks_per_group, s->s_clusters_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000608 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609
610 if (fs->group_desc_count == 1) {
611 printf("\n");
612 return;
613 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500614
JP Abgralle0ed7402014-03-19 19:08:39 -0700615 printf("%s", _("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 group_block = s->s_first_data_block;
617 col_left = 0;
618 for (i = 1; i < fs->group_desc_count; i++) {
619 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000620 if (!ext2fs_bg_has_super(fs, i))
621 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000622 if (i != 1)
623 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000624 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000625 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000627 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000629 col_left -= need;
JP Abgralle0ed7402014-03-19 19:08:39 -0700630 printf("%llu", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 }
632 printf("\n\n");
633}
634
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000635/*
636 * Set the S_CREATOR_OS field. Return true if OS is known,
637 * otherwise, 0.
638 */
639static int set_os(struct ext2_super_block *sb, char *os)
640{
641 if (isdigit (*os))
642 sb->s_creator_os = atoi (os);
643 else if (strcasecmp(os, "linux") == 0)
644 sb->s_creator_os = EXT2_OS_LINUX;
645 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
646 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500647 else if (strcasecmp(os, "freebsd") == 0)
648 sb->s_creator_os = EXT2_OS_FREEBSD;
649 else if (strcasecmp(os, "lites") == 0)
650 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000651 else
652 return 0;
653 return 1;
654}
655
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000656#define PATH_SET "PATH=/sbin"
657
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400658static void parse_extended_opts(struct ext2_super_block *param,
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500659 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000660{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400661 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000662 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500663 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000664
665 len = strlen(opts);
666 buf = malloc(len+1);
667 if (!buf) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700668 fprintf(stderr, "%s",
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500669 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000670 exit(1);
671 }
672 strcpy(buf, opts);
673 for (token = buf; token && *token; token = next) {
674 p = strchr(token, ',');
675 next = 0;
676 if (p) {
677 *p = 0;
678 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500679 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000680 arg = strchr(token, '=');
681 if (arg) {
682 *arg = 0;
683 arg++;
684 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700685 if (strcmp(token, "desc-size") == 0 ||
686 strcmp(token, "desc_size") == 0) {
687 int desc_size;
688
689 if (!(fs_param.s_feature_incompat &
690 EXT4_FEATURE_INCOMPAT_64BIT)) {
691 fprintf(stderr,
692 _("%s requires '-O 64bit'\n"), token);
693 r_usage++;
694 continue;
695 }
696 if (param->s_reserved_gdt_blocks != 0) {
697 fprintf(stderr,
698 _("'%s' must be before 'resize=%u'\n"),
699 token, param->s_reserved_gdt_blocks);
700 r_usage++;
701 continue;
702 }
703 if (!arg) {
704 r_usage++;
705 badopt = token;
706 continue;
707 }
708 desc_size = strtoul(arg, &p, 0);
709 if (*p || (desc_size & (desc_size - 1))) {
710 fprintf(stderr,
711 _("Invalid desc_size: '%s'\n"), arg);
712 r_usage++;
713 continue;
714 }
715 param->s_desc_size = desc_size;
716 } else if (strcmp(token, "mmp_update_interval") == 0) {
717 if (!arg) {
718 r_usage++;
719 badopt = token;
720 continue;
721 }
722 param->s_mmp_update_interval = strtoul(arg, &p, 0);
723 if (*p) {
724 fprintf(stderr,
725 _("Invalid mmp_update_interval: %s\n"),
726 arg);
727 r_usage++;
728 continue;
729 }
730 } else if (strcmp(token, "stride") == 0) {
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000731 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500732 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500733 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000734 continue;
735 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500736 param->s_raid_stride = strtoul(arg, &p, 0);
JP Abgralle0ed7402014-03-19 19:08:39 -0700737 if (*p) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000738 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400739 _("Invalid stride parameter: %s\n"),
740 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500741 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000742 continue;
743 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500744 } else if (strcmp(token, "stripe-width") == 0 ||
745 strcmp(token, "stripe_width") == 0) {
746 if (!arg) {
747 r_usage++;
748 badopt = token;
749 continue;
750 }
751 param->s_raid_stripe_width = strtoul(arg, &p, 0);
JP Abgralle0ed7402014-03-19 19:08:39 -0700752 if (*p) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500753 fprintf(stderr,
754 _("Invalid stripe-width parameter: %s\n"),
755 arg);
756 r_usage++;
757 continue;
758 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500759 } else if (!strcmp(token, "resize")) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700760 blk64_t resize;
761 unsigned long bpg, rsv_groups;
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500762 unsigned long group_desc_count, desc_blocks;
763 unsigned int gdpb, blocksize;
764 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500765
766 if (!arg) {
767 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500768 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500769 continue;
770 }
771
JP Abgralle0ed7402014-03-19 19:08:39 -0700772 resize = parse_num_blocks2(arg,
773 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500774
775 if (resize == 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400776 fprintf(stderr,
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500777 _("Invalid resize parameter: %s\n"),
778 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500779 r_usage++;
780 continue;
781 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700782 if (resize <= ext2fs_blocks_count(param)) {
783 fprintf(stderr, "%s",
Theodore Ts'of37ab682005-05-05 23:15:55 -0400784 _("The resize maximum must be greater "
785 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500786 r_usage++;
787 continue;
788 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500789
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500790 blocksize = EXT2_BLOCK_SIZE(param);
791 bpg = param->s_blocks_per_group;
792 if (!bpg)
793 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200794 gdpb = EXT2_DESC_PER_BLOCK(param);
JP Abgralle0ed7402014-03-19 19:08:39 -0700795 group_desc_count = (__u32) ext2fs_div64_ceil(
796 ext2fs_blocks_count(param), bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500797 desc_blocks = (group_desc_count +
798 gdpb - 1) / gdpb;
JP Abgralle0ed7402014-03-19 19:08:39 -0700799 rsv_groups = ext2fs_div64_ceil(resize, bpg);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400800 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500801 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500802 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500803 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
804
805 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400806 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700807 fprintf(stderr, "%s",
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400808 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400809 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400810 exit(1);
811 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500812 param->s_feature_compat |=
813 EXT2_FEATURE_COMPAT_RESIZE_INODE;
814
815 param->s_reserved_gdt_blocks = rsv_gdb;
816 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500817 } else if (!strcmp(token, "test_fs")) {
818 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400819 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400820 if (arg)
821 lazy_itable_init = strtoul(arg, &p, 0);
822 else
823 lazy_itable_init = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700824 } else if (!strcmp(token, "lazy_journal_init")) {
825 if (arg)
826 journal_flags |= strtoul(arg, &p, 0) ?
827 EXT2_MKJOURNAL_LAZYINIT : 0;
828 else
829 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
830 } else if (!strcmp(token, "root_owner")) {
831 if (arg) {
832 root_uid = strtoul(arg, &p, 0);
833 if (*p != ':') {
834 fprintf(stderr,
835 _("Invalid root_owner: '%s'\n"),
836 arg);
837 r_usage++;
838 continue;
839 }
840 p++;
841 root_gid = strtoul(p, &p, 0);
842 if (*p) {
843 fprintf(stderr,
844 _("Invalid root_owner: '%s'\n"),
845 arg);
846 r_usage++;
847 continue;
848 }
849 } else {
850 root_uid = getuid();
851 root_gid = getgid();
852 }
Lukas Czerner0bc85df2010-11-18 14:38:39 +0100853 } else if (!strcmp(token, "discard")) {
854 discard = 1;
855 } else if (!strcmp(token, "nodiscard")) {
856 discard = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700857 } else if (!strcmp(token, "quotatype")) {
858 if (!arg) {
859 r_usage++;
860 badopt = token;
861 continue;
862 }
863 if (!strncmp(arg, "usr", 3)) {
864 quotatype = 0;
865 } else if (!strncmp(arg, "grp", 3)) {
866 quotatype = 1;
867 } else {
868 fprintf(stderr,
869 _("Invalid quotatype parameter: %s\n"),
870 arg);
871 r_usage++;
872 continue;
873 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500874 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500875 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500876 badopt = token;
877 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000878 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500879 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500880 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400881 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000882 "and may take an argument which\n"
883 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400884 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500885 "\tstride=<RAID per-disk data chunk in blocks>\n"
886 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400887 "\tresize=<resize maximum size in blocks>\n"
888 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
JP Abgralle0ed7402014-03-19 19:08:39 -0700889 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
890 "\troot_uid=<uid of root directory>\n"
891 "\troot_gid=<gid of root directory>\n"
Lukas Czerner0bc85df2010-11-18 14:38:39 +0100892 "\ttest_fs\n"
893 "\tdiscard\n"
JP Abgralle0ed7402014-03-19 19:08:39 -0700894 "\tnodiscard\n"
895 "\tquotatype=<usr OR grp>\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400896 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400897 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000898 exit(1);
899 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500900 if (param->s_raid_stride &&
901 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
902 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
903 "multiple of stride %u.\n\n"),
904 param->s_raid_stripe_width, param->s_raid_stride);
905
Brian Behlendorf21400382007-05-31 11:30:47 -0400906 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400907}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000908
Theodore Ts'o896938d1999-10-23 01:04:50 +0000909static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500910 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400911 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500912 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400913 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500914 EXT2_FEATURE_COMPAT_EXT_ATTR,
915 /* Incompat */
916 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500917 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400918 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500919 EXT2_FEATURE_INCOMPAT_META_BG|
JP Abgralle0ed7402014-03-19 19:08:39 -0700920 EXT4_FEATURE_INCOMPAT_FLEX_BG|
921 EXT4_FEATURE_INCOMPAT_MMP |
922 EXT4_FEATURE_INCOMPAT_64BIT,
Theodore Ts'o558df542008-02-27 15:01:19 -0500923 /* R/O compat */
924 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400925 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
926 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
927 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500928 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
JP Abgralle0ed7402014-03-19 19:08:39 -0700929 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
930 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
931#ifdef CONFIG_QUOTA
932 EXT4_FEATURE_RO_COMPAT_QUOTA|
933#endif
934 0
Theodore Ts'o896938d1999-10-23 01:04:50 +0000935};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000936
937
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500938static void syntax_err_report(const char *filename, long err, int line_num)
939{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400940 fprintf(stderr,
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500941 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
942 filename, line_num, error_message(err));
943 exit(1);
944}
945
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200946static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500947
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400948static void edit_feature(const char *str, __u32 *compat_array)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500949{
950 if (!str)
951 return;
952
953 if (e2p_edit_feature(str, compat_array, ok_features)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400954 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500955 str);
956 exit(1);
957 }
958}
959
JP Abgralle0ed7402014-03-19 19:08:39 -0700960static void edit_mntopts(const char *str, __u32 *mntopts)
961{
962 if (!str)
963 return;
964
965 if (e2p_edit_mntopts(str, mntopts, ~0)) {
966 fprintf(stderr, _("Invalid mount option set: %s\n"),
967 str);
968 exit(1);
969 }
970}
971
Theodore Ts'o3d438362008-02-19 08:32:58 -0500972struct str_list {
973 char **list;
974 int num;
975 int max;
976};
977
978static errcode_t init_list(struct str_list *sl)
979{
980 sl->num = 0;
981 sl->max = 0;
982 sl->list = malloc((sl->max+1) * sizeof(char *));
983 if (!sl->list)
984 return ENOMEM;
985 sl->list[0] = 0;
986 return 0;
987}
988
989static errcode_t push_string(struct str_list *sl, const char *str)
990{
991 char **new_list;
992
993 if (sl->num >= sl->max) {
994 sl->max += 2;
995 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
996 if (!new_list)
997 return ENOMEM;
998 sl->list = new_list;
999 }
1000 sl->list[sl->num] = malloc(strlen(str)+1);
1001 if (sl->list[sl->num] == 0)
1002 return ENOMEM;
1003 strcpy(sl->list[sl->num], str);
1004 sl->num++;
1005 sl->list[sl->num] = 0;
1006 return 0;
1007}
1008
1009static void print_str_list(char **list)
1010{
1011 char **cpp;
1012
1013 for (cpp = list; *cpp; cpp++) {
1014 printf("'%s'", *cpp);
1015 if (cpp[1])
1016 fputs(", ", stdout);
1017 }
1018 fputc('\n', stdout);
1019}
1020
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001021/*
1022 * Return TRUE if the profile has the given subsection
1023 */
JP Abgralle0ed7402014-03-19 19:08:39 -07001024static int profile_has_subsection(profile_t prof, const char *section,
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001025 const char *subsection)
1026{
1027 void *state;
1028 const char *names[4];
1029 char *name;
1030 int ret = 0;
1031
1032 names[0] = section;
1033 names[1] = subsection;
1034 names[2] = 0;
1035
JP Abgralle0ed7402014-03-19 19:08:39 -07001036 if (profile_iterator_create(prof, names,
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001037 PROFILE_ITER_LIST_SECTION |
1038 PROFILE_ITER_RELATIONS_ONLY, &state))
1039 return 0;
1040
1041 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1042 free(name);
1043 ret = 1;
1044 }
1045
1046 profile_iterator_free(&state);
1047 return ret;
1048}
1049
Theodore Ts'o3d438362008-02-19 08:32:58 -05001050static char **parse_fs_type(const char *fs_type,
1051 const char *usage_types,
JP Abgralle0ed7402014-03-19 19:08:39 -07001052 struct ext2_super_block *sb,
1053 blk64_t fs_blocks_count,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001054 char *progname)
1055{
1056 const char *ext_type = 0;
1057 char *parse_str;
1058 char *profile_type = 0;
1059 char *cp, *t;
1060 const char *size_type;
1061 struct str_list list;
JP Abgralle0ed7402014-03-19 19:08:39 -07001062 unsigned long long meg;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001063 int is_hurd = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001064
1065 if (init_list(&list))
1066 return 0;
1067
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001068 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1069 !strcasecmp(creator_os, "hurd")))
1070 is_hurd = 1;
1071
Theodore Ts'o3d438362008-02-19 08:32:58 -05001072 if (fs_type)
1073 ext_type = fs_type;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001074 else if (is_hurd)
1075 ext_type = "ext2";
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001076 else if (!strcmp(program_name, "mke3fs"))
1077 ext_type = "ext3";
JP Abgralle0ed7402014-03-19 19:08:39 -07001078 else if (!strcmp(program_name, "mke4fs"))
1079 ext_type = "ext4";
Theodore Ts'o3d438362008-02-19 08:32:58 -05001080 else if (progname) {
1081 ext_type = strrchr(progname, '/');
1082 if (ext_type)
1083 ext_type++;
1084 else
1085 ext_type = progname;
1086
1087 if (!strncmp(ext_type, "mkfs.", 5)) {
1088 ext_type += 5;
1089 if (ext_type[0] == 0)
1090 ext_type = 0;
1091 } else
1092 ext_type = 0;
1093 }
1094
1095 if (!ext_type) {
1096 profile_get_string(profile, "defaults", "fs_type", 0,
1097 "ext2", &profile_type);
1098 ext_type = profile_type;
1099 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1100 ext_type = "ext3";
1101 }
1102
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001103
1104 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1105 strcmp(ext_type, "ext2")) {
1106 printf(_("\nYour mke2fs.conf file does not define the "
1107 "%s filesystem type.\n"), ext_type);
1108 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1109 !strcmp(ext_type, "ext4dev")) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001110 printf("%s", _("You probably need to install an "
1111 "updated mke2fs.conf file.\n\n"));
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001112 }
1113 if (!force) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001114 printf("%s", _("Aborting...\n"));
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001115 exit(1);
Theodore Ts'obad89b22008-08-22 21:57:29 -04001116 }
1117 }
1118
JP Abgralle0ed7402014-03-19 19:08:39 -07001119 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1120 if (fs_blocks_count < 3 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001121 size_type = "floppy";
JP Abgralle0ed7402014-03-19 19:08:39 -07001122 else if (fs_blocks_count < 512 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001123 size_type = "small";
JP Abgralle0ed7402014-03-19 19:08:39 -07001124 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
JP Abgrall65f0aab2014-03-06 13:50:20 -08001125 size_type = "default";
JP Abgralle0ed7402014-03-19 19:08:39 -07001126 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1127 size_type = "big";
1128 else
1129 size_type = "huge";
Theodore Ts'o3d438362008-02-19 08:32:58 -05001130
1131 if (!usage_types)
1132 usage_types = size_type;
1133
JP Abgralle0ed7402014-03-19 19:08:39 -07001134 parse_str = malloc(strlen(usage_types)+1);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001135 if (!parse_str) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001136 free(profile_type);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001137 free(list.list);
1138 return 0;
1139 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001140 strcpy(parse_str, usage_types);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001141
1142 if (ext_type)
1143 push_string(&list, ext_type);
1144 cp = parse_str;
1145 while (1) {
1146 t = strchr(cp, ',');
1147 if (t)
1148 *t = '\0';
1149
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001150 if (*cp) {
Theodore Ts'o0f7479b2010-12-22 18:31:36 -05001151 if (profile_has_subsection(profile, "fs_types", cp))
1152 push_string(&list, cp);
1153 else if (strcmp(cp, "default") != 0)
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001154 fprintf(stderr,
1155 _("\nWarning: the fs_type %s is not "
Theodore Ts'o0f7479b2010-12-22 18:31:36 -05001156 "defined in mke2fs.conf\n\n"),
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001157 cp);
Theodore Ts'o2ee45442010-12-01 18:28:35 -05001158 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001159 if (t)
1160 cp = t+1;
JP Abgralle0ed7402014-03-19 19:08:39 -07001161 else
Theodore Ts'o3d438362008-02-19 08:32:58 -05001162 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001163 }
1164 free(parse_str);
Jim Meyering45e338f2009-02-23 18:07:50 +01001165 free(profile_type);
Theodore Ts'o7f5601e2008-07-10 09:24:25 -04001166 if (is_hurd)
1167 push_string(&list, "hurd");
Theodore Ts'o3d438362008-02-19 08:32:58 -05001168 return (list.list);
1169}
1170
JP Abgralle0ed7402014-03-19 19:08:39 -07001171static char *get_string_from_profile(char **types, const char *opt,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001172 const char *def_val)
1173{
1174 char *ret = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001175 int i;
1176
JP Abgralle0ed7402014-03-19 19:08:39 -07001177 for (i=0; types[i]; i++);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001178 for (i-=1; i >=0 ; i--) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001179 profile_get_string(profile, "fs_types", types[i],
Theodore Ts'o3d438362008-02-19 08:32:58 -05001180 opt, 0, &ret);
1181 if (ret)
1182 return ret;
1183 }
1184 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1185 return (ret);
1186}
1187
JP Abgralle0ed7402014-03-19 19:08:39 -07001188static int get_int_from_profile(char **types, const char *opt, int def_val)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001189{
1190 int ret;
1191 char **cpp;
1192
1193 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
JP Abgralle0ed7402014-03-19 19:08:39 -07001194 for (cpp = types; *cpp; cpp++)
Theodore Ts'o3d438362008-02-19 08:32:58 -05001195 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1196 return ret;
1197}
1198
JP Abgralle0ed7402014-03-19 19:08:39 -07001199static double get_double_from_profile(char **types, const char *opt,
1200 double def_val)
1201{
1202 double ret;
1203 char **cpp;
1204
1205 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1206 for (cpp = types; *cpp; cpp++)
1207 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1208 return ret;
1209}
1210
1211static int get_bool_from_profile(char **types, const char *opt, int def_val)
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001212{
1213 int ret;
1214 char **cpp;
1215
1216 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
JP Abgralle0ed7402014-03-19 19:08:39 -07001217 for (cpp = types; *cpp; cpp++)
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001218 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1219 return ret;
1220}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001221
Theodore Ts'od48bc602007-07-04 14:10:46 -04001222extern const char *mke2fs_default_profile;
1223static const char *default_files[] = { "<default>", 0 };
1224
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001225#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1226/*
1227 * Sets the geometry of a device (stripe/stride), and returns the
1228 * device's alignment offset, if any, or a negative error.
1229 */
Theodore Ts'o1599b472010-11-22 10:50:42 -05001230static int get_device_geometry(const char *file,
1231 struct ext2_super_block *fs_param,
1232 int psector_size)
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001233{
1234 int rc = -1;
1235 int blocksize;
1236 blkid_probe pr;
1237 blkid_topology tp;
1238 unsigned long min_io;
1239 unsigned long opt_io;
Eric Sandeen13b0b122010-01-23 21:50:45 -06001240 struct stat statbuf;
1241
1242 /* Nothing to do for a regular file */
1243 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1244 return 0;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001245
1246 pr = blkid_new_probe_from_filename(file);
1247 if (!pr)
1248 goto out;
1249
1250 tp = blkid_probe_get_topology(pr);
1251 if (!tp)
1252 goto out;
1253
1254 min_io = blkid_topology_get_minimum_io_size(tp);
1255 opt_io = blkid_topology_get_optimal_io_size(tp);
1256 blocksize = EXT2_BLOCK_SIZE(fs_param);
Theodore Ts'o1599b472010-11-22 10:50:42 -05001257 if ((min_io == 0) && (psector_size > blocksize))
1258 min_io = psector_size;
1259 if ((opt_io == 0) && min_io)
1260 opt_io = min_io;
1261 if ((opt_io == 0) && (psector_size > blocksize))
1262 opt_io = psector_size;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001263
JP Abgralle0ed7402014-03-19 19:08:39 -07001264 /* setting stripe/stride to blocksize is pointless */
1265 if (min_io > blocksize)
1266 fs_param->s_raid_stride = min_io / blocksize;
1267 if (opt_io > blocksize)
1268 fs_param->s_raid_stripe_width = opt_io / blocksize;
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001269
1270 rc = blkid_topology_get_alignment_offset(tp);
1271out:
1272 blkid_free_probe(pr);
1273 return rc;
1274}
1275#endif
1276
Theodore Ts'o3839e651997-04-26 13:21:57 +00001277static void PRS(int argc, char *argv[])
1278{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001279 int b, c;
JP Abgralle0ed7402014-03-19 19:08:39 -07001280 int cluster_size = 0;
Eric Sandeen79e62402008-07-06 18:36:56 -04001281 char *tmp, **cpp;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001282 int blocksize = 0;
1283 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001284 int inode_size = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001285 unsigned long flex_bg_size = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001286 double reserved_ratio = -1.0;
Theodore Ts'obb1158b2010-05-17 22:31:45 -04001287 int lsector_size = 0, psector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001288 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001289 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001290 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001291 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001292 char * extended_opts = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001293 char * fs_type = 0;
1294 char * usage_types = 0;
1295 blk64_t dev_size;
1296 /*
1297 * NOTE: A few words about fs_blocks_count and blocksize:
1298 *
1299 * Initially, blocksize is set to zero, which implies 1024.
1300 * If -b is specified, blocksize is updated to the user's value.
1301 *
1302 * Next, the device size or the user's "blocks" command line argument
1303 * is used to set fs_blocks_count; the units are blocksize.
1304 *
1305 * Later, if blocksize hasn't been set and the profile specifies a
1306 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1307 * appropriately. Note the change in units!
1308 *
1309 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1310 */
1311 blk64_t fs_blocks_count = 0;
Theodore Ts'o756df352002-03-07 20:52:12 -05001312#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001313 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001314#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001315 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001316 int s_opt = -1, r_opt = -1;
1317 char *fs_features = 0;
1318 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001319 char *newpath;
1320 int pathlen = sizeof(PATH_SET) + 1;
1321
1322 if (oldpath)
1323 pathlen += strlen(oldpath);
1324 newpath = malloc(pathlen);
JP Abgralle0ed7402014-03-19 19:08:39 -07001325 if (!newpath) {
1326 fprintf(stderr, "%s",
1327 _("Couldn't allocate memory for new PATH.\n"));
1328 exit(1);
1329 }
Theodore Ts'o642935c2006-11-14 23:38:17 -05001330 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001331
Theodore Ts'o3839e651997-04-26 13:21:57 +00001332 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001333 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001334 strcat (newpath, ":");
1335 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001336 }
1337 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001338
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001339 tmp = getenv("MKE2FS_SYNC");
1340 if (tmp)
1341 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001342
1343 /* Determine the system page size if possible */
1344#ifdef HAVE_SYSCONF
1345#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1346#define _SC_PAGESIZE _SC_PAGE_SIZE
1347#endif
1348#ifdef _SC_PAGESIZE
1349 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001350 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001351 sys_page_size = sysval;
1352#endif /* _SC_PAGESIZE */
1353#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001354
1355 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1356 config_fn[0] = tmp;
1357 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001358 retval = profile_init(config_fn, &profile);
1359 if (retval == ENOENT) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001360 retval = profile_init(default_files, &profile);
1361 if (retval)
1362 goto profile_error;
1363 retval = profile_set_default(profile, mke2fs_default_profile);
1364 if (retval)
1365 goto profile_error;
1366 } else if (retval) {
1367profile_error:
1368 fprintf(stderr, _("Couldn't init profile successfully"
1369 " (error: %ld).\n"), retval);
1370 exit(1);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001371 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001372
Theodore Ts'o3839e651997-04-26 13:21:57 +00001373 setbuf(stdout, NULL);
1374 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001375 add_error_table(&et_ext2_error_table);
1376 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001377 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1378 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001379
Theodore Ts'o756df352002-03-07 20:52:12 -05001380#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001381 if (uname(&ut)) {
1382 perror("uname");
1383 exit(1);
1384 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001385 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001386 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001387 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001388#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001389
1390 if (argc && *argv) {
1391 program_name = get_progname(*argv);
1392
1393 /* If called as mkfs.ext3, create a journal inode */
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001394 if (!strcmp(program_name, "mkfs.ext3") ||
1395 !strcmp(program_name, "mke3fs"))
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001396 journal_size = -1;
1397 }
1398
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001399 while ((c = getopt (argc, argv,
JP Abgralle0ed7402014-03-19 19:08:39 -07001400 "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 +00001401 switch (c) {
1402 case 'b':
JP Abgralle0ed7402014-03-19 19:08:39 -07001403 blocksize = parse_num_blocks2(optarg, -1);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001404 b = (blocksize > 0) ? blocksize : -blocksize;
1405 if (b < EXT2_MIN_BLOCK_SIZE ||
JP Abgralle0ed7402014-03-19 19:08:39 -07001406 b > EXT2_MAX_BLOCK_SIZE) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001407 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001408 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001409 exit(1);
1410 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001411 if (blocksize > 4096)
1412 fprintf(stderr, _("Warning: blocksize %d not "
1413 "usable on most systems.\n"),
1414 blocksize);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001415 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001416 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001417 int_log2(blocksize >>
1418 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001419 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001420 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001421 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001422 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001423 case 'C':
1424 cluster_size = parse_num_blocks2(optarg, -1);
1425 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1426 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001427 com_err(program_name, 0,
JP Abgralle0ed7402014-03-19 19:08:39 -07001428 _("invalid cluster size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001429 optarg);
1430 exit(1);
1431 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001432 break;
1433 case 'D':
1434 direct_io = 1;
1435 break;
1436 case 'R':
1437 com_err(program_name, 0, "%s",
1438 _("'-R' is deprecated, use '-E' instead"));
1439 /* fallthrough */
1440 case 'E':
1441 extended_opts = optarg;
1442 break;
1443 case 'F':
1444 force++;
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001445 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001446 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001447 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001448 if (*tmp) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001449 com_err(program_name, 0, "%s",
1450 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001451 exit(1);
1452 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001453 if ((fs_param.s_blocks_per_group % 8) != 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001454 com_err(program_name, 0, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001455 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001456 exit(1);
1457 }
1458 break;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001459 case 'G':
1460 flex_bg_size = strtoul(optarg, &tmp, 0);
1461 if (*tmp) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001462 com_err(program_name, 0, "%s",
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001463 _("Illegal number for flex_bg size"));
1464 exit(1);
1465 }
Theodore Ts'od5314502010-05-10 21:49:58 -04001466 if (flex_bg_size < 1 ||
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001467 (flex_bg_size & (flex_bg_size-1)) != 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001468 com_err(program_name, 0, "%s",
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001469 _("flex_bg size must be a power of 2"));
1470 exit(1);
1471 }
1472 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001473 case 'i':
1474 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001475 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1476 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001477 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001478 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001479 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001480 optarg, EXT2_MIN_BLOCK_SIZE,
Tim Smalle447ba32010-07-30 20:43:11 -04001481 EXT2_MAX_BLOCK_SIZE * 1024);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001482 exit(1);
1483 }
1484 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001485 case 'I':
1486 inode_size = strtoul(optarg, &tmp, 0);
1487 if (*tmp) {
1488 com_err(program_name, 0,
1489 _("invalid inode size - %s"), optarg);
1490 exit(1);
1491 }
Andreas Dilger2a83b3c2013-06-15 18:47:30 -04001492 break;
1493 case 'j':
1494 if (!journal_size)
1495 journal_size = -1;
1496 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001497 case 'J':
1498 parse_journal_opts(optarg);
1499 break;
1500 case 'K':
1501 fprintf(stderr, "%s",
1502 _("Warning: -K option is deprecated and "
1503 "should not be used anymore. Use "
1504 "\'-E nodiscard\' extended option "
1505 "instead!\n"));
1506 discard = 0;
1507 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001508 case 'l':
JP Abgralle0ed7402014-03-19 19:08:39 -07001509 bad_blocks_filename = realloc(bad_blocks_filename,
1510 strlen(optarg) + 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001511 if (!bad_blocks_filename) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001512 com_err(program_name, ENOMEM, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001513 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001514 exit(1);
1515 }
1516 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001517 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001518 case 'L':
1519 volume_label = optarg;
1520 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001521 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001522 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o8d822452009-03-06 02:23:59 -05001523 if ( *tmp || reserved_ratio > 50 ||
1524 reserved_ratio < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001525 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001526 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001527 optarg);
1528 exit(1);
1529 }
1530 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001531 case 'M':
1532 mount_dir = optarg;
1533 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001534 case 'n':
1535 noaction++;
1536 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001537 case 'N':
1538 num_inodes = strtoul(optarg, &tmp, 0);
1539 if (*tmp) {
1540 com_err(program_name, 0,
1541 _("bad num inodes - %s"), optarg);
1542 exit(1);
1543 }
1544 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001545 case 'o':
1546 creator_os = optarg;
1547 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001548 case 'O':
1549 fs_features = optarg;
1550 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001551 case 'q':
1552 quiet = 1;
1553 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001554 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001555 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001556 if (*tmp) {
1557 com_err(program_name, 0,
1558 _("bad revision level - %s"), optarg);
1559 exit(1);
1560 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001561 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001562 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001563 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001564 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001565 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001566 case 'S':
1567 super_only = 1;
1568 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001569 case 't':
JP Abgralle0ed7402014-03-19 19:08:39 -07001570 if (fs_type) {
1571 com_err(program_name, 0, "%s",
1572 _("The -t option may only be used once"));
1573 exit(1);
1574 }
1575 fs_type = strdup(optarg);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001576 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001577 case 'T':
JP Abgralle0ed7402014-03-19 19:08:39 -07001578 if (usage_types) {
1579 com_err(program_name, 0, "%s",
1580 _("The -T option may only be used once"));
1581 exit(1);
1582 }
1583 usage_types = strdup(optarg);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001584 break;
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001585 case 'U':
1586 fs_uuid = optarg;
1587 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001588 case 'v':
1589 verbose = 1;
1590 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001591 case 'V':
1592 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001593 show_version_only++;
1594 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001595 default:
1596 usage();
1597 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001598 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001599 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001600 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001601 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001602
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001603 if (!quiet || show_version_only)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001604 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
Theodore Ts'o38798572003-04-16 15:29:39 -04001605 E2FSPROGS_DATE);
1606
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001607 if (show_version_only) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001608 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001609 error_message(EXT2_ET_BASE));
1610 exit(0);
1611 }
1612
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001613 /*
1614 * If there's no blocksize specified and there is a journal
1615 * device, use it to figure out the blocksize
1616 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001617 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001618 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001619 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001620
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001621#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001622 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1623 io_ptr = test_io_manager;
1624 test_io_backing_manager = unix_io_manager;
1625 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001626#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001627 io_ptr = unix_io_manager;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001628 retval = ext2fs_open(journal_device,
1629 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001630 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001631 if (retval) {
1632 com_err(program_name, retval,
1633 _("while trying to open journal device %s\n"),
1634 journal_device);
1635 exit(1);
1636 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001637 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001638 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001639 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001640 "minimum blocksize %d\n"), jfs->blocksize,
1641 -blocksize);
1642 exit(1);
1643 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001644 blocksize = jfs->blocksize;
Theodore Ts'of8df04b2008-07-06 20:57:17 -04001645 printf(_("Using journal device's blocksize: %d\n"), blocksize);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001646 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001647 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1648 ext2fs_close(jfs);
1649 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001650
JP Abgrall65f0aab2014-03-06 13:50:20 -08001651 if (optind < argc) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001652 fs_blocks_count = parse_num_blocks2(argv[optind++],
1653 fs_param.s_log_block_size);
1654 if (!fs_blocks_count) {
JP Abgrall65f0aab2014-03-06 13:50:20 -08001655 com_err(program_name, 0,
JP Abgralle0ed7402014-03-19 19:08:39 -07001656 _("invalid blocks '%s' on device '%s'"),
Eric Sandeen792cce32010-08-12 14:40:44 -04001657 argv[optind - 1], device_name);
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001658 exit(1);
1659 }
1660 }
1661 if (optind < argc)
1662 usage();
1663
Theodore Ts'o74becf31997-04-26 14:37:06 +00001664 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001665 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001666 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001667
JP Abgralle0ed7402014-03-19 19:08:39 -07001668 /* Determine the size of the device (if possible) */
1669 if (noaction && fs_blocks_count) {
1670 dev_size = fs_blocks_count;
JP Abgrall65f0aab2014-03-06 13:50:20 -08001671 retval = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001672 } else
JP Abgrall65f0aab2014-03-06 13:50:20 -08001673 retval = ext2fs_get_device_size2(device_name,
JP Abgralle0ed7402014-03-19 19:08:39 -07001674 EXT2_BLOCK_SIZE(&fs_param),
1675 &dev_size);
1676
Theodore Ts'oa789d841998-03-30 01:20:55 +00001677 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001678 com_err(program_name, retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001679 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001680 exit(1);
1681 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001682 if (!fs_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001683 if (retval == EXT2_ET_UNIMPLEMENTED) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001684 com_err(program_name, 0, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001685 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001686 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001687 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001688 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001689 } else {
1690 if (dev_size == 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001691 com_err(program_name, 0, "%s",
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001692 _("Device size reported to be zero. "
1693 "Invalid partition specified, or\n\t"
1694 "partition table wasn't reread "
1695 "after running fdisk, due to\n\t"
1696 "a modified partition being busy "
1697 "and in use. You may need to reboot\n\t"
1698 "to re-read your partition table.\n"
1699 ));
1700 exit(1);
1701 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001702 fs_blocks_count = dev_size;
Theodore Ts'o493024e2010-06-13 14:00:00 -04001703 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
JP Abgralle0ed7402014-03-19 19:08:39 -07001704 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1705 EXT2_BLOCK_SIZE(&fs_param))-1));
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001706 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001707 } else if (!force && (fs_blocks_count > dev_size)) {
1708 com_err(program_name, 0, "%s",
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001709 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001710 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001711 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001712
JP Abgralle0ed7402014-03-19 19:08:39 -07001713 if (!fs_type)
1714 profile_get_string(profile, "devices", device_name,
1715 "fs_type", 0, &fs_type);
1716 if (!usage_types)
1717 profile_get_string(profile, "devices", device_name,
1718 "usage_types", 0, &usage_types);
1719
1720 /*
1721 * We have the file system (or device) size, so we can now
1722 * determine the appropriate file system types so the fs can
1723 * be appropriately configured.
1724 */
1725 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1726 fs_blocks_count ? fs_blocks_count : dev_size,
1727 argv[0]);
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001728 if (!fs_types) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001729 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001730 exit(1);
1731 }
1732
1733 /* Figure out what features should be enabled */
1734
1735 tmp = NULL;
1736 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1737 tmp = get_string_from_profile(fs_types, "base_features",
1738 "sparse_super,filetype,resize_inode,dir_index");
1739 edit_feature(tmp, &fs_param.s_feature_compat);
1740 free(tmp);
1741
JP Abgralle0ed7402014-03-19 19:08:39 -07001742 /* And which mount options as well */
1743 tmp = get_string_from_profile(fs_types, "default_mntopts",
1744 "acl,user_xattr");
1745 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1746 if (tmp)
1747 free(tmp);
1748
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001749 for (cpp = fs_types; *cpp; cpp++) {
1750 tmp = NULL;
1751 profile_get_string(profile, "fs_types", *cpp,
1752 "features", "", &tmp);
1753 if (tmp && *tmp)
1754 edit_feature(tmp, &fs_param.s_feature_compat);
JP Abgralle0ed7402014-03-19 19:08:39 -07001755 if (tmp)
1756 free(tmp);
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001757 }
1758 tmp = get_string_from_profile(fs_types, "default_features",
1759 "");
1760 }
1761 edit_feature(fs_features ? fs_features : tmp,
1762 &fs_param.s_feature_compat);
JP Abgralle0ed7402014-03-19 19:08:39 -07001763 if (tmp)
1764 free(tmp);
Theodore Ts'o9b27e9c2010-12-22 18:22:40 -05001765
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001766 /* Get the hardware sector sizes, if available */
1767 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1768 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001769 com_err(program_name, retval, "%s",
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001770 _("while trying to determine hardware sector size"));
1771 exit(1);
1772 }
1773 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1774 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001775 com_err(program_name, retval, "%s",
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001776 _("while trying to determine physical sector size"));
1777 exit(1);
1778 }
1779
JP Abgralle0ed7402014-03-19 19:08:39 -07001780 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1781 if (tmp != NULL)
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001782 lsector_size = atoi(tmp);
JP Abgralle0ed7402014-03-19 19:08:39 -07001783 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1784 if (tmp != NULL)
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001785 psector_size = atoi(tmp);
1786
1787 /* Older kernels may not have physical/logical distinction */
1788 if (!psector_size)
1789 psector_size = lsector_size;
1790
1791 if (blocksize <= 0) {
1792 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1793
1794 if (use_bsize == -1) {
1795 use_bsize = sys_page_size;
1796 if ((linux_version_code < (2*65536 + 6*256)) &&
1797 (use_bsize > 4096))
1798 use_bsize = 4096;
1799 }
1800 if (lsector_size && use_bsize < lsector_size)
1801 use_bsize = lsector_size;
1802 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1803 use_bsize = -blocksize;
1804 blocksize = use_bsize;
JP Abgralle0ed7402014-03-19 19:08:39 -07001805 fs_blocks_count /= (blocksize / 1024);
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001806 } else {
1807 if (blocksize < lsector_size) { /* Impossible */
JP Abgralle0ed7402014-03-19 19:08:39 -07001808 com_err(program_name, EINVAL, "%s",
Darrick J. Wongd20403d2013-12-10 17:18:37 -08001809 _("while setting blocksize; too small "
1810 "for device\n"));
1811 exit(1);
1812 } else if ((blocksize < psector_size) &&
1813 (psector_size <= sys_page_size)) { /* Suboptimal */
1814 fprintf(stderr, _("Warning: specified blocksize %d is "
1815 "less than device physical sectorsize %d\n"),
1816 blocksize, psector_size);
1817 }
1818 }
1819
JP Abgralle0ed7402014-03-19 19:08:39 -07001820 fs_param.s_log_block_size =
1821 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1822
1823 /*
1824 * We now need to do a sanity check of fs_blocks_count for
1825 * 32-bit vs 64-bit block number support.
1826 */
1827 if ((fs_blocks_count > MAX_32_NUM) &&
1828 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1829 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1830 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1831 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1832 }
1833 if ((fs_blocks_count > MAX_32_NUM) &&
1834 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1835 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1836 "too big to be expressed\n\t"
1837 "in 32 bits using a blocksize of %d.\n"),
1838 program_name, fs_blocks_count, device_name,
1839 EXT2_BLOCK_SIZE(&fs_param));
1840 exit(1);
1841 }
1842
1843 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1844
1845 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1846 fs_types[0] = strdup("journal");
1847 fs_types[1] = 0;
1848 }
1849
1850 if (verbose) {
1851 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1852 print_str_list(fs_types);
1853 }
1854
1855 if (r_opt == EXT2_GOOD_OLD_REV &&
1856 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1857 fs_param.s_feature_ro_compat)) {
1858 fprintf(stderr, "%s", _("Filesystem features not supported "
1859 "with revision 0 filesystems\n"));
1860 exit(1);
1861 }
1862
1863 if (s_opt > 0) {
1864 if (r_opt == EXT2_GOOD_OLD_REV) {
1865 fprintf(stderr, "%s",
1866 _("Sparse superblocks not supported "
1867 "with revision 0 filesystems\n"));
1868 exit(1);
1869 }
1870 fs_param.s_feature_ro_compat |=
1871 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1872 } else if (s_opt == 0)
1873 fs_param.s_feature_ro_compat &=
1874 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1875
1876 if (journal_size != 0) {
1877 if (r_opt == EXT2_GOOD_OLD_REV) {
1878 fprintf(stderr, "%s", _("Journals not supported with "
1879 "revision 0 filesystems\n"));
1880 exit(1);
1881 }
1882 fs_param.s_feature_compat |=
1883 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1884 }
1885
1886 /* Get reserved_ratio from profile if not specified on cmd line. */
1887 if (reserved_ratio < 0.0) {
1888 reserved_ratio = get_double_from_profile(
1889 fs_types, "reserved_ratio", 5.0);
1890 if (reserved_ratio > 50 || reserved_ratio < 0) {
1891 com_err(program_name, 0,
1892 _("invalid reserved blocks percent - %lf"),
1893 reserved_ratio);
1894 exit(1);
1895 }
1896 }
1897
1898 if (fs_param.s_feature_incompat &
1899 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1900 reserved_ratio = 0;
1901 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1902 fs_param.s_feature_compat = 0;
1903 fs_param.s_feature_ro_compat = 0;
1904 }
1905
1906 /* Check the user's mkfs options for 64bit */
1907 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1908 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
1909 printf("%s", _("Extents MUST be enabled for a 64-bit "
1910 "filesystem. Pass -O extents to rectify.\n"));
1911 exit(1);
1912 }
1913
1914 /* Set first meta blockgroup via an environment variable */
1915 /* (this is mostly for debugging purposes) */
1916 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1917 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1918 fs_param.s_first_meta_bg = atoi(tmp);
1919 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1920 if (!cluster_size)
1921 cluster_size = get_int_from_profile(fs_types,
1922 "cluster_size",
1923 blocksize*16);
1924 fs_param.s_log_cluster_size =
1925 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1926 if (fs_param.s_log_cluster_size &&
1927 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
1928 com_err(program_name, 0, "%s",
1929 _("The cluster size may not be "
1930 "smaller than the block size.\n"));
1931 exit(1);
1932 }
1933 } else if (cluster_size) {
1934 com_err(program_name, 0, "%s",
1935 _("specifying a cluster size requires the "
1936 "bigalloc feature"));
1937 exit(1);
1938 } else
1939 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1940
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001941 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001942 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1943 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001944 if (inode_ratio < blocksize)
1945 inode_ratio = blocksize;
JP Abgralle0ed7402014-03-19 19:08:39 -07001946 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1947 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001948 }
1949
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001950#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
Theodore Ts'o1599b472010-11-22 10:50:42 -05001951 retval = get_device_geometry(device_name, &fs_param, psector_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001952 if (retval < 0) {
1953 fprintf(stderr,
Eric Sandeen13b0b122010-01-23 21:50:45 -06001954 _("warning: Unable to get device geometry for %s\n"),
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001955 device_name);
1956 } else if (retval) {
1957 printf(_("%s alignment is offset by %lu bytes.\n"),
1958 device_name, retval);
1959 printf(_("This may result in very poor performance, "
1960 "(re)-partitioning suggested.\n"));
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001961 }
1962#endif
1963
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001964 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001965
JP Abgralle0ed7402014-03-19 19:08:39 -07001966 /*
1967 * Initialize s_desc_size so that the parse_extended_opts()
1968 * can correctly handle "-E resize=NNN" if the 64-bit option
1969 * is set.
1970 */
1971 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
1972 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
1973
1974 /* This check should happen beyond the last assignment to blocksize */
1975 if (blocksize > sys_page_size) {
1976 if (!force) {
1977 com_err(program_name, 0,
1978 _("%d-byte blocks too big for system (max %d)"),
1979 blocksize, sys_page_size);
1980 proceed_question();
1981 }
1982 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1983 "(max %d), forced to continue\n"),
1984 blocksize, sys_page_size);
1985 }
1986
Theodore Ts'o210fd2c2010-10-01 10:47:38 -04001987 lazy_itable_init = 0;
1988 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1989 lazy_itable_init = 1;
1990
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001991 lazy_itable_init = get_bool_from_profile(fs_types,
Theodore Ts'o210fd2c2010-10-01 10:47:38 -04001992 "lazy_itable_init",
1993 lazy_itable_init);
Lukas Czerner7fe5ff32010-11-18 14:38:41 +01001994 discard = get_bool_from_profile(fs_types, "discard" , discard);
JP Abgralle0ed7402014-03-19 19:08:39 -07001995 journal_flags |= get_bool_from_profile(fs_types,
1996 "lazy_journal_init", 0) ?
1997 EXT2_MKJOURNAL_LAZYINIT : 0;
1998 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001999
Theodore Ts'o2d363582008-05-14 18:09:37 -04002000 /* Get options from profile */
2001 for (cpp = fs_types; *cpp; cpp++) {
2002 tmp = NULL;
2003 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2004 if (tmp && *tmp)
2005 parse_extended_opts(&fs_param, tmp);
Jim Meyering45e338f2009-02-23 18:07:50 +01002006 free(tmp);
Theodore Ts'o2d363582008-05-14 18:09:37 -04002007 }
2008
Theodore Ts'oc6a44132005-01-05 11:12:20 -05002009 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002010 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002011
JP Abgralle0ed7402014-03-19 19:08:39 -07002012 /* Can't support bigalloc feature without extents feature */
2013 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2014 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2015 com_err(program_name, 0, "%s",
2016 _("Can't support bigalloc feature without "
2017 "extents feature"));
2018 exit(1);
2019 }
2020
2021 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2022 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2023 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2024 "features are not compatible.\n"
2025 "They can not be both enabled "
2026 "simultaneously.\n"));
2027 exit(1);
2028 }
2029
2030 if (!quiet &&
2031 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2032 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2033 "still under development\n"
2034 "See https://ext4.wiki.kernel.org/"
2035 "index.php/Bigalloc for more information\n\n"));
2036
2037 if (!quiet &&
2038 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2039 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2040 "still under development\n"
2041 "See https://ext4.wiki.kernel.org/"
2042 "index.php/Quota for more information\n\n"));
2043
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002044 /* Since sparse_super is the default, we would only have a problem
2045 * here if it was explicitly disabled.
2046 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002047 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2048 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002049 com_err(program_name, 0, "%s",
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002050 _("reserved online resize blocks not supported "
2051 "on non-sparse filesystem"));
2052 exit(1);
2053 }
2054
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002055 if (fs_param.s_blocks_per_group) {
2056 if (fs_param.s_blocks_per_group < 256 ||
2057 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002058 com_err(program_name, 0, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002059 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00002060 exit(1);
2061 }
2062 }
2063
JP Abgralle0ed7402014-03-19 19:08:39 -07002064 /*
2065 * If the bigalloc feature is enabled, then the -g option will
2066 * specify the number of clusters per group.
2067 */
2068 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2069 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2070 fs_param.s_blocks_per_group = 0;
2071 }
2072
Theodore Ts'o3d438362008-02-19 08:32:58 -05002073 if (inode_size == 0)
2074 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Theodore Ts'o9ba40002008-04-22 08:27:01 -04002075 if (!flex_bg_size && (fs_param.s_feature_incompat &
2076 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2077 flex_bg_size = get_int_from_profile(fs_types,
2078 "flex_bg_size", 16);
2079 if (flex_bg_size) {
2080 if (!(fs_param.s_feature_incompat &
2081 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002082 com_err(program_name, 0, "%s",
Theodore Ts'o9ba40002008-04-22 08:27:01 -04002083 _("Flex_bg feature not enabled, so "
2084 "flex_bg size may not be specified"));
2085 exit(1);
2086 }
2087 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2088 }
Andreas Dilger067911a2006-07-15 22:08:20 -04002089
2090 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06002091 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002092 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06002093 inode_size & (inode_size - 1)) {
2094 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04002095 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06002096 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04002097 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06002098 exit(1);
2099 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002100 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06002101 }
2102
Eric Sandeenf3358642006-09-12 14:56:17 -04002103 /* Make sure number of inodes specified will fit in 32 bits */
2104 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04002105 unsigned long long n;
JP Abgralle0ed7402014-03-19 19:08:39 -07002106 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2107 if (n > MAX_32_NUM) {
2108 if (fs_param.s_feature_incompat &
2109 EXT4_FEATURE_INCOMPAT_64BIT)
2110 num_inodes = MAX_32_NUM;
2111 else {
2112 com_err(program_name, 0,
2113 _("too many inodes (%llu), raise "
2114 "inode ratio?"), n);
2115 exit(1);
2116 }
Eric Sandeenf3358642006-09-12 14:56:17 -04002117 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002118 } else if (num_inodes > MAX_32_NUM) {
Eric Sandeenf3358642006-09-12 14:56:17 -04002119 com_err(program_name, 0,
2120 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04002121 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04002122 exit(1);
2123 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00002124 /*
2125 * Calculate number of inodes based on the inode ratio
2126 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002127 fs_param.s_inodes_count = num_inodes ? num_inodes :
JP Abgralle0ed7402014-03-19 19:08:39 -07002128 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002129
JP Abgralle0ed7402014-03-19 19:08:39 -07002130 if ((((unsigned long long)fs_param.s_inodes_count) *
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002131 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
JP Abgralle0ed7402014-03-19 19:08:39 -07002132 ((ext2fs_blocks_count(&fs_param)) *
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002133 EXT2_BLOCK_SIZE(&fs_param))) {
2134 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2135 "(%u) too big for a\n\t"
JP Abgralle0ed7402014-03-19 19:08:39 -07002136 "filesystem with %llu blocks, "
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002137 "specify higher inode_ratio (-i)\n\t"
2138 "or lower inode count (-N).\n"),
2139 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002140 fs_param.s_inodes_count,
JP Abgralle0ed7402014-03-19 19:08:39 -07002141 (unsigned long long) ext2fs_blocks_count(&fs_param));
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04002142 exit(1);
2143 }
2144
Theodore Ts'o3839e651997-04-26 13:21:57 +00002145 /*
2146 * Calculate number of blocks to reserve
2147 */
JP Abgralle0ed7402014-03-19 19:08:39 -07002148 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2149 ext2fs_blocks_count(&fs_param) / 100.0);
2150
2151 free(fs_type);
2152 free(usage_types);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002153}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002154
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302155static int should_do_undo(const char *name)
2156{
2157 errcode_t retval;
2158 io_channel channel;
2159 __u16 s_magic;
2160 struct ext2_super_block super;
2161 io_manager manager = unix_io_manager;
2162 int csum_flag, force_undo;
2163
2164 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2165 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2166 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2167 if (!force_undo && (!csum_flag || !lazy_itable_init))
2168 return 0;
2169
2170 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2171 if (retval) {
2172 /*
2173 * We don't handle error cases instead we
2174 * declare that the file system doesn't exist
2175 * and let the rest of mke2fs take care of
2176 * error
2177 */
2178 retval = 0;
2179 goto open_err_out;
2180 }
2181
2182 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
JP Abgralle0ed7402014-03-19 19:08:39 -07002183 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302184 if (retval) {
2185 retval = 0;
2186 goto err_out;
2187 }
2188
2189#if defined(WORDS_BIGENDIAN)
2190 s_magic = ext2fs_swab16(super.s_magic);
2191#else
2192 s_magic = super.s_magic;
2193#endif
2194
2195 if (s_magic == EXT2_SUPER_MAGIC)
2196 retval = 1;
2197
2198err_out:
2199 io_channel_close(channel);
2200
2201open_err_out:
2202
2203 return retval;
2204}
2205
2206static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2207{
JP Abgralle0ed7402014-03-19 19:08:39 -07002208 errcode_t retval = ENOMEM;
2209 char *tdb_dir = NULL, *tdb_file = NULL;
2210 char *dev_name, *tmp_name;
2211 int free_tdb_dir = 0;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302212
2213 /*
2214 * Configuration via a conf file would be
2215 * nice
2216 */
2217 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
JP Abgralle0ed7402014-03-19 19:08:39 -07002218 if (!tdb_dir) {
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302219 profile_get_string(profile, "defaults",
2220 "undo_dir", 0, "/var/lib/e2fsprogs",
2221 &tdb_dir);
JP Abgralle0ed7402014-03-19 19:08:39 -07002222 free_tdb_dir = 1;
2223 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302224
2225 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
JP Abgralle0ed7402014-03-19 19:08:39 -07002226 access(tdb_dir, W_OK)) {
2227 if (free_tdb_dir)
2228 free(tdb_dir);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302229 return 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07002230 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302231
2232 tmp_name = strdup(name);
JP Abgralle0ed7402014-03-19 19:08:39 -07002233 if (!tmp_name)
2234 goto errout;
2235 dev_name = basename(tmp_name);
2236 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2237 if (!tdb_file) {
2238 free(tmp_name);
2239 goto errout;
Theodore Ts'o8a1cf3c2010-12-21 21:57:02 -05002240 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002241 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2242 free(tmp_name);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302243
2244 if (!access(tdb_file, F_OK)) {
2245 if (unlink(tdb_file) < 0) {
2246 retval = errno;
JP Abgralle0ed7402014-03-19 19:08:39 -07002247 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302248 }
2249 }
2250
2251 set_undo_io_backing_manager(*io_ptr);
2252 *io_ptr = undo_io_manager;
JP Abgralle0ed7402014-03-19 19:08:39 -07002253 retval = set_undo_io_backup_file(tdb_file);
2254 if (retval)
2255 goto errout;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302256 printf(_("Overwriting existing filesystem; this can be undone "
2257 "using the command:\n"
2258 " e2undo %s %s\n\n"), tdb_file, name);
Eric Sandeen79e62402008-07-06 18:36:56 -04002259
JP Abgralle0ed7402014-03-19 19:08:39 -07002260 if (free_tdb_dir)
2261 free(tdb_dir);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04002262 free(tdb_file);
JP Abgralle0ed7402014-03-19 19:08:39 -07002263 return 0;
2264
2265errout:
2266 if (free_tdb_dir)
2267 free(tdb_dir);
2268 free(tdb_file);
2269 com_err(program_name, retval, "%s",
2270 _("while trying to setup undo file\n"));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302271 return retval;
2272}
2273
JP Abgralle0ed7402014-03-19 19:08:39 -07002274static int mke2fs_discard_device(ext2_filsys fs)
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002275{
JP Abgralle0ed7402014-03-19 19:08:39 -07002276 struct ext2fs_numeric_progress_struct progress;
2277 blk64_t blocks = ext2fs_blocks_count(fs->super);
2278 blk64_t count = DISCARD_STEP_MB;
2279 blk64_t cur;
2280 int retval = 0;
Lukas Czerner7d9e3162011-01-24 20:52:00 +01002281
JP Abgralle0ed7402014-03-19 19:08:39 -07002282 /*
2283 * Let's try if discard really works on the device, so
2284 * we do not print numeric progress resulting in failure
2285 * afterwards.
2286 */
2287 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2288 if (retval)
2289 return retval;
2290 cur = fs->blocksize;
Theodore Ts'oaa07cb72011-02-27 20:09:54 -05002291
JP Abgralle0ed7402014-03-19 19:08:39 -07002292 count *= (1024 * 1024);
2293 count /= fs->blocksize;
2294
2295 ext2fs_numeric_progress_init(fs, &progress,
2296 _("Discarding device blocks: "),
2297 blocks);
2298 while (cur < blocks) {
2299 ext2fs_numeric_progress_update(fs, &progress, cur);
2300
2301 if (cur + count > blocks)
2302 count = blocks - cur;
2303
2304 retval = io_channel_discard(fs->io, cur, count);
2305 if (retval)
2306 break;
2307 cur += count;
2308 }
2309
2310 if (retval) {
2311 ext2fs_numeric_progress_close(fs, &progress,
2312 _("failed - "));
2313 if (!quiet)
2314 printf("%s\n",error_message(retval));
2315 } else
2316 ext2fs_numeric_progress_close(fs, &progress,
2317 _("done \n"));
2318
2319 return retval;
2320}
2321
2322static void fix_cluster_bg_counts(ext2_filsys fs)
2323{
2324 blk64_t cluster, num_clusters, tot_free;
2325 unsigned num = 0;
2326 int grp_free, num_free, group;
2327
2328 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2329 tot_free = num_free = group = grp_free = 0;
2330 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2331 cluster < num_clusters; cluster++) {
2332 if (!ext2fs_test_block_bitmap2(fs->block_map,
2333 EXT2FS_C2B(fs, cluster))) {
2334 grp_free++;
2335 tot_free++;
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002336 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002337 num++;
2338 if ((num == fs->super->s_clusters_per_group) ||
2339 (cluster == num_clusters-1)) {
2340 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2341 ext2fs_group_desc_csum_set(fs, group);
2342 num = 0;
2343 grp_free = 0;
2344 group++;
2345 }
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002346 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002347 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
Theodore Ts'oc6ed60c2011-06-10 18:57:22 -04002348}
2349
JP Abgralle0ed7402014-03-19 19:08:39 -07002350static int create_quota_inodes(ext2_filsys fs)
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002351{
JP Abgralle0ed7402014-03-19 19:08:39 -07002352 quota_ctx_t qctx;
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002353
JP Abgralle0ed7402014-03-19 19:08:39 -07002354 quota_init_context(&qctx, fs, -1);
2355 quota_compute_usage(qctx);
2356 quota_write_inode(qctx, quotatype);
2357 quota_release_context(&qctx);
2358
2359 return 0;
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002360}
2361
Theodore Ts'o3839e651997-04-26 13:21:57 +00002362int main (int argc, char *argv[])
2363{
2364 errcode_t retval = 0;
2365 ext2_filsys fs;
2366 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002367 unsigned int journal_blocks;
JP Abgralle0ed7402014-03-19 19:08:39 -07002368 unsigned int i, checkinterval;
2369 int max_mnt_count;
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002370 int val, hash_alg;
JP Abgralle0ed7402014-03-19 19:08:39 -07002371 int flags;
2372 int old_bitmaps;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002373 io_manager io_ptr;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302374 char tdb_string[40];
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002375 char *hash_alg_str;
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002376 int itable_zeroed = 0;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002377
2378#ifdef ENABLE_NLS
2379 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05002380 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002381 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2382 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -07002383 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002384#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00002385 PRS(argc, argv);
2386
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002387#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04002388 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2389 io_ptr = test_io_manager;
2390 test_io_backing_manager = unix_io_manager;
2391 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002392#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04002393 io_ptr = unix_io_manager;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04002394
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302395 if (should_do_undo(device_name)) {
2396 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2397 if (retval)
2398 exit(1);
2399 }
2400
Theodore Ts'o3839e651997-04-26 13:21:57 +00002401 /*
2402 * Initialize the superblock....
2403 */
JP Abgralle0ed7402014-03-19 19:08:39 -07002404 flags = EXT2_FLAG_EXCLUSIVE;
2405 if (direct_io)
2406 flags |= EXT2_FLAG_DIRECT_IO;
2407 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2408 &old_bitmaps);
2409 if (!old_bitmaps)
2410 flags |= EXT2_FLAG_64BITS;
2411 /*
2412 * By default, we print how many inode tables or block groups
2413 * or whatever we've written so far. The quiet flag disables
2414 * this, along with a lot of other output.
2415 */
2416 if (!quiet)
2417 flags |= EXT2_FLAG_PRINT_PROGRESS;
2418 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002419 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002420 com_err(device_name, retval, "%s",
2421 _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00002422 exit(1);
2423 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002424
JP Abgralle0ed7402014-03-19 19:08:39 -07002425 /* Calculate journal blocks */
2426 if (!journal_device && ((journal_size) ||
2427 (fs_param.s_feature_compat &
2428 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2429 journal_blocks = figure_journal_size(journal_size, fs);
JP Abgrall65f0aab2014-03-06 13:50:20 -08002430
JP Abgralle0ed7402014-03-19 19:08:39 -07002431 /* Can't undo discard ... */
2432 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2433 retval = mke2fs_discard_device(fs);
2434 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002435 if (verbose)
JP Abgralle0ed7402014-03-19 19:08:39 -07002436 printf("%s",
2437 _("Discard succeeded and will return "
2438 "0s - skipping inode table wipe\n"));
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002439 lazy_itable_init = 1;
2440 itable_zeroed = 1;
2441 }
2442 }
Eric Sandeen5827d242009-10-07 16:49:17 -05002443
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302444 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2445 32768 : fs->blocksize * 8);
2446 io_channel_set_options(fs->io, tdb_string);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002447
Theodore Ts'o6cb27402008-01-26 19:06:35 -05002448 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2449 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2450
Theodore Ts'ob7c5b402009-03-05 19:40:20 -05002451 if ((fs_param.s_feature_incompat &
2452 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2453 (fs_param.s_feature_ro_compat &
2454 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2455 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2456 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2457 fs->super->s_kbytes_written = 1;
2458
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002459 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00002460 * Wipe out the old on-disk superblock
2461 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04002462 if (!noaction)
2463 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00002464
2465 /*
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05002466 * Parse or generate a UUID for the filesystem
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002467 */
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05002468 if (fs_uuid) {
2469 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2470 com_err(device_name, 0, "could not parse UUID: %s\n",
2471 fs_uuid);
2472 exit(1);
2473 }
2474 } else
2475 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002476
2477 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04002478 * Initialize the directory index variables
2479 */
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002480 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2481 "half_md4");
2482 hash_alg = e2p_string2hash(hash_alg_str);
Theodore Ts'o5a2db042010-12-01 18:49:26 -05002483 free(hash_alg_str);
Theodore Ts'od5f57d92008-08-29 21:39:36 -04002484 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2485 EXT2_HASH_HALF_MD4;
Theodore Ts'o843049c2002-09-22 15:37:40 -04002486 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2487
2488 /*
JP Abgralle0ed7402014-03-19 19:08:39 -07002489 * Periodic checks can be enabled/disabled via config file.
2490 * Note we override the kernel include file's idea of what the default
2491 * check interval (never) should be. It's a good idea to check at
2492 * least *occasionally*, specially since servers will never rarely get
2493 * to reboot, since Linux is so robust these days. :-)
2494 *
2495 * 180 days (six months) seems like a good value.
Theodore Ts'o44c09c02001-01-14 17:02:09 +00002496 */
JP Abgralle0ed7402014-03-19 19:08:39 -07002497#ifdef EXT2_DFL_CHECKINTERVAL
2498#undef EXT2_DFL_CHECKINTERVAL
2499#endif
2500#define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2501
2502 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2503 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2504 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2505 /*
2506 * Add "jitter" to the superblock's check interval so that we
2507 * don't check all the filesystems at the same time. We use a
2508 * kludgy hack of using the UUID to derive a random jitter value
2509 */
2510 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2511 val += fs->super->s_uuid[i];
2512 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2513 } else
2514 fs->super->s_max_mnt_count = -1;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00002515
2516 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002517 * Override the creator OS, if applicable
2518 */
2519 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002520 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002521 exit(1);
2522 }
2523
2524 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00002525 * For the Hurd, we will turn off filetype since it doesn't
2526 * support it.
2527 */
2528 if (fs->super->s_creator_os == EXT2_OS_HURD)
2529 fs->super->s_feature_incompat &=
2530 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2531
2532 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002533 * Set the volume label...
2534 */
2535 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00002536 memset(fs->super->s_volume_name, 0,
2537 sizeof(fs->super->s_volume_name));
2538 strncpy(fs->super->s_volume_name, volume_label,
2539 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002540 }
2541
2542 /*
2543 * Set the last mount directory
2544 */
2545 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00002546 memset(fs->super->s_last_mounted, 0,
2547 sizeof(fs->super->s_last_mounted));
2548 strncpy(fs->super->s_last_mounted, mount_dir,
2549 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002550 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002551
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002552 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00002553 show_stats(fs);
2554
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002555 if (noaction)
2556 exit(0);
2557
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002558 if (fs->super->s_feature_incompat &
2559 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2560 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06002561 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002562 }
2563
Theodore Ts'o3839e651997-04-26 13:21:57 +00002564 if (bad_blocks_filename)
2565 read_bb_file(fs, &bb_list, bad_blocks_filename);
2566 if (cflag)
2567 test_disk(fs, &bb_list);
2568
2569 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05002570 fs->stride = fs_stride = fs->super->s_raid_stride;
JP Abgralle0ed7402014-03-19 19:08:39 -07002571 if (!quiet)
2572 printf("%s", _("Allocating group tables: "));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002573 retval = ext2fs_allocate_tables(fs);
2574 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002575 com_err(program_name, retval, "%s",
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002576 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002577 exit(1);
2578 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002579 if (!quiet)
2580 printf("%s", _("done \n"));
2581
2582 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2583 if (retval) {
2584 com_err(program_name, retval, "%s",
2585 _("\n\twhile converting subcluster bitmap"));
2586 exit(1);
2587 }
2588
Theodore Ts'of3db3561997-04-26 13:34:30 +00002589 if (super_only) {
2590 fs->super->s_state |= EXT2_ERROR_FS;
2591 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
JP Abgralle0ed7402014-03-19 19:08:39 -07002592 /*
2593 * The command "mke2fs -S" is used to recover
2594 * corrupted file systems, so do not mark any of the
2595 * inodes as unused; we want e2fsck to consider all
2596 * inodes as potentially containing recoverable data.
2597 */
2598 if (fs->super->s_feature_ro_compat &
2599 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2600 for (i = 0; i < fs->group_desc_count; i++)
2601 ext2fs_bg_itable_unused_set(fs, i, 0);
2602 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002603 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06002604 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
JP Abgralle0ed7402014-03-19 19:08:39 -07002605 blk64_t rsv = 65536 / fs->blocksize;
2606 blk64_t blocks = ext2fs_blocks_count(fs->super);
2607 blk64_t start;
2608 blk64_t ret_blk;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002609
2610#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04002611 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002612#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06002613
2614 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002615 * Wipe out any old MD RAID (or other) metadata at the end
2616 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06002617 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002618 */
Andreas Dilger59f27242001-08-30 15:39:04 -06002619 start = (blocks & ~(rsv - 1));
2620 if (start > rsv)
2621 start -= rsv;
2622 if (start > 0)
JP Abgralle0ed7402014-03-19 19:08:39 -07002623 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302624 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06002625
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002626 if (retval) {
2627 com_err(program_name, retval,
JP Abgralle0ed7402014-03-19 19:08:39 -07002628 _("while zeroing block %llu at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002629 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002630 }
Eric Sandeen6fcd6f82010-08-20 16:41:14 -05002631 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002632 create_root_dir(fs);
2633 create_lost_and_found(fs);
2634 reserve_inodes(fs);
2635 create_bad_block_inode(fs, bb_list);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002636 if (fs->super->s_feature_compat &
Theodore Ts'oea774312005-01-28 11:45:28 -05002637 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2638 retval = ext2fs_create_resize_inode(fs);
2639 if (retval) {
2640 com_err("ext2fs_create_resize_inode", retval,
JP Abgralle0ed7402014-03-19 19:08:39 -07002641 "%s",
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002642 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05002643 exit(1);
2644 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002645 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002646 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002647
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002648 if (journal_device) {
2649 ext2_filsys jfs;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002650
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002651 if (!force)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002652 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00002653 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002654
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002655 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2656 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2657 fs->blocksize, unix_io_manager, &jfs);
2658 if (retval) {
2659 com_err(program_name, retval,
2660 _("while trying to open journal device %s\n"),
2661 journal_device);
2662 exit(1);
2663 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002664 if (!quiet) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002665 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002666 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002667 fflush(stdout);
2668 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002669 retval = ext2fs_add_journal_device(fs, jfs);
2670 if(retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002671 com_err (program_name, retval,
2672 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002673 journal_device);
2674 exit(1);
2675 }
2676 if (!quiet)
JP Abgralle0ed7402014-03-19 19:08:39 -07002677 printf("%s", _("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002678 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06002679 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002680 } else if ((journal_size) ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002681 (fs_param.s_feature_compat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002682 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'oa620bad2009-03-31 07:42:24 -04002683 if (super_only) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002684 printf("%s", _("Skipping journal creation in super-only mode\n"));
Theodore Ts'oa620bad2009-03-31 07:42:24 -04002685 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2686 goto no_journal;
2687 }
2688
Theodore Ts'o93345d12001-02-17 06:09:50 +00002689 if (!journal_blocks) {
2690 fs->super->s_feature_compat &=
2691 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2692 goto no_journal;
2693 }
2694 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002695 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00002696 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002697 fflush(stdout);
2698 }
Theodore Ts'o63985322001-01-03 17:02:13 +00002699 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2700 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002701 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002702 com_err(program_name, retval, "%s",
2703 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002704 exit(1);
2705 }
2706 if (!quiet)
JP Abgralle0ed7402014-03-19 19:08:39 -07002707 printf("%s", _("done\n"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002708 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002709no_journal:
JP Abgralle0ed7402014-03-19 19:08:39 -07002710 if (!super_only &&
2711 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2712 retval = ext2fs_mmp_init(fs);
2713 if (retval) {
2714 fprintf(stderr, "%s",
2715 _("\nError while enabling multiple "
2716 "mount protection feature."));
2717 exit(1);
2718 }
2719 if (!quiet)
2720 printf(_("Multiple mount protection is enabled "
2721 "with update interval %d seconds.\n"),
2722 fs->super->s_mmp_update_interval);
2723 }
2724
2725 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2726 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2727 fix_cluster_bg_counts(fs);
2728 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2729 EXT4_FEATURE_RO_COMPAT_QUOTA))
2730 create_quota_inodes(fs);
Aditya Kali1f5d7a82011-07-20 11:40:04 -07002731
Theodore Ts'o3839e651997-04-26 13:21:57 +00002732 if (!quiet)
JP Abgralle0ed7402014-03-19 19:08:39 -07002733 printf("%s", _("Writing superblocks and "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002734 "filesystem accounting information: "));
JP Abgralle0ed7402014-03-19 19:08:39 -07002735 checkinterval = fs->super->s_checkinterval;
2736 max_mnt_count = fs->super->s_max_mnt_count;
2737 retval = ext2fs_close(fs);
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002738 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002739 fprintf(stderr, "%s",
Theodore Ts'o66938372002-03-08 00:14:46 -05002740 _("\nWarning, had trouble writing out superblocks."));
JP Abgralle0ed7402014-03-19 19:08:39 -07002741 } else if (!quiet) {
2742 printf("%s", _("done\n\n"));
JP Abgrall65f0aab2014-03-06 13:50:20 -08002743 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
JP Abgralle0ed7402014-03-19 19:08:39 -07002744 print_check_message(max_mnt_count, checkinterval);
JP Abgrall65f0aab2014-03-06 13:50:20 -08002745 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002746
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002747 remove_error_table(&et_ext2_error_table);
2748 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002749 profile_release(profile);
Theodore Ts'o5a2db042010-12-01 18:49:26 -05002750 for (i=0; fs_types[i]; i++)
2751 free(fs_types[i]);
2752 free(fs_types);
JP Abgralle0ed7402014-03-19 19:08:39 -07002753 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002754}