blob: 155936857df0a190da80c8a356e45a5ca417bf34 [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
45#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000046#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000047#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000048#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000049#include <sys/types.h>
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053050#include <libgen.h>
Theodore Ts'o36585792008-06-07 22:07:50 -040051#include <limits.h>
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -050052#include <blkid/blkid.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000053
Theodore Ts'o54c637d2001-05-14 11:45:38 +000054#include "ext2fs/ext2_fs.h"
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -040055#include "ext2fs/ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000057#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000058#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000060#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050061#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050062#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000063#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000064#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000065
66#define STRIDE_LENGTH 8
67
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000068#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000069#define ZAP_BOOTBLOCK
70#endif
71
Theodore Ts'o3839e651997-04-26 13:21:57 +000072extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000073extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000074
75const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000076const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000077
78/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000079int cflag;
80int verbose;
81int quiet;
82int super_only;
83int force;
84int noaction;
85int journal_size;
86int journal_flags;
Theodore Ts'oa4396e92008-04-18 10:19:27 -040087int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000088char *bad_blocks_filename;
89__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050091struct ext2_super_block fs_param;
Theodore Ts'ob0afdda2009-01-20 13:18:23 -050092char *fs_uuid = NULL;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000093char *creator_os;
94char *volume_label;
95char *mount_dir;
96char *journal_device;
97int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053098char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +000099
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500100profile_t profile;
101
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400102int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -0400103int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400104
Theodore Ts'o63985322001-01-03 17:02:13 +0000105static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500107 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400108 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500109 "[-J journal-options]\n"
Theodore Ts'o9ba40002008-04-22 08:27:01 -0400110 "\t[-G meta group size] [-N number-of-inodes]\n"
111 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
112 "\t[-g blocks-per-group] [-L volume-label] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400113 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500114 "[-r fs-revision] [-E extended-option[,...]]\n"
Theodore Ts'ob0afdda2009-01-20 13:18:23 -0500115 "\t[-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 program_name);
117 exit(1);
118}
119
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000120static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121{
122 int l = 0;
123
124 arg >>= 1;
125 while (arg) {
126 l++;
127 arg >>= 1;
128 }
129 return l;
130}
131
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000132static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000133{
134 int l;
135
136 for (l=0; arg ; l++)
137 arg = arg / 10;
138 return l;
139}
140
Theodore Ts'od99225e2004-09-25 07:40:12 -0400141static int parse_version_number(const char *s)
142{
143 int major, minor, rev;
144 char *endptr;
145 const char *cp = s;
146
147 if (!s)
148 return 0;
149 major = strtol(cp, &endptr, 10);
150 if (cp == endptr || *endptr != '.')
151 return 0;
152 cp = endptr + 1;
153 minor = strtol(cp, &endptr, 10);
154 if (cp == endptr || *endptr != '.')
155 return 0;
156 cp = endptr + 1;
157 rev = strtol(cp, &endptr, 10);
158 if (cp == endptr)
159 return 0;
160 return ((((major * 256) + minor) * 256) + rev);
161}
162
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000163/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 * Helper function for read_bb_file and test_disk
165 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500166static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167{
Theodore Ts'o66938372002-03-08 00:14:46 -0500168 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 return;
170}
171
172/*
173 * Reads the bad blocks list from a file
174 */
175static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
176 const char *bad_blocks_file)
177{
178 FILE *f;
179 errcode_t retval;
180
181 f = fopen(bad_blocks_file, "r");
182 if (!f) {
183 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000184 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185 exit(1);
186 }
187 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
188 fclose (f);
189 if (retval) {
190 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000191 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 exit(1);
193 }
194}
195
196/*
197 * Runs the badblocks program to test the disk
198 */
199static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
200{
201 FILE *f;
202 errcode_t retval;
203 char buf[1024];
204
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400205 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500206 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400207 fs->device_name, ext2fs_blocks_count(fs->super)-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000209 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 f = popen(buf, "r");
211 if (!f) {
212 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400213 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214 exit(1);
215 }
216 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000217 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 if (retval) {
219 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000220 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000221 exit(1);
222 }
223}
224
225static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
226{
Theodore Ts'o54434922003-12-07 01:28:50 -0500227 dgrp_t i;
228 blk_t j;
229 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 blk_t blk;
231 badblocks_iterate bb_iter;
232 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000233 blk_t group_block;
234 int group;
235 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236
237 if (!bb_list)
238 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400239
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 /*
241 * The primary superblock and group descriptors *must* be
242 * good; if not, abort.
243 */
244 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
245 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000246 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000247 fprintf(stderr, _("Block %d in primary "
248 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400249 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000250 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500252 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253 exit(1);
254 }
255 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000256
257 /*
258 * See if any of the bad blocks are showing up in the backup
259 * superblocks and/or group descriptors. If so, issue a
260 * warning and adjust the block counts appropriately.
261 */
262 group_block = fs->super->s_first_data_block +
263 fs->super->s_blocks_per_group;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400264
Theodore Ts'of3db3561997-04-26 13:34:30 +0000265 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000266 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000267 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000268 if (ext2fs_badblocks_list_test(bb_list,
269 group_block + j)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400270 if (!group_bad)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000271 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500272_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000273" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000274 group_block);
275 group_bad++;
Theodore Ts'o6493f8e2009-10-25 20:50:15 -0400276 group = ext2fs_group_of_blk2(fs, group_block+j);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400277 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400278 ext2fs_group_desc_csum_set(fs, group);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400279 ext2fs_free_blocks_count_add(fs->super, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 }
281 }
282 group_block += fs->super->s_blocks_per_group;
283 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400284
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 /*
286 * Mark all the bad blocks as used...
287 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000288 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000290 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000291 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292 exit(1);
293 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400294 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Valerie Aurora Henson463e7322009-08-05 00:17:56 -0400295 ext2fs_mark_block_bitmap2(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000296 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297}
298
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400299static void write_inode_tables(ext2_filsys fs, int lazy_flag)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000300{
301 errcode_t retval;
302 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500303 dgrp_t i;
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400304 int num, ipb;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400305 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000306
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400307 ext2fs_numeric_progress_init(fs, &progress,
308 _("Writing inode tables: "),
309 fs->group_desc_count);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000310
Theodore Ts'o3839e651997-04-26 13:21:57 +0000311 for (i = 0; i < fs->group_desc_count; i++) {
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400312 ext2fs_numeric_progress_update(fs, &progress, i);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400313
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400314 blk = ext2fs_inode_table_loc(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000315 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000316
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400317 if (lazy_flag) {
318 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400319 num = ((((fs->super->s_inodes_per_group -
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400320 ext2fs_bg_itable_unused(fs, i)) *
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400321 EXT2_INODE_SIZE(fs->super)) +
322 EXT2_BLOCK_SIZE(fs->super) - 1) /
323 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400324 } else {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500325 /* The kernel doesn't need to zero the itable blocks */
Eric Sandeene633b582009-10-25 21:41:32 -0400326 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400327 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000328 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530329 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400330 if (retval) {
331 fprintf(stderr, _("\nCould not write %d "
332 "blocks in inode table starting at %u: %s\n"),
333 num, blk, error_message(retval));
334 exit(1);
335 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000336 if (sync_kludge) {
337 if (sync_kludge == 1)
338 sync();
339 else if ((i % sync_kludge) == 0)
340 sync();
341 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530343 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400344 ext2fs_numeric_progress_close(fs, &progress,
345 _("done \n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346}
347
348static void create_root_dir(ext2_filsys fs)
349{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400350 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000351 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400352 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353
354 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
355 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000356 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 exit(1);
358 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000359 if (geteuid()) {
360 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
361 if (retval) {
362 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000363 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000364 exit(1);
365 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400366 uid = getuid();
367 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500368 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400369 if (uid) {
370 gid = getgid();
371 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500372 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400373 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500374 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000375 if (retval) {
376 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000377 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000378 exit(1);
379 }
380 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381}
382
383static void create_lost_and_found(ext2_filsys fs)
384{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400385 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000387 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 const char *name = "lost+found";
389 int i;
390
Theodore Ts'o6a525062001-12-24 09:40:00 -0500391 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
393 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000394 com_err("ext2fs_mkdir", retval,
395 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000396 exit(1);
397 }
398
399 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
400 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000401 com_err("ext2_lookup", retval,
402 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 exit(1);
404 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400405
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100407 /* Ensure that lost+found is at least 2 blocks, so we always
408 * test large empty blocks for big-block filesystems. */
409 if ((lpf_size += fs->blocksize) >= 16*1024 &&
410 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000411 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 retval = ext2fs_expand_dir(fs, ino);
413 if (retval) {
414 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000415 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 exit(1);
417 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500418 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419}
420
421static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
422{
423 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400424
Valerie Aurora Henson463e7322009-08-05 00:17:56 -0400425 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400426 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 retval = ext2fs_update_bb_inode(fs, bb_list);
428 if (retval) {
429 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000430 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 exit(1);
432 }
433
434}
435
436static void reserve_inodes(ext2_filsys fs)
437{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000438 ext2_ino_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400440 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
441 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 ext2fs_mark_ib_dirty(fs);
443}
444
Theodore Ts'o756df352002-03-07 20:52:12 -0500445#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500446#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500447#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500448
Theodore Ts'o04a96852001-08-30 21:55:26 -0400449static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000450{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400451 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000452 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500453 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000454
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400455 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600456 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500457 printf(_("Out of memory erasing sectors %d-%d\n"),
458 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600459 exit(1);
460 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500461
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500462 if (sect == 0) {
463 /* Check for a BSD disklabel, and don't erase it if so */
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400464 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500465 if (retval)
466 fprintf(stderr,
467 _("Warning: could not read block 0: %s\n"),
468 error_message(retval));
469 else {
470 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
471 if ((*magic == BSD_DISKMAGIC) ||
472 (*magic == BSD_MAGICDISK))
473 return;
474 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500475 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500476
Theodore Ts'o70988102002-07-14 08:00:00 -0400477 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000478 io_channel_set_blksize(fs->io, 512);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400479 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000480 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400481 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500483 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
484 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000485}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000486
487static void create_journal_dev(ext2_filsys fs)
488{
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400489 struct ext2fs_numeric_progress_struct progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000490 errcode_t retval;
491 char *buf;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530492 blk_t blk, err_blk;
493 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000494
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000495 retval = ext2fs_create_journal_superblock(fs,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400496 ext2fs_blocks_count(fs->super), 0, &buf);
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000497 if (retval) {
498 com_err("create_journal_dev", retval,
499 _("while initializing journal superblock"));
500 exit(1);
501 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400502 ext2fs_numeric_progress_init(fs, &progress,
503 _("Zeroing journal device: "),
504 ext2fs_blocks_count(fs->super));
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530505 blk = 0;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400506 count = ext2fs_blocks_count(fs->super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530507 while (count > 0) {
508 if (count > 1024)
509 c = 1024;
510 else
511 c = count;
512 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
513 if (retval) {
514 com_err("create_journal_dev", retval,
515 _("while zeroing journal device "
516 "(block %u, count %d)"),
517 err_blk, err_count);
518 exit(1);
519 }
520 blk += c;
521 count -= c;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400522 ext2fs_numeric_progress_update(fs, &progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000523 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530524 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000525
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400526 retval = io_channel_write_blk64(fs->io,
527 fs->super->s_first_data_block+1,
528 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000529 if (retval) {
530 com_err("create_journal_dev", retval,
531 _("while writing journal superblock"));
532 exit(1);
533 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -0400534 ext2fs_numeric_progress_close(fs, &progress, NULL);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000535}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000536
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537static void show_stats(ext2_filsys fs)
538{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000539 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000540 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500541 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500543 dgrp_t i;
544 int need, col_left;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400545
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400546 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
547 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
548 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000549
550 memset(buf, 0, sizeof(buf));
551 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000552 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500553 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500554 os = e2p_os2string(fs->super->s_creator_os);
555 fputs(os, stdout);
556 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000557 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000558 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000559 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000560 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000561 s->s_log_frag_size);
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -0500562 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
563 s->s_raid_stride, s->s_raid_stripe_width);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400564 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
565 ext2fs_blocks_count(s));
566 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
567 ext2fs_r_blocks_count(s),
568 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000569 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500570 if (s->s_reserved_gdt_blocks)
571 printf(_("Maximum filesystem blocks=%lu\n"),
572 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200573 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000574 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000575 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000576 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000577 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000578 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000580 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581
582 if (fs->group_desc_count == 1) {
583 printf("\n");
584 return;
585 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500586
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000587 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 group_block = s->s_first_data_block;
589 col_left = 0;
590 for (i = 1; i < fs->group_desc_count; i++) {
591 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000592 if (!ext2fs_bg_has_super(fs, i))
593 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000594 if (i != 1)
595 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000596 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000597 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000599 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000601 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000602 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 }
604 printf("\n\n");
605}
606
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000607/*
608 * Set the S_CREATOR_OS field. Return true if OS is known,
609 * otherwise, 0.
610 */
611static int set_os(struct ext2_super_block *sb, char *os)
612{
613 if (isdigit (*os))
614 sb->s_creator_os = atoi (os);
615 else if (strcasecmp(os, "linux") == 0)
616 sb->s_creator_os = EXT2_OS_LINUX;
617 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
618 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500619 else if (strcasecmp(os, "freebsd") == 0)
620 sb->s_creator_os = EXT2_OS_FREEBSD;
621 else if (strcasecmp(os, "lites") == 0)
622 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000623 else
624 return 0;
625 return 1;
626}
627
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000628#define PATH_SET "PATH=/sbin"
629
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400630static void parse_extended_opts(struct ext2_super_block *param,
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500631 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000632{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400633 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000634 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500635 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000636
637 len = strlen(opts);
638 buf = malloc(len+1);
639 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500640 fprintf(stderr,
641 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000642 exit(1);
643 }
644 strcpy(buf, opts);
645 for (token = buf; token && *token; token = next) {
646 p = strchr(token, ',');
647 next = 0;
648 if (p) {
649 *p = 0;
650 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500651 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000652 arg = strchr(token, '=');
653 if (arg) {
654 *arg = 0;
655 arg++;
656 }
657 if (strcmp(token, "stride") == 0) {
658 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500659 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500660 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000661 continue;
662 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500663 param->s_raid_stride = strtoul(arg, &p, 0);
664 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000665 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400666 _("Invalid stride parameter: %s\n"),
667 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500668 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000669 continue;
670 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500671 } else if (strcmp(token, "stripe-width") == 0 ||
672 strcmp(token, "stripe_width") == 0) {
673 if (!arg) {
674 r_usage++;
675 badopt = token;
676 continue;
677 }
678 param->s_raid_stripe_width = strtoul(arg, &p, 0);
679 if (*p || (param->s_raid_stripe_width == 0)) {
680 fprintf(stderr,
681 _("Invalid stripe-width parameter: %s\n"),
682 arg);
683 r_usage++;
684 continue;
685 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500686 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500687 unsigned long resize, bpg, rsv_groups;
688 unsigned long group_desc_count, desc_blocks;
689 unsigned int gdpb, blocksize;
690 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500691
692 if (!arg) {
693 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500694 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500695 continue;
696 }
697
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400698 resize = parse_num_blocks(arg,
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500699 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500700
701 if (resize == 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400702 fprintf(stderr,
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500703 _("Invalid resize parameter: %s\n"),
704 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500705 r_usage++;
706 continue;
707 }
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400708 if (resize <= ext2fs_blocks_count(param)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400709 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400710 _("The resize maximum must be greater "
711 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500712 r_usage++;
713 continue;
714 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500715
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500716 blocksize = EXT2_BLOCK_SIZE(param);
717 bpg = param->s_blocks_per_group;
718 if (!bpg)
719 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200720 gdpb = EXT2_DESC_PER_BLOCK(param);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400721 group_desc_count = (__u32) ext2fs_div64_ceil(
722 ext2fs_blocks_count(param), bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500723 desc_blocks = (group_desc_count +
724 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400725 rsv_groups = ext2fs_div_ceil(resize, bpg);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400726 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500727 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500728 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500729 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
730
731 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400732 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400733 fprintf(stderr,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400734 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400735 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400736 exit(1);
737 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500738 param->s_feature_compat |=
739 EXT2_FEATURE_COMPAT_RESIZE_INODE;
740
741 param->s_reserved_gdt_blocks = rsv_gdb;
742 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500743 } else if (!strcmp(token, "test_fs")) {
744 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400745 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400746 if (arg)
747 lazy_itable_init = strtoul(arg, &p, 0);
748 else
749 lazy_itable_init = 1;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500750 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500751 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500752 badopt = token;
753 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000754 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500755 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500756 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400757 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000758 "and may take an argument which\n"
759 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400760 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500761 "\tstride=<RAID per-disk data chunk in blocks>\n"
762 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400763 "\tresize=<resize maximum size in blocks>\n"
764 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
765 "\ttest_fs\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400766 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400767 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000768 exit(1);
769 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500770 if (param->s_raid_stride &&
771 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
772 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
773 "multiple of stride %u.\n\n"),
774 param->s_raid_stripe_width, param->s_raid_stride);
775
Brian Behlendorf21400382007-05-31 11:30:47 -0400776 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400777}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000778
Theodore Ts'o896938d1999-10-23 01:04:50 +0000779static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500780 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400781 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500782 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400783 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500784 EXT2_FEATURE_COMPAT_EXT_ATTR,
785 /* Incompat */
786 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500787 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400788 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500789 EXT2_FEATURE_INCOMPAT_META_BG|
790 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500791 /* R/O compat */
792 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400793 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
794 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
795 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500796 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
797 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000798};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000799
800
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500801static void syntax_err_report(const char *filename, long err, int line_num)
802{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400803 fprintf(stderr,
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500804 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
805 filename, line_num, error_message(err));
806 exit(1);
807}
808
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200809static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500810
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400811static void edit_feature(const char *str, __u32 *compat_array)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500812{
813 if (!str)
814 return;
815
816 if (e2p_edit_feature(str, compat_array, ok_features)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400817 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500818 str);
819 exit(1);
820 }
821}
822
Theodore Ts'o3d438362008-02-19 08:32:58 -0500823struct str_list {
824 char **list;
825 int num;
826 int max;
827};
828
829static errcode_t init_list(struct str_list *sl)
830{
831 sl->num = 0;
832 sl->max = 0;
833 sl->list = malloc((sl->max+1) * sizeof(char *));
834 if (!sl->list)
835 return ENOMEM;
836 sl->list[0] = 0;
837 return 0;
838}
839
840static errcode_t push_string(struct str_list *sl, const char *str)
841{
842 char **new_list;
843
844 if (sl->num >= sl->max) {
845 sl->max += 2;
846 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
847 if (!new_list)
848 return ENOMEM;
849 sl->list = new_list;
850 }
851 sl->list[sl->num] = malloc(strlen(str)+1);
852 if (sl->list[sl->num] == 0)
853 return ENOMEM;
854 strcpy(sl->list[sl->num], str);
855 sl->num++;
856 sl->list[sl->num] = 0;
857 return 0;
858}
859
860static void print_str_list(char **list)
861{
862 char **cpp;
863
864 for (cpp = list; *cpp; cpp++) {
865 printf("'%s'", *cpp);
866 if (cpp[1])
867 fputs(", ", stdout);
868 }
869 fputc('\n', stdout);
870}
871
872static char **parse_fs_type(const char *fs_type,
873 const char *usage_types,
874 struct ext2_super_block *fs_param,
875 char *progname)
876{
877 const char *ext_type = 0;
878 char *parse_str;
879 char *profile_type = 0;
880 char *cp, *t;
881 const char *size_type;
882 struct str_list list;
Theodore Ts'o3d438362008-02-19 08:32:58 -0500883 unsigned long meg;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -0400884 int is_hurd = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -0500885
886 if (init_list(&list))
887 return 0;
888
Theodore Ts'o7f5601e2008-07-10 09:24:25 -0400889 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
890 !strcasecmp(creator_os, "hurd")))
891 is_hurd = 1;
892
Theodore Ts'o3d438362008-02-19 08:32:58 -0500893 if (fs_type)
894 ext_type = fs_type;
Theodore Ts'o7f5601e2008-07-10 09:24:25 -0400895 else if (is_hurd)
896 ext_type = "ext2";
Theodore Ts'o1a71bd42009-01-20 12:02:40 -0500897 else if (!strcmp(program_name, "mke3fs"))
898 ext_type = "ext3";
Theodore Ts'o3d438362008-02-19 08:32:58 -0500899 else if (progname) {
900 ext_type = strrchr(progname, '/');
901 if (ext_type)
902 ext_type++;
903 else
904 ext_type = progname;
905
906 if (!strncmp(ext_type, "mkfs.", 5)) {
907 ext_type += 5;
908 if (ext_type[0] == 0)
909 ext_type = 0;
910 } else
911 ext_type = 0;
912 }
913
914 if (!ext_type) {
915 profile_get_string(profile, "defaults", "fs_type", 0,
916 "ext2", &profile_type);
917 ext_type = profile_type;
918 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
919 ext_type = "ext3";
920 }
921
Theodore Ts'obad89b22008-08-22 21:57:29 -0400922 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
923 !strcmp(ext_type, "ext4dev")) {
924 profile_get_string(profile, "fs_types", ext_type, "features",
925 0, &t);
926 if (!t) {
927 printf(_("\nWarning! Your mke2fs.conf file does "
928 "not define the %s filesystem type.\n"),
929 ext_type);
930 printf(_("You probably need to install an updated "
931 "mke2fs.conf file.\n\n"));
932 sleep(5);
933 }
934 }
935
Theodore Ts'o3d438362008-02-19 08:32:58 -0500936 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400937 if (ext2fs_blocks_count(fs_param) < 3 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -0500938 size_type = "floppy";
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400939 else if (ext2fs_blocks_count(fs_param) < 512 * meg)
Theodore Ts'o3d438362008-02-19 08:32:58 -0500940 size_type = "small";
941 else
942 size_type = "default";
943
944 if (!usage_types)
945 usage_types = size_type;
946
947 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
948 if (!parse_str) {
949 free(list.list);
950 return 0;
951 }
952 if (usage_types)
953 strcpy(parse_str, usage_types);
954 else
955 *parse_str = '\0';
956
957 if (ext_type)
958 push_string(&list, ext_type);
959 cp = parse_str;
960 while (1) {
961 t = strchr(cp, ',');
962 if (t)
963 *t = '\0';
964
965 if (*cp)
966 push_string(&list, cp);
967 if (t)
968 cp = t+1;
969 else {
970 cp = "";
971 break;
972 }
973 }
974 free(parse_str);
Jim Meyering45e338f2009-02-23 18:07:50 +0100975 free(profile_type);
Theodore Ts'o7f5601e2008-07-10 09:24:25 -0400976 if (is_hurd)
977 push_string(&list, "hurd");
Theodore Ts'o3d438362008-02-19 08:32:58 -0500978 return (list.list);
979}
980
981static char *get_string_from_profile(char **fs_types, const char *opt,
982 const char *def_val)
983{
984 char *ret = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -0500985 int i;
986
987 for (i=0; fs_types[i]; i++);
988 for (i-=1; i >=0 ; i--) {
989 profile_get_string(profile, "fs_types", fs_types[i],
990 opt, 0, &ret);
991 if (ret)
992 return ret;
993 }
994 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
995 return (ret);
996}
997
998static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
999{
1000 int ret;
1001 char **cpp;
1002
1003 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1004 for (cpp = fs_types; *cpp; cpp++)
1005 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1006 return ret;
1007}
1008
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001009static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1010{
1011 int ret;
1012 char **cpp;
1013
1014 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1015 for (cpp = fs_types; *cpp; cpp++)
1016 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1017 return ret;
1018}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001019
Theodore Ts'od48bc602007-07-04 14:10:46 -04001020extern const char *mke2fs_default_profile;
1021static const char *default_files[] = { "<default>", 0 };
1022
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001023#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1024/*
1025 * Sets the geometry of a device (stripe/stride), and returns the
1026 * device's alignment offset, if any, or a negative error.
1027 */
1028static int ext2fs_get_device_geometry(const char *file,
1029 struct ext2_super_block *fs_param)
1030{
1031 int rc = -1;
1032 int blocksize;
1033 blkid_probe pr;
1034 blkid_topology tp;
1035 unsigned long min_io;
1036 unsigned long opt_io;
1037
1038 pr = blkid_new_probe_from_filename(file);
1039 if (!pr)
1040 goto out;
1041
1042 tp = blkid_probe_get_topology(pr);
1043 if (!tp)
1044 goto out;
1045
1046 min_io = blkid_topology_get_minimum_io_size(tp);
1047 opt_io = blkid_topology_get_optimal_io_size(tp);
1048 blocksize = EXT2_BLOCK_SIZE(fs_param);
1049
1050 fs_param->s_raid_stride = min_io / blocksize;
1051 fs_param->s_raid_stripe_width = opt_io / blocksize;
1052
1053 rc = blkid_topology_get_alignment_offset(tp);
1054out:
1055 blkid_free_probe(pr);
1056 return rc;
1057}
1058#endif
1059
Theodore Ts'o3839e651997-04-26 13:21:57 +00001060static void PRS(int argc, char *argv[])
1061{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001062 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001063 int size;
Eric Sandeen79e62402008-07-06 18:36:56 -04001064 char *tmp, **cpp;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001065 int blocksize = 0;
1066 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001067 int inode_size = 0;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001068 unsigned long flex_bg_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -05001069 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001070 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001071 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001072 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001073 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001074 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001075 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -05001076 const char * fs_type = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001077 const char * usage_types = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001078 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -05001079#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001080 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001081#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001082 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001083 int s_opt = -1, r_opt = -1;
1084 char *fs_features = 0;
1085 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001086 char *newpath;
1087 int pathlen = sizeof(PATH_SET) + 1;
1088
1089 if (oldpath)
1090 pathlen += strlen(oldpath);
1091 newpath = malloc(pathlen);
1092 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001093
Theodore Ts'o3839e651997-04-26 13:21:57 +00001094 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001095 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001096 strcat (newpath, ":");
1097 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001098 }
1099 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001101 tmp = getenv("MKE2FS_SYNC");
1102 if (tmp)
1103 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001104
1105 /* Determine the system page size if possible */
1106#ifdef HAVE_SYSCONF
1107#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1108#define _SC_PAGESIZE _SC_PAGE_SIZE
1109#endif
1110#ifdef _SC_PAGESIZE
1111 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001112 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001113 sys_page_size = sysval;
1114#endif /* _SC_PAGESIZE */
1115#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001116
1117 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1118 config_fn[0] = tmp;
1119 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001120 retval = profile_init(config_fn, &profile);
1121 if (retval == ENOENT) {
1122 profile_init(default_files, &profile);
1123 profile_set_default(profile, mke2fs_default_profile);
1124 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001125
Theodore Ts'o3839e651997-04-26 13:21:57 +00001126 setbuf(stdout, NULL);
1127 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001128 add_error_table(&et_ext2_error_table);
1129 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001130 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1131 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001132
Theodore Ts'o756df352002-03-07 20:52:12 -05001133#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001134 if (uname(&ut)) {
1135 perror("uname");
1136 exit(1);
1137 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001138 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001139 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001140 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001141#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001142
1143 if (argc && *argv) {
1144 program_name = get_progname(*argv);
1145
1146 /* If called as mkfs.ext3, create a journal inode */
Theodore Ts'o1a71bd42009-01-20 12:02:40 -05001147 if (!strcmp(program_name, "mkfs.ext3") ||
1148 !strcmp(program_name, "mke3fs"))
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001149 journal_size = -1;
1150 }
1151
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001152 while ((c = getopt (argc, argv,
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001153 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:U:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001154 switch (c) {
1155 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001156 blocksize = strtol(optarg, &tmp, 0);
1157 b = (blocksize > 0) ? blocksize : -blocksize;
1158 if (b < EXT2_MIN_BLOCK_SIZE ||
1159 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001160 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001161 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001162 exit(1);
1163 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001164 if (blocksize > 4096)
1165 fprintf(stderr, _("Warning: blocksize %d not "
1166 "usable on most systems.\n"),
1167 blocksize);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001168 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001169 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001170 int_log2(blocksize >>
1171 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001172 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001173 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001174 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001175 break;
1176 case 'f':
1177 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001178 if (size < EXT2_MIN_BLOCK_SIZE ||
1179 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001180 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001181 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001182 optarg);
1183 exit(1);
1184 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001185 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001186 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001187 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001188 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001189 break;
1190 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001191 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001192 if (*tmp) {
1193 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001194 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001195 exit(1);
1196 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001197 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001198 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001199 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001200 exit(1);
1201 }
1202 break;
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001203 case 'G':
1204 flex_bg_size = strtoul(optarg, &tmp, 0);
1205 if (*tmp) {
1206 com_err(program_name, 0,
1207 _("Illegal number for flex_bg size"));
1208 exit(1);
1209 }
1210 if (flex_bg_size < 2 ||
1211 (flex_bg_size & (flex_bg_size-1)) != 0) {
1212 com_err(program_name, 0,
1213 _("flex_bg size must be a power of 2"));
1214 exit(1);
1215 }
1216 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001217 case 'i':
1218 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001219 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1220 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001221 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001222 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001223 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001224 optarg, EXT2_MIN_BLOCK_SIZE,
1225 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001226 exit(1);
1227 }
1228 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001229 case 'J':
1230 parse_journal_opts(optarg);
1231 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001232 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001233 if (!journal_size)
1234 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001235 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001236 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001237 bad_blocks_filename = malloc(strlen(optarg)+1);
1238 if (!bad_blocks_filename) {
1239 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001240 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001241 exit(1);
1242 }
1243 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001244 break;
1245 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001246 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o8d822452009-03-06 02:23:59 -05001247 if ( *tmp || reserved_ratio > 50 ||
1248 reserved_ratio < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001249 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001250 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001251 optarg);
1252 exit(1);
1253 }
1254 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001255 case 'n':
1256 noaction++;
1257 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001258 case 'o':
1259 creator_os = optarg;
1260 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001261 case 'q':
1262 quiet = 1;
1263 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001264 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001265 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001266 if (*tmp) {
1267 com_err(program_name, 0,
1268 _("bad revision level - %s"), optarg);
1269 exit(1);
1270 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001271 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001272 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001273 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001274 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001275 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001276 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001277 inode_size = strtoul(optarg, &tmp, 0);
1278 if (*tmp) {
1279 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001280 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001281 exit(1);
1282 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001283 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001284 case 'v':
1285 verbose = 1;
1286 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001287 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001288 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001289 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001290 case 'L':
1291 volume_label = optarg;
1292 break;
1293 case 'M':
1294 mount_dir = optarg;
1295 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001296 case 'N':
1297 num_inodes = strtoul(optarg, &tmp, 0);
1298 if (*tmp) {
1299 com_err(program_name, 0,
1300 _("bad num inodes - %s"), optarg);
1301 exit(1);
1302 }
1303 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001304 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001305 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001306 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001307 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001308 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001309 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001310 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001311 case 'S':
1312 super_only = 1;
1313 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001314 case 't':
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001315 fs_type = optarg;
1316 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001317 case 'T':
1318 usage_types = optarg;
1319 break;
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001320 case 'U':
1321 fs_uuid = optarg;
1322 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001323 case 'V':
1324 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001325 show_version_only++;
1326 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001327 default:
1328 usage();
1329 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001330 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001331 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001332 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001333 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001334
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001335 if (!quiet || show_version_only)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001336 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
Theodore Ts'o38798572003-04-16 15:29:39 -04001337 E2FSPROGS_DATE);
1338
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001339 if (show_version_only) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001340 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001341 error_message(EXT2_ET_BASE));
1342 exit(0);
1343 }
1344
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001345 /*
1346 * If there's no blocksize specified and there is a journal
1347 * device, use it to figure out the blocksize
1348 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001349 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001350 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001351 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001352
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001353#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001354 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1355 io_ptr = test_io_manager;
1356 test_io_backing_manager = unix_io_manager;
1357 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001358#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001359 io_ptr = unix_io_manager;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001360 retval = ext2fs_open(journal_device,
1361 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001362 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001363 if (retval) {
1364 com_err(program_name, retval,
1365 _("while trying to open journal device %s\n"),
1366 journal_device);
1367 exit(1);
1368 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001369 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001370 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001371 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001372 "minimum blocksize %d\n"), jfs->blocksize,
1373 -blocksize);
1374 exit(1);
1375 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001376 blocksize = jfs->blocksize;
Theodore Ts'of8df04b2008-07-06 20:57:17 -04001377 printf(_("Using journal device's blocksize: %d\n"), blocksize);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001378 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001379 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1380 ext2fs_close(jfs);
1381 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001382
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001383 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001384 if (!force) {
1385 com_err(program_name, 0,
1386 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001387 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001388 proceed_question();
1389 }
1390 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1391 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001392 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001393 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001394 if (optind < argc) {
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001395 ext2fs_blocks_count_set(&fs_param, parse_num_blocks(argv[optind++],
1396 fs_param.s_log_block_size));
1397 if (!ext2fs_blocks_count(&fs_param)) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001398 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001399 argv[optind - 1]);
1400 exit(1);
1401 }
1402 }
1403 if (optind < argc)
1404 usage();
1405
Theodore Ts'o74becf31997-04-26 14:37:06 +00001406 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001407 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001408 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001409
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001410 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001411
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001412 if (noaction && ext2fs_blocks_count(&fs_param)) {
1413 dev_size = ext2fs_blocks_count(&fs_param);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001414 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001415 } else {
1416 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001417 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001418 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001419 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001420 if ((retval == EFBIG) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001421 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001422 (fs_param.s_log_block_size == 0)) {
1423 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001424 blocksize = 4096;
1425 goto retry;
1426 }
1427 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001428
Theodore Ts'of8df04b2008-07-06 20:57:17 -04001429 if (retval == EFBIG) {
Theodore Ts'o37d17a62009-01-20 14:03:03 -05001430 blk64_t big_dev_size;
1431
1432 if (blocksize < 4096) {
1433 fs_param.s_log_block_size = 2;
1434 blocksize = 4096;
1435 }
1436 retval = ext2fs_get_device_size2(device_name,
1437 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1438 if (retval)
1439 goto get_size_failure;
1440 if (big_dev_size == (1ULL << 32)) {
1441 dev_size = (blk_t) (big_dev_size - 1);
1442 goto got_size;
1443 }
Theodore Ts'of8df04b2008-07-06 20:57:17 -04001444 fprintf(stderr, _("%s: Size of device %s too big "
1445 "to be expressed in 32 bits\n\t"
1446 "using a blocksize of %d.\n"),
1447 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1448 exit(1);
1449 }
Theodore Ts'o37d17a62009-01-20 14:03:03 -05001450get_size_failure:
Theodore Ts'oa789d841998-03-30 01:20:55 +00001451 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1452 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001453 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001454 exit(1);
1455 }
Theodore Ts'o37d17a62009-01-20 14:03:03 -05001456got_size:
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001457 if (!ext2fs_blocks_count(&fs_param)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001458 if (retval == EXT2_ET_UNIMPLEMENTED) {
1459 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001460 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001461 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001462 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001463 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001464 } else {
1465 if (dev_size == 0) {
1466 com_err(program_name, 0,
1467 _("Device size reported to be zero. "
1468 "Invalid partition specified, or\n\t"
1469 "partition table wasn't reread "
1470 "after running fdisk, due to\n\t"
1471 "a modified partition being busy "
1472 "and in use. You may need to reboot\n\t"
1473 "to re-read your partition table.\n"
1474 ));
1475 exit(1);
1476 }
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001477 ext2fs_blocks_count_set(&fs_param, dev_size);
1478 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param)) {
1479 blk64_t tmp = ext2fs_blocks_count(&fs_param);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001480
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001481 tmp &= ~((blk64_t) ((sys_page_size /
1482 EXT2_BLOCK_SIZE(&fs_param))-1));
1483 ext2fs_blocks_count_set(&fs_param, tmp);
1484 }
1485 }
1486 } else if (!force && (ext2fs_blocks_count(&fs_param) > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001487 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001488 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001489 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001490 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001491
Theodore Ts'o3d438362008-02-19 08:32:58 -05001492 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1493 if (!fs_types) {
1494 fprintf(stderr, _("Failed to parse fs types list\n"));
1495 exit(1);
1496 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001497
1498 /* Figure out what features should be enabled */
1499
Theodore Ts'o3d438362008-02-19 08:32:58 -05001500 tmp = NULL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001501 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001502 tmp = get_string_from_profile(fs_types, "base_features",
1503 "sparse_super,filetype,resize_inode,dir_index");
1504 edit_feature(tmp, &fs_param.s_feature_compat);
1505 free(tmp);
1506
1507 for (cpp = fs_types; *cpp; cpp++) {
1508 tmp = NULL;
1509 profile_get_string(profile, "fs_types", *cpp,
1510 "features", "", &tmp);
1511 if (tmp && *tmp)
1512 edit_feature(tmp, &fs_param.s_feature_compat);
Jim Meyering45e338f2009-02-23 18:07:50 +01001513 free(tmp);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001514 }
1515 tmp = get_string_from_profile(fs_types, "default_features",
1516 "");
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001517 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001518 edit_feature(fs_features ? fs_features : tmp,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001519 &fs_param.s_feature_compat);
Jim Meyering45e338f2009-02-23 18:07:50 +01001520 free(tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001521
Theodore Ts'ob4d51052008-07-06 20:24:29 -04001522 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1523 fs_types[0] = strdup("journal");
1524 fs_types[1] = 0;
1525 }
1526
1527 if (verbose) {
1528 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1529 print_str_list(fs_types);
1530 }
1531
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001532 if (r_opt == EXT2_GOOD_OLD_REV &&
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001533 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
Theodore Ts'o92fb8542008-07-18 21:08:56 -04001534 fs_param.s_feature_ro_compat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001535 fprintf(stderr, _("Filesystem features not supported "
1536 "with revision 0 filesystems\n"));
1537 exit(1);
1538 }
1539
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001540 if (s_opt > 0) {
1541 if (r_opt == EXT2_GOOD_OLD_REV) {
1542 fprintf(stderr, _("Sparse superblocks not supported "
1543 "with revision 0 filesystems\n"));
1544 exit(1);
1545 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001546 fs_param.s_feature_ro_compat |=
1547 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001548 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001549 fs_param.s_feature_ro_compat &=
1550 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1551
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001552 if (journal_size != 0) {
1553 if (r_opt == EXT2_GOOD_OLD_REV) {
1554 fprintf(stderr, _("Journals not supported "
1555 "with revision 0 filesystems\n"));
1556 exit(1);
1557 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001558 fs_param.s_feature_compat |=
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001559 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001560 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001561
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001562 if (fs_param.s_feature_incompat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001563 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001564 reserved_ratio = 0;
1565 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1566 fs_param.s_feature_compat = 0;
1567 fs_param.s_feature_ro_compat = 0;
1568 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001569
1570 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1571 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1572 fprintf(stderr, _("The resize_inode and meta_bg features "
1573 "are not compatible.\n"
1574 "They can not be both enabled "
1575 "simultaneously.\n"));
1576 exit(1);
1577 }
1578
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001579 /* Set first meta blockgroup via an environment variable */
1580 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001581 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001582 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001583 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001584
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001585 /* Get the hardware sector size, if available */
1586 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1587 if (retval) {
1588 com_err(program_name, retval,
1589 _("while trying to determine hardware sector size"));
1590 exit(1);
1591 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001592
Theodore Ts'o54434922003-12-07 01:28:50 -05001593 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001594 sector_size = atoi(tmp);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001595
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001596 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001597 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001598
1599 if (use_bsize == -1) {
1600 use_bsize = sys_page_size;
1601 if ((linux_version_code < (2*65536 + 6*256)) &&
1602 (use_bsize > 4096))
1603 use_bsize = 4096;
1604 }
1605 if (sector_size && use_bsize < sector_size)
1606 use_bsize = sector_size;
1607 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1608 use_bsize = -blocksize;
1609 blocksize = use_bsize;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001610 ext2fs_blocks_count_set(&fs_param,
1611 ext2fs_blocks_count(&fs_param) /
1612 (blocksize / 1024));
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001613 }
1614
1615 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001616 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1617 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001618 if (inode_ratio < blocksize)
1619 inode_ratio = blocksize;
1620 }
1621
1622 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1623 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1624
Eric Sandeen9ed8e5f2009-10-02 11:32:42 -05001625#ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1626 retval = ext2fs_get_device_geometry(device_name, &fs_param);
1627 if (retval < 0) {
1628 fprintf(stderr,
1629 _("warning: Unable to get device geometry for %s"),
1630 device_name);
1631 } else if (retval) {
1632 printf(_("%s alignment is offset by %lu bytes.\n"),
1633 device_name, retval);
1634 printf(_("This may result in very poor performance, "
1635 "(re)-partitioning suggested.\n"));
1636 proceed_question();
1637 }
1638#endif
1639
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001640 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001641
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001642 lazy_itable_init = get_bool_from_profile(fs_types,
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001643 "lazy_itable_init", 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001644
Theodore Ts'o2d363582008-05-14 18:09:37 -04001645 /* Get options from profile */
1646 for (cpp = fs_types; *cpp; cpp++) {
1647 tmp = NULL;
1648 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1649 if (tmp && *tmp)
1650 parse_extended_opts(&fs_param, tmp);
Jim Meyering45e338f2009-02-23 18:07:50 +01001651 free(tmp);
Theodore Ts'o2d363582008-05-14 18:09:37 -04001652 }
1653
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001654 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001655 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001656
1657 /* Since sparse_super is the default, we would only have a problem
1658 * here if it was explicitly disabled.
1659 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001660 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1661 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001662 com_err(program_name, 0,
1663 _("reserved online resize blocks not supported "
1664 "on non-sparse filesystem"));
1665 exit(1);
1666 }
1667
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001668 if (fs_param.s_blocks_per_group) {
1669 if (fs_param.s_blocks_per_group < 256 ||
1670 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001671 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001672 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001673 exit(1);
1674 }
1675 }
1676
Theodore Ts'o3d438362008-02-19 08:32:58 -05001677 if (inode_size == 0)
1678 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Theodore Ts'o9ba40002008-04-22 08:27:01 -04001679 if (!flex_bg_size && (fs_param.s_feature_incompat &
1680 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1681 flex_bg_size = get_int_from_profile(fs_types,
1682 "flex_bg_size", 16);
1683 if (flex_bg_size) {
1684 if (!(fs_param.s_feature_incompat &
1685 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1686 com_err(program_name, 0,
1687 _("Flex_bg feature not enabled, so "
1688 "flex_bg size may not be specified"));
1689 exit(1);
1690 }
1691 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1692 }
Andreas Dilger067911a2006-07-15 22:08:20 -04001693
1694 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001695 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001696 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001697 inode_size & (inode_size - 1)) {
1698 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001699 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001700 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001701 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001702 exit(1);
1703 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001704 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001705 }
1706
Eric Sandeenf3358642006-09-12 14:56:17 -04001707 /* Make sure number of inodes specified will fit in 32 bits */
1708 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001709 unsigned long long n;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001710 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001711 if (n > ~0U) {
1712 com_err(program_name, 0,
1713 _("too many inodes (%llu), raise inode ratio?"), n);
1714 exit(1);
1715 }
1716 } else if (num_inodes > ~0U) {
1717 com_err(program_name, 0,
1718 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001719 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001720 exit(1);
1721 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001722 /*
1723 * Calculate number of inodes based on the inode ratio
1724 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001725 fs_param.s_inodes_count = num_inodes ? num_inodes :
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001726 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001727
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001728 if ((((long long)fs_param.s_inodes_count) *
1729 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001730 ((ext2fs_blocks_count(&fs_param)) *
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001731 EXT2_BLOCK_SIZE(&fs_param))) {
1732 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1733 "(%u) too big for a\n\t"
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001734 "filesystem with %llu blocks, "
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001735 "specify higher inode_ratio (-i)\n\t"
1736 "or lower inode count (-N).\n"),
1737 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001738 fs_param.s_inodes_count,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001739 (unsigned long long) ext2fs_blocks_count(&fs_param));
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001740 exit(1);
1741 }
1742
Theodore Ts'o3839e651997-04-26 13:21:57 +00001743 /*
1744 * Calculate number of blocks to reserve
1745 */
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001746 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1747 ext2fs_blocks_count(&fs_param) / 100.0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001748}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001749
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301750static int should_do_undo(const char *name)
1751{
1752 errcode_t retval;
1753 io_channel channel;
1754 __u16 s_magic;
1755 struct ext2_super_block super;
1756 io_manager manager = unix_io_manager;
1757 int csum_flag, force_undo;
1758
1759 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1760 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1761 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1762 if (!force_undo && (!csum_flag || !lazy_itable_init))
1763 return 0;
1764
1765 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1766 if (retval) {
1767 /*
1768 * We don't handle error cases instead we
1769 * declare that the file system doesn't exist
1770 * and let the rest of mke2fs take care of
1771 * error
1772 */
1773 retval = 0;
1774 goto open_err_out;
1775 }
1776
1777 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -04001778 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301779 if (retval) {
1780 retval = 0;
1781 goto err_out;
1782 }
1783
1784#if defined(WORDS_BIGENDIAN)
1785 s_magic = ext2fs_swab16(super.s_magic);
1786#else
1787 s_magic = super.s_magic;
1788#endif
1789
1790 if (s_magic == EXT2_SUPER_MAGIC)
1791 retval = 1;
1792
1793err_out:
1794 io_channel_close(channel);
1795
1796open_err_out:
1797
1798 return retval;
1799}
1800
1801static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1802{
1803 errcode_t retval = 0;
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001804 char *tdb_dir, *tdb_file;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301805 char *device_name, *tmp_name;
1806
1807 /*
1808 * Configuration via a conf file would be
1809 * nice
1810 */
1811 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1812 if (!tdb_dir)
1813 profile_get_string(profile, "defaults",
1814 "undo_dir", 0, "/var/lib/e2fsprogs",
1815 &tdb_dir);
1816
1817 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1818 access(tdb_dir, W_OK))
1819 return 0;
1820
1821 tmp_name = strdup(name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001822 if (!tmp_name) {
1823 alloc_fn_fail:
1824 com_err(program_name, ENOMEM,
1825 _("Couldn't allocate memory for tdb filename\n"));
1826 return ENOMEM;
1827 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301828 device_name = basename(tmp_name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001829 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1830 if (!tdb_file)
1831 goto alloc_fn_fail;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301832 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1833
1834 if (!access(tdb_file, F_OK)) {
1835 if (unlink(tdb_file) < 0) {
1836 retval = errno;
1837 com_err(program_name, retval,
1838 _("while trying to delete %s"),
1839 tdb_file);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001840 free(tdb_file);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301841 return retval;
1842 }
1843 }
1844
1845 set_undo_io_backing_manager(*io_ptr);
1846 *io_ptr = undo_io_manager;
1847 set_undo_io_backup_file(tdb_file);
1848 printf(_("Overwriting existing filesystem; this can be undone "
1849 "using the command:\n"
1850 " e2undo %s %s\n\n"), tdb_file, name);
Eric Sandeen79e62402008-07-06 18:36:56 -04001851
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001852 free(tdb_file);
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301853 free(tmp_name);
1854 return retval;
1855}
1856
Theodore Ts'o3839e651997-04-26 13:21:57 +00001857int main (int argc, char *argv[])
1858{
1859 errcode_t retval = 0;
1860 ext2_filsys fs;
1861 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001862 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001863 unsigned int i;
Theodore Ts'od5f57d92008-08-29 21:39:36 -04001864 int val, hash_alg;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04001865 int flags;
1866 int old_bitmaps;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001867 io_manager io_ptr;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301868 char tdb_string[40];
Theodore Ts'od5f57d92008-08-29 21:39:36 -04001869 char *hash_alg_str;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001870
1871#ifdef ENABLE_NLS
1872 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001873 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001874 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1875 textdomain(NLS_CAT_NAME);
1876#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001877 PRS(argc, argv);
1878
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001879#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001880 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1881 io_ptr = test_io_manager;
1882 test_io_backing_manager = unix_io_manager;
1883 } else
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001884#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001885 io_ptr = unix_io_manager;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001886
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301887 if (should_do_undo(device_name)) {
1888 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1889 if (retval)
1890 exit(1);
1891 }
1892
Theodore Ts'o3839e651997-04-26 13:21:57 +00001893 /*
1894 * Initialize the superblock....
1895 */
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04001896 flags = EXT2_FLAG_EXCLUSIVE;
1897 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
1898 &old_bitmaps);
1899 if (!old_bitmaps)
1900 flags |= EXT2_FLAG_64BITS;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04001901 /*
1902 * By default, we print how many inode tables or block groups
1903 * or whatever we've written so far. The quiet flag disables
1904 * this, along with a lot of other output.
1905 */
1906 if (!quiet)
1907 flags |= EXT2_FLAG_PRINT_PROGRESS;
Valerie Aurora Henson463e7322009-08-05 00:17:56 -04001908 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001909 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001910 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001911 exit(1);
1912 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04001913
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301914 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1915 32768 : fs->blocksize * 8);
1916 io_channel_set_options(fs->io, tdb_string);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001917
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001918 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1919 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1920
Theodore Ts'ob7c5b402009-03-05 19:40:20 -05001921 if ((fs_param.s_feature_incompat &
1922 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
1923 (fs_param.s_feature_ro_compat &
1924 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1925 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1926 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
1927 fs->super->s_kbytes_written = 1;
1928
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001929 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001930 * Wipe out the old on-disk superblock
1931 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001932 if (!noaction)
1933 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001934
1935 /*
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001936 * Parse or generate a UUID for the filesystem
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001937 */
Theodore Ts'ob0afdda2009-01-20 13:18:23 -05001938 if (fs_uuid) {
1939 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
1940 com_err(device_name, 0, "could not parse UUID: %s\n",
1941 fs_uuid);
1942 exit(1);
1943 }
1944 } else
1945 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001946
1947 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001948 * Initialize the directory index variables
1949 */
Theodore Ts'od5f57d92008-08-29 21:39:36 -04001950 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
1951 "half_md4");
1952 hash_alg = e2p_string2hash(hash_alg_str);
1953 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
1954 EXT2_HASH_HALF_MD4;
Theodore Ts'o843049c2002-09-22 15:37:40 -04001955 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1956
1957 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001958 * Add "jitter" to the superblock's check interval so that we
1959 * don't check all the filesystems at the same time. We use a
1960 * kludgy hack of using the UUID to derive a random jitter value.
1961 */
1962 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1963 val += fs->super->s_uuid[i];
1964 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1965
1966 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001967 * Override the creator OS, if applicable
1968 */
1969 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001970 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001971 exit(1);
1972 }
1973
1974 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001975 * For the Hurd, we will turn off filetype since it doesn't
1976 * support it.
1977 */
1978 if (fs->super->s_creator_os == EXT2_OS_HURD)
1979 fs->super->s_feature_incompat &=
1980 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1981
1982 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001983 * Set the volume label...
1984 */
1985 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001986 memset(fs->super->s_volume_name, 0,
1987 sizeof(fs->super->s_volume_name));
1988 strncpy(fs->super->s_volume_name, volume_label,
1989 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001990 }
1991
1992 /*
1993 * Set the last mount directory
1994 */
1995 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001996 memset(fs->super->s_last_mounted, 0,
1997 sizeof(fs->super->s_last_mounted));
1998 strncpy(fs->super->s_last_mounted, mount_dir,
1999 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002000 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002001
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002002 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00002003 show_stats(fs);
2004
Theodore Ts'o50787ea1999-07-19 15:30:21 +00002005 if (noaction)
2006 exit(0);
2007
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002008 if (fs->super->s_feature_incompat &
2009 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2010 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06002011 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002012 }
2013
Theodore Ts'o3839e651997-04-26 13:21:57 +00002014 if (bad_blocks_filename)
2015 read_bb_file(fs, &bb_list, bad_blocks_filename);
2016 if (cflag)
2017 test_disk(fs, &bb_list);
2018
2019 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05002020 fs->stride = fs_stride = fs->super->s_raid_stride;
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002021 if (!quiet)
2022 printf(_("Allocating group tables: "));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002023 retval = ext2fs_allocate_tables(fs);
2024 if (retval) {
2025 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002026 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002027 exit(1);
2028 }
Valerie Aurora Henson95fd65b2009-08-23 19:20:03 -04002029 if (!quiet)
2030 printf(_("done \n"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00002031 if (super_only) {
2032 fs->super->s_state |= EXT2_ERROR_FS;
2033 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2034 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06002035 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05002036 unsigned int rsv = 65536 / fs->blocksize;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04002037 unsigned long blocks = ext2fs_blocks_count(fs->super);
Andreas Dilger59f27242001-08-30 15:39:04 -06002038 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002039 blk_t ret_blk;
2040
2041#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04002042 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002043#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06002044
2045 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002046 * Wipe out any old MD RAID (or other) metadata at the end
2047 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06002048 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002049 */
Andreas Dilger59f27242001-08-30 15:39:04 -06002050 start = (blocks & ~(rsv - 1));
2051 if (start > rsv)
2052 start -= rsv;
2053 if (start > 0)
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05302054 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2055 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06002056
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002057 if (retval) {
2058 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04002059 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002060 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00002061 }
Theodore Ts'oa4396e92008-04-18 10:19:27 -04002062 write_inode_tables(fs, lazy_itable_init);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002063 create_root_dir(fs);
2064 create_lost_and_found(fs);
2065 reserve_inodes(fs);
2066 create_bad_block_inode(fs, bb_list);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002067 if (fs->super->s_feature_compat &
Theodore Ts'oea774312005-01-28 11:45:28 -05002068 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2069 retval = ext2fs_create_resize_inode(fs);
2070 if (retval) {
2071 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002072 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05002073 exit(1);
2074 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05002075 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00002076 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002077
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002078 if (journal_device) {
2079 ext2_filsys jfs;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002080
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002081 if (!force)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002082 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00002083 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002084
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002085 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2086 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2087 fs->blocksize, unix_io_manager, &jfs);
2088 if (retval) {
2089 com_err(program_name, retval,
2090 _("while trying to open journal device %s\n"),
2091 journal_device);
2092 exit(1);
2093 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002094 if (!quiet) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002095 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002096 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002097 fflush(stdout);
2098 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002099 retval = ext2fs_add_journal_device(fs, jfs);
2100 if(retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002101 com_err (program_name, retval,
2102 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00002103 journal_device);
2104 exit(1);
2105 }
2106 if (!quiet)
2107 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00002108 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06002109 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002110 } else if ((journal_size) ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002111 (fs_param.s_feature_compat &
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05002112 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002113 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002114
Theodore Ts'oa620bad2009-03-31 07:42:24 -04002115 if (super_only) {
2116 printf(_("Skipping journal creation in super-only mode\n"));
2117 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2118 goto no_journal;
2119 }
2120
Theodore Ts'o93345d12001-02-17 06:09:50 +00002121 if (!journal_blocks) {
2122 fs->super->s_feature_compat &=
2123 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2124 goto no_journal;
2125 }
2126 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04002127 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00002128 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002129 fflush(stdout);
2130 }
Theodore Ts'o63985322001-01-03 17:02:13 +00002131 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2132 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002133 if (retval) {
2134 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00002135 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00002136 exit(1);
2137 }
2138 if (!quiet)
2139 printf(_("done\n"));
2140 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002141no_journal:
2142
Theodore Ts'o3839e651997-04-26 13:21:57 +00002143 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002144 printf(_("Writing superblocks and "
2145 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002146 retval = ext2fs_flush(fs);
2147 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05002148 fprintf(stderr,
2149 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002150 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002151 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002152 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002153 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2154 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002155 }
Andreas Dilger568101f2001-10-13 01:22:25 -06002156 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002157 remove_error_table(&et_ext2_error_table);
2158 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002159 profile_release(profile);
Andreas Dilger568101f2001-10-13 01:22:25 -06002160 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002161}