blob: ee608cd2657b75a5e90e817b43058ad786349315 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05004 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 */
12
13/* Usage: mke2fs [options] device
14 *
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
17 */
18
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000019#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050024#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000025#include <sys/utsname.h>
26#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000027#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000029#else
30extern char *optarg;
31extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#endif
33#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000035#endif
36#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#endif
39#ifdef HAVE_ERRNO_H
40#include <errno.h>
41#endif
42#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000044#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <sys/types.h>
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053047#include <libgen.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000048
Theodore Ts'o54c637d2001-05-14 11:45:38 +000049#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000050#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000051#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000052#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000054#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050055#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050056#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000058#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
60#define STRIDE_LENGTH 8
61
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000062#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000063#define ZAP_BOOTBLOCK
64#endif
65
Theodore Ts'o3839e651997-04-26 13:21:57 +000066extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000067extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
69const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000070const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
72/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000073int cflag;
74int verbose;
75int quiet;
76int super_only;
77int force;
78int noaction;
79int journal_size;
80int journal_flags;
Theodore Ts'oa4396e92008-04-18 10:19:27 -040081int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000082char *bad_blocks_filename;
83__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050085struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000086char *creator_os;
87char *volume_label;
88char *mount_dir;
89char *journal_device;
90int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +053091char **fs_types;
Theodore Ts'o3839e651997-04-26 13:21:57 +000092
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050093profile_t profile;
94
Theodore Ts'o31e29a12002-05-17 10:53:07 -040095int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040096int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040097
Theodore Ts'o63985322001-01-03 17:02:13 +000098static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000099{
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500100 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -0400101 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500102 "[-J journal-options]\n"
Andreas Dilger067911a2006-07-15 22:08:20 -0400103 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
104 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
105 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500106 "[-r fs-revision] [-E extended-option[,...]]\n"
107 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108 program_name);
109 exit(1);
110}
111
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000112static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113{
114 int l = 0;
115
116 arg >>= 1;
117 while (arg) {
118 l++;
119 arg >>= 1;
120 }
121 return l;
122}
123
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000124static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000125{
126 int l;
127
128 for (l=0; arg ; l++)
129 arg = arg / 10;
130 return l;
131}
132
Theodore Ts'od99225e2004-09-25 07:40:12 -0400133static int parse_version_number(const char *s)
134{
135 int major, minor, rev;
136 char *endptr;
137 const char *cp = s;
138
139 if (!s)
140 return 0;
141 major = strtol(cp, &endptr, 10);
142 if (cp == endptr || *endptr != '.')
143 return 0;
144 cp = endptr + 1;
145 minor = strtol(cp, &endptr, 10);
146 if (cp == endptr || *endptr != '.')
147 return 0;
148 cp = endptr + 1;
149 rev = strtol(cp, &endptr, 10);
150 if (cp == endptr)
151 return 0;
152 return ((((major * 256) + minor) * 256) + rev);
153}
154
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000155/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 * Helper function for read_bb_file and test_disk
157 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500158static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159{
Theodore Ts'o66938372002-03-08 00:14:46 -0500160 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 return;
162}
163
164/*
165 * Reads the bad blocks list from a file
166 */
167static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
168 const char *bad_blocks_file)
169{
170 FILE *f;
171 errcode_t retval;
172
173 f = fopen(bad_blocks_file, "r");
174 if (!f) {
175 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000176 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 exit(1);
178 }
179 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
180 fclose (f);
181 if (retval) {
182 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000183 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 exit(1);
185 }
186}
187
188/*
189 * Runs the badblocks program to test the disk
190 */
191static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
192{
193 FILE *f;
194 errcode_t retval;
195 char buf[1024];
196
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400197 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500198 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500199 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000201 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 f = popen(buf, "r");
203 if (!f) {
204 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400205 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 exit(1);
207 }
208 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000209 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 if (retval) {
211 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000212 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213 exit(1);
214 }
215}
216
217static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
218{
Theodore Ts'o54434922003-12-07 01:28:50 -0500219 dgrp_t i;
220 blk_t j;
221 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 blk_t blk;
223 badblocks_iterate bb_iter;
224 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000225 blk_t group_block;
226 int group;
227 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228
229 if (!bb_list)
230 return;
231
232 /*
233 * The primary superblock and group descriptors *must* be
234 * good; if not, abort.
235 */
236 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
237 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000238 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000239 fprintf(stderr, _("Block %d in primary "
240 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400241 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000242 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500244 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 exit(1);
246 }
247 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000248
249 /*
250 * See if any of the bad blocks are showing up in the backup
251 * superblocks and/or group descriptors. If so, issue a
252 * warning and adjust the block counts appropriately.
253 */
254 group_block = fs->super->s_first_data_block +
255 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000256
257 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000258 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000259 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000260 if (ext2fs_badblocks_list_test(bb_list,
261 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000262 if (!group_bad)
263 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500264_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000265" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000266 group_block);
267 group_bad++;
268 group = ext2fs_group_of_blk(fs, group_block+j);
269 fs->group_desc[group].bg_free_blocks_count++;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400270 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000271 fs->super->s_free_blocks_count++;
272 }
273 }
274 group_block += fs->super->s_blocks_per_group;
275 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276
277 /*
278 * Mark all the bad blocks as used...
279 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000280 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000282 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000283 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 exit(1);
285 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000286 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000287 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000288 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289}
290
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000291/*
292 * These functions implement a generalized progress meter.
293 */
294struct progress_struct {
295 char format[20];
296 char backup[80];
297 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400298 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000299};
300
301static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500302 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000303{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000304 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000305
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000306 memset(progress, 0, sizeof(struct progress_struct));
307 if (quiet)
308 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000309
310 /*
311 * Figure out how many digits we need
312 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000313 i = int_log10(max);
314 sprintf(progress->format, "%%%dd/%%%dld", i, i);
315 memset(progress->backup, '\b', sizeof(progress->backup)-1);
316 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500317 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000318 progress->backup[(2*i)+1] = 0;
319 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000320
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400321 progress->skip_progress = 0;
322 if (getenv("MKE2FS_SKIP_PROGRESS"))
323 progress->skip_progress++;
324
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000325 fputs(label, stdout);
326 fflush(stdout);
327}
328
329static void progress_update(struct progress_struct *progress, __u32 val)
330{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400331 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000332 return;
333 printf(progress->format, val, progress->max);
334 fputs(progress->backup, stdout);
335}
336
337static void progress_close(struct progress_struct *progress)
338{
339 if (progress->format[0] == 0)
340 return;
341 fputs(_("done \n"), stdout);
342}
343
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400344static void write_inode_tables(ext2_filsys fs, int lazy_flag)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000345{
346 errcode_t retval;
347 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500348 dgrp_t i;
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400349 int num, ipb;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000350 struct progress_struct progress;
351
352 if (quiet)
353 memset(&progress, 0, sizeof(progress));
354 else
355 progress_init(&progress, _("Writing inode tables: "),
356 fs->group_desc_count);
357
Theodore Ts'o3839e651997-04-26 13:21:57 +0000358 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000359 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000361 blk = fs->group_desc[i].bg_inode_table;
362 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000363
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400364 if (lazy_flag) {
365 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400366 num = ((((fs->super->s_inodes_per_group -
367 fs->group_desc[i].bg_itable_unused) *
368 EXT2_INODE_SIZE(fs->super)) +
369 EXT2_BLOCK_SIZE(fs->super) - 1) /
370 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400371 } else {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500372 /* The kernel doesn't need to zero the itable blocks */
373 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400374 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000375 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530376 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400377 if (retval) {
378 fprintf(stderr, _("\nCould not write %d "
379 "blocks in inode table starting at %u: %s\n"),
380 num, blk, error_message(retval));
381 exit(1);
382 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000383 if (sync_kludge) {
384 if (sync_kludge == 1)
385 sync();
386 else if ((i % sync_kludge) == 0)
387 sync();
388 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530390 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000391 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392}
393
394static void create_root_dir(ext2_filsys fs)
395{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400396 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000397 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400398 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399
400 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
401 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000402 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 exit(1);
404 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000405 if (geteuid()) {
406 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
407 if (retval) {
408 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000409 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000410 exit(1);
411 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400412 uid = getuid();
413 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500414 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400415 if (uid) {
416 gid = getgid();
417 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500418 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400419 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500420 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000421 if (retval) {
422 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000423 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000424 exit(1);
425 }
426 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427}
428
429static void create_lost_and_found(ext2_filsys fs)
430{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400431 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000433 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 const char *name = "lost+found";
435 int i;
436
Theodore Ts'o6a525062001-12-24 09:40:00 -0500437 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
439 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000440 com_err("ext2fs_mkdir", retval,
441 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 exit(1);
443 }
444
445 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
446 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000447 com_err("ext2_lookup", retval,
448 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 exit(1);
450 }
451
452 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100453 /* Ensure that lost+found is at least 2 blocks, so we always
454 * test large empty blocks for big-block filesystems. */
455 if ((lpf_size += fs->blocksize) >= 16*1024 &&
456 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000457 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 retval = ext2fs_expand_dir(fs, ino);
459 if (retval) {
460 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000461 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 exit(1);
463 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500464 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465}
466
467static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
468{
469 errcode_t retval;
470
Theodore Ts'of3db3561997-04-26 13:34:30 +0000471 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400472 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 retval = ext2fs_update_bb_inode(fs, bb_list);
474 if (retval) {
475 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000476 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 exit(1);
478 }
479
480}
481
482static void reserve_inodes(ext2_filsys fs)
483{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000484 ext2_ino_t i;
485 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400487 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
488 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489 ext2fs_mark_ib_dirty(fs);
490}
491
Theodore Ts'o756df352002-03-07 20:52:12 -0500492#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500493#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500494#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500495
Theodore Ts'o04a96852001-08-30 21:55:26 -0400496static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000497{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400498 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500500 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400502 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600503 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500504 printf(_("Out of memory erasing sectors %d-%d\n"),
505 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600506 exit(1);
507 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500508
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500509 if (sect == 0) {
510 /* Check for a BSD disklabel, and don't erase it if so */
511 retval = io_channel_read_blk(fs->io, 0, -512, buf);
512 if (retval)
513 fprintf(stderr,
514 _("Warning: could not read block 0: %s\n"),
515 error_message(retval));
516 else {
517 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
518 if ((*magic == BSD_DISKMAGIC) ||
519 (*magic == BSD_MAGICDISK))
520 return;
521 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500522 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500523
Theodore Ts'o70988102002-07-14 08:00:00 -0400524 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000525 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400526 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000527 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400528 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000529 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500530 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
531 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000532}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000533
534static void create_journal_dev(ext2_filsys fs)
535{
536 struct progress_struct progress;
537 errcode_t retval;
538 char *buf;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530539 blk_t blk, err_blk;
540 int c, count, err_count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000541
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000542 retval = ext2fs_create_journal_superblock(fs,
543 fs->super->s_blocks_count, 0, &buf);
544 if (retval) {
545 com_err("create_journal_dev", retval,
546 _("while initializing journal superblock"));
547 exit(1);
548 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000549 if (quiet)
550 memset(&progress, 0, sizeof(progress));
551 else
552 progress_init(&progress, _("Zeroing journal device: "),
553 fs->super->s_blocks_count);
554
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530555 blk = 0;
556 count = fs->super->s_blocks_count;
557 while (count > 0) {
558 if (count > 1024)
559 c = 1024;
560 else
561 c = count;
562 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
563 if (retval) {
564 com_err("create_journal_dev", retval,
565 _("while zeroing journal device "
566 "(block %u, count %d)"),
567 err_blk, err_count);
568 exit(1);
569 }
570 blk += c;
571 count -= c;
572 progress_update(&progress, blk);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000573 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +0530574 ext2fs_zero_blocks(0, 0, 0, 0, 0);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000575
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000576 retval = io_channel_write_blk(fs->io,
577 fs->super->s_first_data_block+1,
578 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000579 if (retval) {
580 com_err("create_journal_dev", retval,
581 _("while writing journal superblock"));
582 exit(1);
583 }
584 progress_close(&progress);
585}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000586
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587static void show_stats(ext2_filsys fs)
588{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000589 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000590 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500591 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500593 dgrp_t i;
594 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500596 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500597 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500598 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000599
600 memset(buf, 0, sizeof(buf));
601 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000602 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500603 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500604 os = e2p_os2string(fs->super->s_creator_os);
605 fputs(os, stdout);
606 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000607 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000608 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000609 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000610 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000611 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000612 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000614 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 s->s_r_blocks_count,
616 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000617 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500618 if (s->s_reserved_gdt_blocks)
619 printf(_("Maximum filesystem blocks=%lu\n"),
620 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200621 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000622 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000623 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000624 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000625 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000626 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000628 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629
630 if (fs->group_desc_count == 1) {
631 printf("\n");
632 return;
633 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500634
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000635 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 group_block = s->s_first_data_block;
637 col_left = 0;
638 for (i = 1; i < fs->group_desc_count; i++) {
639 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000640 if (!ext2fs_bg_has_super(fs, i))
641 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000642 if (i != 1)
643 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000644 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000645 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000647 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000649 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000650 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 }
652 printf("\n\n");
653}
654
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000655/*
656 * Set the S_CREATOR_OS field. Return true if OS is known,
657 * otherwise, 0.
658 */
659static int set_os(struct ext2_super_block *sb, char *os)
660{
661 if (isdigit (*os))
662 sb->s_creator_os = atoi (os);
663 else if (strcasecmp(os, "linux") == 0)
664 sb->s_creator_os = EXT2_OS_LINUX;
665 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
666 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500667 else if (strcasecmp(os, "freebsd") == 0)
668 sb->s_creator_os = EXT2_OS_FREEBSD;
669 else if (strcasecmp(os, "lites") == 0)
670 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000671 else
672 return 0;
673 return 1;
674}
675
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000676#define PATH_SET "PATH=/sbin"
677
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500678static void parse_extended_opts(struct ext2_super_block *param,
679 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000680{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400681 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000682 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500683 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000684
685 len = strlen(opts);
686 buf = malloc(len+1);
687 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500688 fprintf(stderr,
689 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000690 exit(1);
691 }
692 strcpy(buf, opts);
693 for (token = buf; token && *token; token = next) {
694 p = strchr(token, ',');
695 next = 0;
696 if (p) {
697 *p = 0;
698 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500699 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000700 arg = strchr(token, '=');
701 if (arg) {
702 *arg = 0;
703 arg++;
704 }
705 if (strcmp(token, "stride") == 0) {
706 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500707 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500708 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000709 continue;
710 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500711 param->s_raid_stride = strtoul(arg, &p, 0);
712 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000713 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400714 _("Invalid stride parameter: %s\n"),
715 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500716 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000717 continue;
718 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500719 } else if (strcmp(token, "stripe-width") == 0 ||
720 strcmp(token, "stripe_width") == 0) {
721 if (!arg) {
722 r_usage++;
723 badopt = token;
724 continue;
725 }
726 param->s_raid_stripe_width = strtoul(arg, &p, 0);
727 if (*p || (param->s_raid_stripe_width == 0)) {
728 fprintf(stderr,
729 _("Invalid stripe-width parameter: %s\n"),
730 arg);
731 r_usage++;
732 continue;
733 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500734 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500735 unsigned long resize, bpg, rsv_groups;
736 unsigned long group_desc_count, desc_blocks;
737 unsigned int gdpb, blocksize;
738 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500739
740 if (!arg) {
741 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500742 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500743 continue;
744 }
745
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500746 resize = parse_num_blocks(arg,
747 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500748
749 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500750 fprintf(stderr,
751 _("Invalid resize parameter: %s\n"),
752 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500753 r_usage++;
754 continue;
755 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500756 if (resize <= param->s_blocks_count) {
757 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400758 _("The resize maximum must be greater "
759 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500760 r_usage++;
761 continue;
762 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500763
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500764 blocksize = EXT2_BLOCK_SIZE(param);
765 bpg = param->s_blocks_per_group;
766 if (!bpg)
767 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200768 gdpb = EXT2_DESC_PER_BLOCK(param);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400769 group_desc_count =
770 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500771 desc_blocks = (group_desc_count +
772 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400773 rsv_groups = ext2fs_div_ceil(resize, bpg);
774 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500775 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500776 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500777 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
778
779 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400780 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
781 fprintf(stderr,
782 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400783 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400784 exit(1);
785 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500786 param->s_feature_compat |=
787 EXT2_FEATURE_COMPAT_RESIZE_INODE;
788
789 param->s_reserved_gdt_blocks = rsv_gdb;
790 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500791 } else if (!strcmp(token, "test_fs")) {
792 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400793 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400794 if (arg)
795 lazy_itable_init = strtoul(arg, &p, 0);
796 else
797 lazy_itable_init = 1;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500798 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500799 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500800 badopt = token;
801 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000802 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500803 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500804 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400805 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000806 "and may take an argument which\n"
807 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400808 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500809 "\tstride=<RAID per-disk data chunk in blocks>\n"
810 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400811 "\tresize=<resize maximum size in blocks>\n"
812 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
813 "\ttest_fs\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400814 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400815 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000816 exit(1);
817 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500818 if (param->s_raid_stride &&
819 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
820 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
821 "multiple of stride %u.\n\n"),
822 param->s_raid_stripe_width, param->s_raid_stride);
823
Brian Behlendorf21400382007-05-31 11:30:47 -0400824 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000825}
826
Theodore Ts'o896938d1999-10-23 01:04:50 +0000827static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500828 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400829 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500830 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400831 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500832 EXT2_FEATURE_COMPAT_EXT_ATTR,
833 /* Incompat */
834 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500835 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400836 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500837 EXT2_FEATURE_INCOMPAT_META_BG|
838 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500839 /* R/O compat */
840 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500841 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
842 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000843};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000844
845
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500846static void syntax_err_report(const char *filename, long err, int line_num)
847{
848 fprintf(stderr,
849 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
850 filename, line_num, error_message(err));
851 exit(1);
852}
853
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200854static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500855
856static void edit_feature(const char *str, __u32 *compat_array)
857{
858 if (!str)
859 return;
860
861 if (e2p_edit_feature(str, compat_array, ok_features)) {
862 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
863 str);
864 exit(1);
865 }
866}
867
Theodore Ts'o3d438362008-02-19 08:32:58 -0500868struct str_list {
869 char **list;
870 int num;
871 int max;
872};
873
874static errcode_t init_list(struct str_list *sl)
875{
876 sl->num = 0;
877 sl->max = 0;
878 sl->list = malloc((sl->max+1) * sizeof(char *));
879 if (!sl->list)
880 return ENOMEM;
881 sl->list[0] = 0;
882 return 0;
883}
884
885static errcode_t push_string(struct str_list *sl, const char *str)
886{
887 char **new_list;
888
889 if (sl->num >= sl->max) {
890 sl->max += 2;
891 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
892 if (!new_list)
893 return ENOMEM;
894 sl->list = new_list;
895 }
896 sl->list[sl->num] = malloc(strlen(str)+1);
897 if (sl->list[sl->num] == 0)
898 return ENOMEM;
899 strcpy(sl->list[sl->num], str);
900 sl->num++;
901 sl->list[sl->num] = 0;
902 return 0;
903}
904
905static void print_str_list(char **list)
906{
907 char **cpp;
908
909 for (cpp = list; *cpp; cpp++) {
910 printf("'%s'", *cpp);
911 if (cpp[1])
912 fputs(", ", stdout);
913 }
914 fputc('\n', stdout);
915}
916
917static char **parse_fs_type(const char *fs_type,
918 const char *usage_types,
919 struct ext2_super_block *fs_param,
920 char *progname)
921{
922 const char *ext_type = 0;
923 char *parse_str;
924 char *profile_type = 0;
925 char *cp, *t;
926 const char *size_type;
927 struct str_list list;
928 int state = 0;
929 unsigned long meg;
930
931 if (init_list(&list))
932 return 0;
933
934 if (fs_type)
935 ext_type = fs_type;
936 else if (progname) {
937 ext_type = strrchr(progname, '/');
938 if (ext_type)
939 ext_type++;
940 else
941 ext_type = progname;
942
943 if (!strncmp(ext_type, "mkfs.", 5)) {
944 ext_type += 5;
945 if (ext_type[0] == 0)
946 ext_type = 0;
947 } else
948 ext_type = 0;
949 }
950
951 if (!ext_type) {
952 profile_get_string(profile, "defaults", "fs_type", 0,
953 "ext2", &profile_type);
954 ext_type = profile_type;
955 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
956 ext_type = "ext3";
957 }
958
959 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
960 if (fs_param->s_blocks_count < 3 * meg)
961 size_type = "floppy";
962 else if (fs_param->s_blocks_count < 512 * meg)
963 size_type = "small";
964 else
965 size_type = "default";
966
967 if (!usage_types)
968 usage_types = size_type;
969
970 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
971 if (!parse_str) {
972 free(list.list);
973 return 0;
974 }
975 if (usage_types)
976 strcpy(parse_str, usage_types);
977 else
978 *parse_str = '\0';
979
980 if (ext_type)
981 push_string(&list, ext_type);
982 cp = parse_str;
983 while (1) {
984 t = strchr(cp, ',');
985 if (t)
986 *t = '\0';
987
988 if (*cp)
989 push_string(&list, cp);
990 if (t)
991 cp = t+1;
992 else {
993 cp = "";
994 break;
995 }
996 }
997 free(parse_str);
998 if (profile_type)
999 free(profile_type);
1000 return (list.list);
1001}
1002
1003static char *get_string_from_profile(char **fs_types, const char *opt,
1004 const char *def_val)
1005{
1006 char *ret = 0;
1007 char **cpp;
1008 int i;
1009
1010 for (i=0; fs_types[i]; i++);
1011 for (i-=1; i >=0 ; i--) {
1012 profile_get_string(profile, "fs_types", fs_types[i],
1013 opt, 0, &ret);
1014 if (ret)
1015 return ret;
1016 }
1017 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1018 return (ret);
1019}
1020
1021static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1022{
1023 int ret;
1024 char **cpp;
1025
1026 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1027 for (cpp = fs_types; *cpp; cpp++)
1028 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1029 return ret;
1030}
1031
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001032static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1033{
1034 int ret;
1035 char **cpp;
1036
1037 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1038 for (cpp = fs_types; *cpp; cpp++)
1039 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1040 return ret;
1041}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001042
Theodore Ts'od48bc602007-07-04 14:10:46 -04001043extern const char *mke2fs_default_profile;
1044static const char *default_files[] = { "<default>", 0 };
1045
Theodore Ts'o3839e651997-04-26 13:21:57 +00001046static void PRS(int argc, char *argv[])
1047{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001048 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001049 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001050 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001051 int blocksize = 0;
1052 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001053 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -05001054 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001055 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001056 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001057 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001058 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001059 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001060 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -05001061 const char * fs_type = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001062 const char * usage_types = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001063 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -05001064#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001065 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001066#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001067 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001068 int s_opt = -1, r_opt = -1;
1069 char *fs_features = 0;
1070 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001071 char *newpath;
1072 int pathlen = sizeof(PATH_SET) + 1;
1073
1074 if (oldpath)
1075 pathlen += strlen(oldpath);
1076 newpath = malloc(pathlen);
1077 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001078
Theodore Ts'o3839e651997-04-26 13:21:57 +00001079 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001080 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001081 strcat (newpath, ":");
1082 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001083 }
1084 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001085
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001086 tmp = getenv("MKE2FS_SYNC");
1087 if (tmp)
1088 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001089
1090 /* Determine the system page size if possible */
1091#ifdef HAVE_SYSCONF
1092#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1093#define _SC_PAGESIZE _SC_PAGE_SIZE
1094#endif
1095#ifdef _SC_PAGESIZE
1096 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001097 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001098 sys_page_size = sysval;
1099#endif /* _SC_PAGESIZE */
1100#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001101
1102 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1103 config_fn[0] = tmp;
1104 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001105 retval = profile_init(config_fn, &profile);
1106 if (retval == ENOENT) {
1107 profile_init(default_files, &profile);
1108 profile_set_default(profile, mke2fs_default_profile);
1109 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001110
Theodore Ts'o3839e651997-04-26 13:21:57 +00001111 setbuf(stdout, NULL);
1112 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001113 add_error_table(&et_ext2_error_table);
1114 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001115 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1116 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001117
Theodore Ts'o756df352002-03-07 20:52:12 -05001118#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001119 if (uname(&ut)) {
1120 perror("uname");
1121 exit(1);
1122 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001123 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001124 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001125 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001126#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001127
1128 if (argc && *argv) {
1129 program_name = get_progname(*argv);
1130
1131 /* If called as mkfs.ext3, create a journal inode */
1132 if (!strcmp(program_name, "mkfs.ext3"))
1133 journal_size = -1;
1134 }
1135
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001136 while ((c = getopt (argc, argv,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001137 "b:cf:g:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001138 switch (c) {
1139 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001140 blocksize = strtol(optarg, &tmp, 0);
1141 b = (blocksize > 0) ? blocksize : -blocksize;
1142 if (b < EXT2_MIN_BLOCK_SIZE ||
1143 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001144 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001145 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001146 exit(1);
1147 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001148 if (blocksize > 4096)
1149 fprintf(stderr, _("Warning: blocksize %d not "
1150 "usable on most systems.\n"),
1151 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001152 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001153 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001154 int_log2(blocksize >>
1155 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001156 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001157 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001158 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001159 break;
1160 case 'f':
1161 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001162 if (size < EXT2_MIN_BLOCK_SIZE ||
1163 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001164 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001165 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001166 optarg);
1167 exit(1);
1168 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001169 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001170 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001171 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001172 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001173 break;
1174 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001175 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001176 if (*tmp) {
1177 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001178 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001179 exit(1);
1180 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001181 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001182 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001183 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001184 exit(1);
1185 }
1186 break;
1187 case 'i':
1188 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001189 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1190 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001191 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001192 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001193 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001194 optarg, EXT2_MIN_BLOCK_SIZE,
1195 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001196 exit(1);
1197 }
1198 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001199 case 'J':
1200 parse_journal_opts(optarg);
1201 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001202 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001203 if (!journal_size)
1204 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001205 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001206 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001207 bad_blocks_filename = malloc(strlen(optarg)+1);
1208 if (!bad_blocks_filename) {
1209 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001210 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001211 exit(1);
1212 }
1213 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001214 break;
1215 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001216 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001217 if (reserved_ratio > 50 || *tmp) {
1218 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001219 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001220 optarg);
1221 exit(1);
1222 }
1223 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001224 case 'n':
1225 noaction++;
1226 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001227 case 'o':
1228 creator_os = optarg;
1229 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001230 case 'q':
1231 quiet = 1;
1232 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001233 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001234 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001235 if (*tmp) {
1236 com_err(program_name, 0,
1237 _("bad revision level - %s"), optarg);
1238 exit(1);
1239 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001240 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001241 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001242 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001243 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001244 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001245 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001246 inode_size = strtoul(optarg, &tmp, 0);
1247 if (*tmp) {
1248 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001249 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001250 exit(1);
1251 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001252 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001253 case 'v':
1254 verbose = 1;
1255 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001256 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001257 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001258 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001259 case 'L':
1260 volume_label = optarg;
1261 break;
1262 case 'M':
1263 mount_dir = optarg;
1264 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001265 case 'N':
1266 num_inodes = strtoul(optarg, &tmp, 0);
1267 if (*tmp) {
1268 com_err(program_name, 0,
1269 _("bad num inodes - %s"), optarg);
1270 exit(1);
1271 }
1272 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001273 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001274 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001275 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001276 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001277 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001278 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001279 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001280 case 'S':
1281 super_only = 1;
1282 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001283 case 't':
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001284 fs_type = optarg;
1285 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001286 case 'T':
1287 usage_types = optarg;
1288 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001289 case 'V':
1290 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001291 show_version_only++;
1292 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001293 default:
1294 usage();
1295 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001296 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001297 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001298 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001299 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001300
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001301 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001302 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1303 E2FSPROGS_DATE);
1304
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001305 if (show_version_only) {
1306 fprintf(stderr, _("\tUsing %s\n"),
1307 error_message(EXT2_ET_BASE));
1308 exit(0);
1309 }
1310
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001311 /*
1312 * If there's no blocksize specified and there is a journal
1313 * device, use it to figure out the blocksize
1314 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001315 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001316 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001317 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001318
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001319#ifdef CONFIG_TESTIO_DEBUG
1320 io_ptr = test_io_manager;
1321 test_io_backing_manager = unix_io_manager;
1322#else
1323 io_ptr = unix_io_manager;
1324#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001325 retval = ext2fs_open(journal_device,
1326 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001327 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001328 if (retval) {
1329 com_err(program_name, retval,
1330 _("while trying to open journal device %s\n"),
1331 journal_device);
1332 exit(1);
1333 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001334 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001335 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001336 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001337 "minimum blocksize %d\n"), jfs->blocksize,
1338 -blocksize);
1339 exit(1);
1340 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001341 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001342 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001343 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1344 ext2fs_close(jfs);
1345 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001346
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001347 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001348 if (!force) {
1349 com_err(program_name, 0,
1350 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001351 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001352 proceed_question();
1353 }
1354 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1355 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001356 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001357 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001358 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001359 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1360 fs_param.s_log_block_size);
1361 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001362 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001363 argv[optind - 1]);
1364 exit(1);
1365 }
1366 }
1367 if (optind < argc)
1368 usage();
1369
Theodore Ts'o74becf31997-04-26 14:37:06 +00001370 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001371 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001372 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001373
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001374 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001375
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001376 if (noaction && fs_param.s_blocks_count) {
1377 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001378 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001379 } else {
1380 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001381 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001382 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001383 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001384 if ((retval == EFBIG) &&
1385 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001386 (fs_param.s_log_block_size == 0)) {
1387 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001388 blocksize = 4096;
1389 goto retry;
1390 }
1391 }
1392
Theodore Ts'oa789d841998-03-30 01:20:55 +00001393 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1394 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001395 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001396 exit(1);
1397 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001398 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001399 if (retval == EXT2_ET_UNIMPLEMENTED) {
1400 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001401 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001402 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001403 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001404 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001405 } else {
1406 if (dev_size == 0) {
1407 com_err(program_name, 0,
1408 _("Device size reported to be zero. "
1409 "Invalid partition specified, or\n\t"
1410 "partition table wasn't reread "
1411 "after running fdisk, due to\n\t"
1412 "a modified partition being busy "
1413 "and in use. You may need to reboot\n\t"
1414 "to re-read your partition table.\n"
1415 ));
1416 exit(1);
1417 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001418 fs_param.s_blocks_count = dev_size;
1419 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1420 fs_param.s_blocks_count &= ~((sys_page_size /
1421 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001422 }
1423
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001424 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001425 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001426 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001427 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001428 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001429
Theodore Ts'o3d438362008-02-19 08:32:58 -05001430 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1431 if (!fs_types) {
1432 fprintf(stderr, _("Failed to parse fs types list\n"));
1433 exit(1);
1434 }
1435 if (verbose) {
1436 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1437 print_str_list(fs_types);
1438 }
1439
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001440 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001441 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001442 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1443
Brian Behlendorfe0661502007-03-19 08:25:38 -04001444 if (fs_param.s_feature_incompat &
1445 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1446 fs_type = "journal";
1447 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001448 fs_type = "floppy";
1449 else if (megs <= 512)
1450 fs_type = "small";
1451 else
1452 fs_type = "default";
1453 }
1454
1455 /* Figure out what features should be enabled */
1456
Theodore Ts'o3d438362008-02-19 08:32:58 -05001457 tmp = NULL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001458 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001459 char **cpp;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001460
Theodore Ts'o3d438362008-02-19 08:32:58 -05001461 tmp = get_string_from_profile(fs_types, "base_features",
1462 "sparse_super,filetype,resize_inode,dir_index");
1463 edit_feature(tmp, &fs_param.s_feature_compat);
1464 free(tmp);
1465
1466 for (cpp = fs_types; *cpp; cpp++) {
1467 tmp = NULL;
1468 profile_get_string(profile, "fs_types", *cpp,
1469 "features", "", &tmp);
1470 if (tmp && *tmp)
1471 edit_feature(tmp, &fs_param.s_feature_compat);
1472 if (tmp)
1473 free(tmp);
1474 }
1475 tmp = get_string_from_profile(fs_types, "default_features",
1476 "");
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001477 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001478 edit_feature(fs_features ? fs_features : tmp,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001479 &fs_param.s_feature_compat);
1480 if (tmp)
1481 free(tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001482
1483 if (r_opt == EXT2_GOOD_OLD_REV &&
1484 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1485 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001486 fprintf(stderr, _("Filesystem features not supported "
1487 "with revision 0 filesystems\n"));
1488 exit(1);
1489 }
1490
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001491 if (s_opt > 0) {
1492 if (r_opt == EXT2_GOOD_OLD_REV) {
1493 fprintf(stderr, _("Sparse superblocks not supported "
1494 "with revision 0 filesystems\n"));
1495 exit(1);
1496 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001497 fs_param.s_feature_ro_compat |=
1498 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001499 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001500 fs_param.s_feature_ro_compat &=
1501 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1502
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001503 if (journal_size != 0) {
1504 if (r_opt == EXT2_GOOD_OLD_REV) {
1505 fprintf(stderr, _("Journals not supported "
1506 "with revision 0 filesystems\n"));
1507 exit(1);
1508 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001509 fs_param.s_feature_compat |=
1510 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001511 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001512
1513 if (fs_param.s_feature_incompat &
1514 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001515 reserved_ratio = 0;
1516 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1517 fs_param.s_feature_compat = 0;
1518 fs_param.s_feature_ro_compat = 0;
1519 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001520
1521 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1522 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1523 fprintf(stderr, _("The resize_inode and meta_bg features "
1524 "are not compatible.\n"
1525 "They can not be both enabled "
1526 "simultaneously.\n"));
1527 exit(1);
1528 }
1529
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001530 /* Set first meta blockgroup via an environment variable */
1531 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001532 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001533 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001534 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001535
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001536 /* Get the hardware sector size, if available */
1537 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1538 if (retval) {
1539 com_err(program_name, retval,
1540 _("while trying to determine hardware sector size"));
1541 exit(1);
1542 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001543
Theodore Ts'o54434922003-12-07 01:28:50 -05001544 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001545 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001546
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001547 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001548 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001549
1550 if (use_bsize == -1) {
1551 use_bsize = sys_page_size;
1552 if ((linux_version_code < (2*65536 + 6*256)) &&
1553 (use_bsize > 4096))
1554 use_bsize = 4096;
1555 }
1556 if (sector_size && use_bsize < sector_size)
1557 use_bsize = sector_size;
1558 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1559 use_bsize = -blocksize;
1560 blocksize = use_bsize;
1561 fs_param.s_blocks_count /= blocksize / 1024;
1562 }
1563
1564 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001565 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1566 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001567 if (inode_ratio < blocksize)
1568 inode_ratio = blocksize;
1569 }
1570
1571 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1572 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1573
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001574 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001575
1576 lazy_itable_init = get_bool_from_profile(fs_types,
1577 "lazy_itable_init", 0);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001578
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001579 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001580 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001581
1582 /* Since sparse_super is the default, we would only have a problem
1583 * here if it was explicitly disabled.
1584 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001585 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1586 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001587 com_err(program_name, 0,
1588 _("reserved online resize blocks not supported "
1589 "on non-sparse filesystem"));
1590 exit(1);
1591 }
1592
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001593 if (fs_param.s_blocks_per_group) {
1594 if (fs_param.s_blocks_per_group < 256 ||
1595 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001596 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001597 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001598 exit(1);
1599 }
1600 }
1601
Theodore Ts'o3d438362008-02-19 08:32:58 -05001602 if (inode_size == 0)
1603 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Andreas Dilger067911a2006-07-15 22:08:20 -04001604
1605 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001606 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001607 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001608 inode_size & (inode_size - 1)) {
1609 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001610 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001611 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001612 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001613 exit(1);
1614 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001615 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001616 }
1617
Eric Sandeenf3358642006-09-12 14:56:17 -04001618 /* Make sure number of inodes specified will fit in 32 bits */
1619 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001620 unsigned long long n;
1621 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001622 if (n > ~0U) {
1623 com_err(program_name, 0,
1624 _("too many inodes (%llu), raise inode ratio?"), n);
1625 exit(1);
1626 }
1627 } else if (num_inodes > ~0U) {
1628 com_err(program_name, 0,
1629 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001630 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001631 exit(1);
1632 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001633 /*
1634 * Calculate number of inodes based on the inode ratio
1635 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001636 fs_param.s_inodes_count = num_inodes ? num_inodes :
1637 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001638 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001639
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001640 if ((((long long)fs_param.s_inodes_count) *
1641 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1642 (((long long)fs_param.s_blocks_count) *
1643 EXT2_BLOCK_SIZE(&fs_param))) {
1644 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1645 "(%u) too big for a\n\t"
1646 "filesystem with %lu blocks, "
1647 "specify higher inode_ratio (-i)\n\t"
1648 "or lower inode count (-N).\n"),
1649 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001650 fs_param.s_inodes_count,
1651 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001652 exit(1);
1653 }
1654
Theodore Ts'o3839e651997-04-26 13:21:57 +00001655 /*
1656 * Calculate number of blocks to reserve
1657 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001658 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1659 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001660}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001661
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301662static int should_do_undo(const char *name)
1663{
1664 errcode_t retval;
1665 io_channel channel;
1666 __u16 s_magic;
1667 struct ext2_super_block super;
1668 io_manager manager = unix_io_manager;
1669 int csum_flag, force_undo;
1670
1671 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1672 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1673 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1674 if (!force_undo && (!csum_flag || !lazy_itable_init))
1675 return 0;
1676
1677 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1678 if (retval) {
1679 /*
1680 * We don't handle error cases instead we
1681 * declare that the file system doesn't exist
1682 * and let the rest of mke2fs take care of
1683 * error
1684 */
1685 retval = 0;
1686 goto open_err_out;
1687 }
1688
1689 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1690 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1691 if (retval) {
1692 retval = 0;
1693 goto err_out;
1694 }
1695
1696#if defined(WORDS_BIGENDIAN)
1697 s_magic = ext2fs_swab16(super.s_magic);
1698#else
1699 s_magic = super.s_magic;
1700#endif
1701
1702 if (s_magic == EXT2_SUPER_MAGIC)
1703 retval = 1;
1704
1705err_out:
1706 io_channel_close(channel);
1707
1708open_err_out:
1709
1710 return retval;
1711}
1712
1713static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1714{
1715 errcode_t retval = 0;
1716 char *tdb_dir, tdb_file[PATH_MAX];
1717 char *device_name, *tmp_name;
1718
1719 /*
1720 * Configuration via a conf file would be
1721 * nice
1722 */
1723 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1724 if (!tdb_dir)
1725 profile_get_string(profile, "defaults",
1726 "undo_dir", 0, "/var/lib/e2fsprogs",
1727 &tdb_dir);
1728
1729 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1730 access(tdb_dir, W_OK))
1731 return 0;
1732
1733 tmp_name = strdup(name);
1734 device_name = basename(tmp_name);
1735 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1736
1737 if (!access(tdb_file, F_OK)) {
1738 if (unlink(tdb_file) < 0) {
1739 retval = errno;
1740 com_err(program_name, retval,
1741 _("while trying to delete %s"),
1742 tdb_file);
1743 return retval;
1744 }
1745 }
1746
1747 set_undo_io_backing_manager(*io_ptr);
1748 *io_ptr = undo_io_manager;
1749 set_undo_io_backup_file(tdb_file);
1750 printf(_("Overwriting existing filesystem; this can be undone "
1751 "using the command:\n"
1752 " e2undo %s %s\n\n"), tdb_file, name);
1753err_out:
1754 free(tmp_name);
1755 return retval;
1756}
1757
Theodore Ts'o3839e651997-04-26 13:21:57 +00001758int main (int argc, char *argv[])
1759{
1760 errcode_t retval = 0;
1761 ext2_filsys fs;
1762 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001763 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001764 unsigned int i;
1765 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001766 io_manager io_ptr;
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301767 char tdb_string[40];
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001768
1769#ifdef ENABLE_NLS
1770 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001771 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001772 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1773 textdomain(NLS_CAT_NAME);
1774#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001775 PRS(argc, argv);
1776
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001777#ifdef CONFIG_TESTIO_DEBUG
1778 io_ptr = test_io_manager;
1779 test_io_backing_manager = unix_io_manager;
1780#else
1781 io_ptr = unix_io_manager;
1782#endif
1783
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301784 if (should_do_undo(device_name)) {
1785 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1786 if (retval)
1787 exit(1);
1788 }
1789
Theodore Ts'o3839e651997-04-26 13:21:57 +00001790 /*
1791 * Initialize the superblock....
1792 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001793 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001794 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001795 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001796 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001797 exit(1);
1798 }
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301799 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1800 32768 : fs->blocksize * 8);
1801 io_channel_set_options(fs->io, tdb_string);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001802
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001803 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1804 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1805
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001806 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001807 * Wipe out the old on-disk superblock
1808 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001809 if (!noaction)
1810 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001811
1812 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001813 * Generate a UUID for it...
1814 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001815 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001816
1817 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001818 * Initialize the directory index variables
1819 */
1820 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1821 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1822
1823 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001824 * Add "jitter" to the superblock's check interval so that we
1825 * don't check all the filesystems at the same time. We use a
1826 * kludgy hack of using the UUID to derive a random jitter value.
1827 */
1828 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1829 val += fs->super->s_uuid[i];
1830 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1831
1832 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001833 * Override the creator OS, if applicable
1834 */
1835 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001836 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001837 exit(1);
1838 }
1839
1840 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001841 * For the Hurd, we will turn off filetype since it doesn't
1842 * support it.
1843 */
1844 if (fs->super->s_creator_os == EXT2_OS_HURD)
1845 fs->super->s_feature_incompat &=
1846 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1847
1848 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001849 * Set the volume label...
1850 */
1851 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001852 memset(fs->super->s_volume_name, 0,
1853 sizeof(fs->super->s_volume_name));
1854 strncpy(fs->super->s_volume_name, volume_label,
1855 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001856 }
1857
1858 /*
1859 * Set the last mount directory
1860 */
1861 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001862 memset(fs->super->s_last_mounted, 0,
1863 sizeof(fs->super->s_last_mounted));
1864 strncpy(fs->super->s_last_mounted, mount_dir,
1865 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001866 }
1867
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001868 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001869 show_stats(fs);
1870
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001871 if (noaction)
1872 exit(0);
1873
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001874 if (fs->super->s_feature_incompat &
1875 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1876 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001877 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001878 }
1879
Theodore Ts'o3839e651997-04-26 13:21:57 +00001880 if (bad_blocks_filename)
1881 read_bb_file(fs, &bb_list, bad_blocks_filename);
1882 if (cflag)
1883 test_disk(fs, &bb_list);
1884
1885 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001886 fs->stride = fs_stride = fs->super->s_raid_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001887 retval = ext2fs_allocate_tables(fs);
1888 if (retval) {
1889 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001890 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001891 exit(1);
1892 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001893 if (super_only) {
1894 fs->super->s_state |= EXT2_ERROR_FS;
1895 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1896 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001897 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001898 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001899 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001900 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001901 blk_t ret_blk;
1902
1903#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001904 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001905#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001906
1907 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001908 * Wipe out any old MD RAID (or other) metadata at the end
1909 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001910 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001911 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001912 start = (blocks & ~(rsv - 1));
1913 if (start > rsv)
1914 start -= rsv;
1915 if (start > 0)
Aneesh Kumar K.Vb626b392007-08-13 15:56:26 +05301916 retval = ext2fs_zero_blocks(fs, start, blocks - start,
1917 &ret_blk, NULL);
Andreas Dilger59f27242001-08-30 15:39:04 -06001918
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001919 if (retval) {
1920 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001921 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001922 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001923 }
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001924 write_inode_tables(fs, lazy_itable_init);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001925 create_root_dir(fs);
1926 create_lost_and_found(fs);
1927 reserve_inodes(fs);
1928 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001929 if (fs->super->s_feature_compat &
1930 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1931 retval = ext2fs_create_resize_inode(fs);
1932 if (retval) {
1933 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001934 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001935 exit(1);
1936 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001937 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001938 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001939
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001940 if (journal_device) {
1941 ext2_filsys jfs;
1942
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001943 if (!force)
1944 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001945 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001946
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001947 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1948 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1949 fs->blocksize, unix_io_manager, &jfs);
1950 if (retval) {
1951 com_err(program_name, retval,
1952 _("while trying to open journal device %s\n"),
1953 journal_device);
1954 exit(1);
1955 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001956 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001957 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001958 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001959 fflush(stdout);
1960 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001961 retval = ext2fs_add_journal_device(fs, jfs);
1962 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001963 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001964 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001965 journal_device);
1966 exit(1);
1967 }
1968 if (!quiet)
1969 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001970 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001971 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001972 } else if ((journal_size) ||
1973 (fs_param.s_feature_compat &
1974 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001975 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001976
1977 if (!journal_blocks) {
1978 fs->super->s_feature_compat &=
1979 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1980 goto no_journal;
1981 }
1982 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001983 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001984 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001985 fflush(stdout);
1986 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001987 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1988 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001989 if (retval) {
1990 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001991 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001992 exit(1);
1993 }
1994 if (!quiet)
1995 printf(_("done\n"));
1996 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001997no_journal:
1998
Theodore Ts'o3839e651997-04-26 13:21:57 +00001999 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002000 printf(_("Writing superblocks and "
2001 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002002 retval = ext2fs_flush(fs);
2003 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05002004 fprintf(stderr,
2005 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002006 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002007 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002008 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002009 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2010 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002011 }
Andreas Dilger568101f2001-10-13 01:22:25 -06002012 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002013 remove_error_table(&et_ext2_error_table);
2014 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002015 profile_release(profile);
Andreas Dilger568101f2001-10-13 01:22:25 -06002016 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002017}