blob: 3812f86875d3b4b086e837c6a4c2338d6c25aa2a [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>
47
Theodore Ts'o54c637d2001-05-14 11:45:38 +000048#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000049#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000050#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000051#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000053#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050054#include "profile.h"
Theodore Ts'oa6d83022006-12-26 03:38:07 -050055#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000057#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058
59#define STRIDE_LENGTH 8
60
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000061#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000062#define ZAP_BOOTBLOCK
63#endif
64
Theodore Ts'o3839e651997-04-26 13:21:57 +000065extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000066extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000069const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000070
71/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000072int cflag;
73int verbose;
74int quiet;
75int super_only;
76int force;
77int noaction;
78int journal_size;
79int journal_flags;
Theodore Ts'oa4396e92008-04-18 10:19:27 -040080int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000081char *bad_blocks_filename;
82__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050084struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000085char *creator_os;
86char *volume_label;
87char *mount_dir;
88char *journal_device;
89int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000090
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050091profile_t profile;
92
Theodore Ts'o31e29a12002-05-17 10:53:07 -040093int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040094int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040095
Theodore Ts'o63985322001-01-03 17:02:13 +000096static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000097{
Theodore Ts'obdd80f22008-02-28 21:12:31 -050098 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -040099 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500100 "[-J journal-options]\n"
Andreas Dilger067911a2006-07-15 22:08:20 -0400101 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
102 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
103 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500104 "[-r fs-revision] [-E extended-option[,...]]\n"
105 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106 program_name);
107 exit(1);
108}
109
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000110static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111{
112 int l = 0;
113
114 arg >>= 1;
115 while (arg) {
116 l++;
117 arg >>= 1;
118 }
119 return l;
120}
121
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000122static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000123{
124 int l;
125
126 for (l=0; arg ; l++)
127 arg = arg / 10;
128 return l;
129}
130
Theodore Ts'od99225e2004-09-25 07:40:12 -0400131static int parse_version_number(const char *s)
132{
133 int major, minor, rev;
134 char *endptr;
135 const char *cp = s;
136
137 if (!s)
138 return 0;
139 major = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
141 return 0;
142 cp = endptr + 1;
143 minor = strtol(cp, &endptr, 10);
144 if (cp == endptr || *endptr != '.')
145 return 0;
146 cp = endptr + 1;
147 rev = strtol(cp, &endptr, 10);
148 if (cp == endptr)
149 return 0;
150 return ((((major * 256) + minor) * 256) + rev);
151}
152
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000153/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 * Helper function for read_bb_file and test_disk
155 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500156static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157{
Theodore Ts'o66938372002-03-08 00:14:46 -0500158 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 return;
160}
161
162/*
163 * Reads the bad blocks list from a file
164 */
165static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
166 const char *bad_blocks_file)
167{
168 FILE *f;
169 errcode_t retval;
170
171 f = fopen(bad_blocks_file, "r");
172 if (!f) {
173 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000174 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 exit(1);
176 }
177 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
178 fclose (f);
179 if (retval) {
180 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000181 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 exit(1);
183 }
184}
185
186/*
187 * Runs the badblocks program to test the disk
188 */
189static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
190{
191 FILE *f;
192 errcode_t retval;
193 char buf[1024];
194
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400195 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500196 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500197 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000199 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 f = popen(buf, "r");
201 if (!f) {
202 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400203 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 exit(1);
205 }
206 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000207 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 if (retval) {
209 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000210 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 exit(1);
212 }
213}
214
215static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
216{
Theodore Ts'o54434922003-12-07 01:28:50 -0500217 dgrp_t i;
218 blk_t j;
219 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 blk_t blk;
221 badblocks_iterate bb_iter;
222 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000223 blk_t group_block;
224 int group;
225 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226
227 if (!bb_list)
228 return;
229
230 /*
231 * The primary superblock and group descriptors *must* be
232 * good; if not, abort.
233 */
234 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
235 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000236 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000237 fprintf(stderr, _("Block %d in primary "
238 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400239 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000240 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500242 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 exit(1);
244 }
245 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000246
247 /*
248 * See if any of the bad blocks are showing up in the backup
249 * superblocks and/or group descriptors. If so, issue a
250 * warning and adjust the block counts appropriately.
251 */
252 group_block = fs->super->s_first_data_block +
253 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000254
255 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000256 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000258 if (ext2fs_badblocks_list_test(bb_list,
259 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000260 if (!group_bad)
261 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500262_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000263" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000264 group_block);
265 group_bad++;
266 group = ext2fs_group_of_blk(fs, group_block+j);
267 fs->group_desc[group].bg_free_blocks_count++;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400268 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000269 fs->super->s_free_blocks_count++;
270 }
271 }
272 group_block += fs->super->s_blocks_per_group;
273 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274
275 /*
276 * Mark all the bad blocks as used...
277 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000278 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000280 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000281 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282 exit(1);
283 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000284 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000285 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000286 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287}
288
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000289/*
290 * These functions implement a generalized progress meter.
291 */
292struct progress_struct {
293 char format[20];
294 char backup[80];
295 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400296 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000297};
298
299static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500300 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000302 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000303
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000304 memset(progress, 0, sizeof(struct progress_struct));
305 if (quiet)
306 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000307
308 /*
309 * Figure out how many digits we need
310 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000311 i = int_log10(max);
312 sprintf(progress->format, "%%%dd/%%%dld", i, i);
313 memset(progress->backup, '\b', sizeof(progress->backup)-1);
314 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500315 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000316 progress->backup[(2*i)+1] = 0;
317 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000318
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400319 progress->skip_progress = 0;
320 if (getenv("MKE2FS_SKIP_PROGRESS"))
321 progress->skip_progress++;
322
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000323 fputs(label, stdout);
324 fflush(stdout);
325}
326
327static void progress_update(struct progress_struct *progress, __u32 val)
328{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400329 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000330 return;
331 printf(progress->format, val, progress->max);
332 fputs(progress->backup, stdout);
333}
334
335static void progress_close(struct progress_struct *progress)
336{
337 if (progress->format[0] == 0)
338 return;
339 fputs(_("done \n"), stdout);
340}
341
342
343/*
344 * Helper function which zeros out _num_ blocks starting at _blk_. In
345 * case of an error, the details of the error is returned via _ret_blk_
346 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
347 * success, and an error code on an error.
348 *
349 * As a special case, if the first argument is NULL, then it will
350 * attempt to free the static zeroizing buffer. (This is to keep
351 * programs that check for memory leaks happy.)
352 */
353static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
354 struct progress_struct *progress,
355 blk_t *ret_blk, int *ret_count)
356{
357 int j, count, next_update, next_update_incr;
358 static char *buf;
359 errcode_t retval;
360
361 /* If fs is null, clean up the static buffer and return */
362 if (!fs) {
363 if (buf) {
364 free(buf);
365 buf = 0;
366 }
367 return 0;
368 }
369 /* Allocate the zeroizing buffer if necessary */
370 if (!buf) {
371 buf = malloc(fs->blocksize * STRIDE_LENGTH);
372 if (!buf) {
373 com_err("malloc", ENOMEM,
374 _("while allocating zeroizing buffer"));
375 exit(1);
376 }
377 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
378 }
379 /* OK, do the write loop */
380 next_update = 0;
381 next_update_incr = num / 100;
382 if (next_update_incr < 1)
383 next_update_incr = 1;
384 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400385 count = num - j;
386 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000387 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000388 retval = io_channel_write_blk(fs->io, blk, count, buf);
389 if (retval) {
390 if (ret_count)
391 *ret_count = count;
392 if (ret_blk)
393 *ret_blk = blk;
394 return retval;
395 }
396 if (progress && j > next_update) {
397 next_update += num / 100;
398 progress_update(progress, blk);
399 }
400 }
401 return 0;
402}
403
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400404static void write_inode_tables(ext2_filsys fs, int lazy_flag)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000405{
406 errcode_t retval;
407 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500408 dgrp_t i;
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400409 int num, ipb;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000410 struct progress_struct progress;
411
412 if (quiet)
413 memset(&progress, 0, sizeof(progress));
414 else
415 progress_init(&progress, _("Writing inode tables: "),
416 fs->group_desc_count);
417
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000419 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000421 blk = fs->group_desc[i].bg_inode_table;
422 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000423
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400424 if (lazy_flag) {
425 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400426 num = ((((fs->super->s_inodes_per_group -
427 fs->group_desc[i].bg_itable_unused) *
428 EXT2_INODE_SIZE(fs->super)) +
429 EXT2_BLOCK_SIZE(fs->super) - 1) /
430 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400431 } else {
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500432 /* The kernel doesn't need to zero the itable blocks */
433 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400434 ext2fs_group_desc_csum_set(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000435 }
Theodore Ts'o0f2794c2008-04-22 23:18:37 -0400436 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
437 if (retval) {
438 fprintf(stderr, _("\nCould not write %d "
439 "blocks in inode table starting at %u: %s\n"),
440 num, blk, error_message(retval));
441 exit(1);
442 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000443 if (sync_kludge) {
444 if (sync_kludge == 1)
445 sync();
446 else if ((i % sync_kludge) == 0)
447 sync();
448 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000450 zero_blocks(0, 0, 0, 0, 0, 0);
451 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452}
453
454static void create_root_dir(ext2_filsys fs)
455{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400456 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000457 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400458 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459
460 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
461 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000462 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 exit(1);
464 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000465 if (geteuid()) {
466 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
467 if (retval) {
468 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000469 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000470 exit(1);
471 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400472 uid = getuid();
473 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500474 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400475 if (uid) {
476 gid = getgid();
477 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500478 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400479 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500480 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000481 if (retval) {
482 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000483 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000484 exit(1);
485 }
486 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487}
488
489static void create_lost_and_found(ext2_filsys fs)
490{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400491 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000493 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 const char *name = "lost+found";
495 int i;
496
Theodore Ts'o6a525062001-12-24 09:40:00 -0500497 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
499 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000500 com_err("ext2fs_mkdir", retval,
501 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 exit(1);
503 }
504
505 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
506 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000507 com_err("ext2_lookup", retval,
508 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 exit(1);
510 }
511
512 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100513 /* Ensure that lost+found is at least 2 blocks, so we always
514 * test large empty blocks for big-block filesystems. */
515 if ((lpf_size += fs->blocksize) >= 16*1024 &&
516 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000517 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 retval = ext2fs_expand_dir(fs, ino);
519 if (retval) {
520 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000521 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 exit(1);
523 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500524 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525}
526
527static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
528{
529 errcode_t retval;
530
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400532 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 retval = ext2fs_update_bb_inode(fs, bb_list);
534 if (retval) {
535 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000536 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 exit(1);
538 }
539
540}
541
542static void reserve_inodes(ext2_filsys fs)
543{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000544 ext2_ino_t i;
545 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546
Theodore Ts'o5711ed22008-04-21 01:29:01 -0400547 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
548 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 ext2fs_mark_ib_dirty(fs);
550}
551
Theodore Ts'o756df352002-03-07 20:52:12 -0500552#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500553#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500554#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500555
Theodore Ts'o04a96852001-08-30 21:55:26 -0400556static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000557{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400558 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000559 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500560 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000561
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400562 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600563 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500564 printf(_("Out of memory erasing sectors %d-%d\n"),
565 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600566 exit(1);
567 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500568
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500569 if (sect == 0) {
570 /* Check for a BSD disklabel, and don't erase it if so */
571 retval = io_channel_read_blk(fs->io, 0, -512, buf);
572 if (retval)
573 fprintf(stderr,
574 _("Warning: could not read block 0: %s\n"),
575 error_message(retval));
576 else {
577 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
578 if ((*magic == BSD_DISKMAGIC) ||
579 (*magic == BSD_MAGICDISK))
580 return;
581 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500582 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500583
Theodore Ts'o70988102002-07-14 08:00:00 -0400584 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000585 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400586 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000587 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400588 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000589 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500590 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
591 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000592}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000593
594static void create_journal_dev(ext2_filsys fs)
595{
596 struct progress_struct progress;
597 errcode_t retval;
598 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000599 blk_t blk;
600 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000601
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000602 retval = ext2fs_create_journal_superblock(fs,
603 fs->super->s_blocks_count, 0, &buf);
604 if (retval) {
605 com_err("create_journal_dev", retval,
606 _("while initializing journal superblock"));
607 exit(1);
608 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000609 if (quiet)
610 memset(&progress, 0, sizeof(progress));
611 else
612 progress_init(&progress, _("Zeroing journal device: "),
613 fs->super->s_blocks_count);
614
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000615 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
616 &progress, &blk, &count);
617 if (retval) {
618 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000619 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000620 blk, count);
621 exit(1);
622 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000623 zero_blocks(0, 0, 0, 0, 0, 0);
624
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000625 retval = io_channel_write_blk(fs->io,
626 fs->super->s_first_data_block+1,
627 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000628 if (retval) {
629 com_err("create_journal_dev", retval,
630 _("while writing journal superblock"));
631 exit(1);
632 }
633 progress_close(&progress);
634}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000635
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636static void show_stats(ext2_filsys fs)
637{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000638 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000639 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500640 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500642 dgrp_t i;
643 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500645 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500646 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500647 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000648
649 memset(buf, 0, sizeof(buf));
650 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000651 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500652 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500653 os = e2p_os2string(fs->super->s_creator_os);
654 fputs(os, stdout);
655 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000656 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000657 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000658 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000659 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000660 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000661 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000663 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664 s->s_r_blocks_count,
665 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000666 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500667 if (s->s_reserved_gdt_blocks)
668 printf(_("Maximum filesystem blocks=%lu\n"),
669 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200670 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000671 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000672 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000673 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000674 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000675 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678
679 if (fs->group_desc_count == 1) {
680 printf("\n");
681 return;
682 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500683
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000684 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685 group_block = s->s_first_data_block;
686 col_left = 0;
687 for (i = 1; i < fs->group_desc_count; i++) {
688 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000689 if (!ext2fs_bg_has_super(fs, i))
690 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000691 if (i != 1)
692 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000693 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000694 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000696 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000698 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000699 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 }
701 printf("\n\n");
702}
703
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000704/*
705 * Set the S_CREATOR_OS field. Return true if OS is known,
706 * otherwise, 0.
707 */
708static int set_os(struct ext2_super_block *sb, char *os)
709{
710 if (isdigit (*os))
711 sb->s_creator_os = atoi (os);
712 else if (strcasecmp(os, "linux") == 0)
713 sb->s_creator_os = EXT2_OS_LINUX;
714 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
715 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500716 else if (strcasecmp(os, "freebsd") == 0)
717 sb->s_creator_os = EXT2_OS_FREEBSD;
718 else if (strcasecmp(os, "lites") == 0)
719 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000720 else
721 return 0;
722 return 1;
723}
724
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000725#define PATH_SET "PATH=/sbin"
726
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500727static void parse_extended_opts(struct ext2_super_block *param,
728 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000729{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400730 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000731 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500732 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000733
734 len = strlen(opts);
735 buf = malloc(len+1);
736 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500737 fprintf(stderr,
738 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000739 exit(1);
740 }
741 strcpy(buf, opts);
742 for (token = buf; token && *token; token = next) {
743 p = strchr(token, ',');
744 next = 0;
745 if (p) {
746 *p = 0;
747 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500748 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000749 arg = strchr(token, '=');
750 if (arg) {
751 *arg = 0;
752 arg++;
753 }
754 if (strcmp(token, "stride") == 0) {
755 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500756 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500757 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000758 continue;
759 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500760 param->s_raid_stride = strtoul(arg, &p, 0);
761 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000762 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400763 _("Invalid stride parameter: %s\n"),
764 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500765 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000766 continue;
767 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500768 } else if (strcmp(token, "stripe-width") == 0 ||
769 strcmp(token, "stripe_width") == 0) {
770 if (!arg) {
771 r_usage++;
772 badopt = token;
773 continue;
774 }
775 param->s_raid_stripe_width = strtoul(arg, &p, 0);
776 if (*p || (param->s_raid_stripe_width == 0)) {
777 fprintf(stderr,
778 _("Invalid stripe-width parameter: %s\n"),
779 arg);
780 r_usage++;
781 continue;
782 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500783 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500784 unsigned long resize, bpg, rsv_groups;
785 unsigned long group_desc_count, desc_blocks;
786 unsigned int gdpb, blocksize;
787 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500788
789 if (!arg) {
790 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500791 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500792 continue;
793 }
794
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500795 resize = parse_num_blocks(arg,
796 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500797
798 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500799 fprintf(stderr,
800 _("Invalid resize parameter: %s\n"),
801 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500802 r_usage++;
803 continue;
804 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500805 if (resize <= param->s_blocks_count) {
806 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400807 _("The resize maximum must be greater "
808 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500809 r_usage++;
810 continue;
811 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500812
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500813 blocksize = EXT2_BLOCK_SIZE(param);
814 bpg = param->s_blocks_per_group;
815 if (!bpg)
816 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200817 gdpb = EXT2_DESC_PER_BLOCK(param);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400818 group_desc_count =
819 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500820 desc_blocks = (group_desc_count +
821 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400822 rsv_groups = ext2fs_div_ceil(resize, bpg);
823 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500824 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500825 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500826 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
827
828 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400829 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
830 fprintf(stderr,
831 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400832 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400833 exit(1);
834 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500835 param->s_feature_compat |=
836 EXT2_FEATURE_COMPAT_RESIZE_INODE;
837
838 param->s_reserved_gdt_blocks = rsv_gdb;
839 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500840 } else if (!strcmp(token, "test_fs")) {
841 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400842 } else if (!strcmp(token, "lazy_itable_init")) {
Theodore Ts'o43781b92008-04-27 19:38:02 -0400843 if (arg)
844 lazy_itable_init = strtoul(arg, &p, 0);
845 else
846 lazy_itable_init = 1;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500847 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500848 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500849 badopt = token;
850 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000851 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500852 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500853 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400854 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000855 "and may take an argument which\n"
856 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400857 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500858 "\tstride=<RAID per-disk data chunk in blocks>\n"
859 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400860 "\tresize=<resize maximum size in blocks>\n"
861 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
862 "\ttest_fs\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400863 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400864 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000865 exit(1);
866 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500867 if (param->s_raid_stride &&
868 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
869 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
870 "multiple of stride %u.\n\n"),
871 param->s_raid_stripe_width, param->s_raid_stride);
872
Brian Behlendorf21400382007-05-31 11:30:47 -0400873 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000874}
875
Theodore Ts'o896938d1999-10-23 01:04:50 +0000876static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500877 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400878 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500879 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400880 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o558df542008-02-27 15:01:19 -0500881 EXT2_FEATURE_COMPAT_EXT_ATTR,
882 /* Incompat */
883 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500884 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400885 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500886 EXT2_FEATURE_INCOMPAT_META_BG|
887 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500888 /* R/O compat */
889 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500890 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
891 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000892};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000893
894
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500895static void syntax_err_report(const char *filename, long err, int line_num)
896{
897 fprintf(stderr,
898 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
899 filename, line_num, error_message(err));
900 exit(1);
901}
902
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200903static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500904
905static void edit_feature(const char *str, __u32 *compat_array)
906{
907 if (!str)
908 return;
909
910 if (e2p_edit_feature(str, compat_array, ok_features)) {
911 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
912 str);
913 exit(1);
914 }
915}
916
Theodore Ts'o3d438362008-02-19 08:32:58 -0500917struct str_list {
918 char **list;
919 int num;
920 int max;
921};
922
923static errcode_t init_list(struct str_list *sl)
924{
925 sl->num = 0;
926 sl->max = 0;
927 sl->list = malloc((sl->max+1) * sizeof(char *));
928 if (!sl->list)
929 return ENOMEM;
930 sl->list[0] = 0;
931 return 0;
932}
933
934static errcode_t push_string(struct str_list *sl, const char *str)
935{
936 char **new_list;
937
938 if (sl->num >= sl->max) {
939 sl->max += 2;
940 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
941 if (!new_list)
942 return ENOMEM;
943 sl->list = new_list;
944 }
945 sl->list[sl->num] = malloc(strlen(str)+1);
946 if (sl->list[sl->num] == 0)
947 return ENOMEM;
948 strcpy(sl->list[sl->num], str);
949 sl->num++;
950 sl->list[sl->num] = 0;
951 return 0;
952}
953
954static void print_str_list(char **list)
955{
956 char **cpp;
957
958 for (cpp = list; *cpp; cpp++) {
959 printf("'%s'", *cpp);
960 if (cpp[1])
961 fputs(", ", stdout);
962 }
963 fputc('\n', stdout);
964}
965
966static char **parse_fs_type(const char *fs_type,
967 const char *usage_types,
968 struct ext2_super_block *fs_param,
969 char *progname)
970{
971 const char *ext_type = 0;
972 char *parse_str;
973 char *profile_type = 0;
974 char *cp, *t;
975 const char *size_type;
976 struct str_list list;
977 int state = 0;
978 unsigned long meg;
979
980 if (init_list(&list))
981 return 0;
982
983 if (fs_type)
984 ext_type = fs_type;
985 else if (progname) {
986 ext_type = strrchr(progname, '/');
987 if (ext_type)
988 ext_type++;
989 else
990 ext_type = progname;
991
992 if (!strncmp(ext_type, "mkfs.", 5)) {
993 ext_type += 5;
994 if (ext_type[0] == 0)
995 ext_type = 0;
996 } else
997 ext_type = 0;
998 }
999
1000 if (!ext_type) {
1001 profile_get_string(profile, "defaults", "fs_type", 0,
1002 "ext2", &profile_type);
1003 ext_type = profile_type;
1004 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1005 ext_type = "ext3";
1006 }
1007
1008 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1009 if (fs_param->s_blocks_count < 3 * meg)
1010 size_type = "floppy";
1011 else if (fs_param->s_blocks_count < 512 * meg)
1012 size_type = "small";
1013 else
1014 size_type = "default";
1015
1016 if (!usage_types)
1017 usage_types = size_type;
1018
1019 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1020 if (!parse_str) {
1021 free(list.list);
1022 return 0;
1023 }
1024 if (usage_types)
1025 strcpy(parse_str, usage_types);
1026 else
1027 *parse_str = '\0';
1028
1029 if (ext_type)
1030 push_string(&list, ext_type);
1031 cp = parse_str;
1032 while (1) {
1033 t = strchr(cp, ',');
1034 if (t)
1035 *t = '\0';
1036
1037 if (*cp)
1038 push_string(&list, cp);
1039 if (t)
1040 cp = t+1;
1041 else {
1042 cp = "";
1043 break;
1044 }
1045 }
1046 free(parse_str);
1047 if (profile_type)
1048 free(profile_type);
1049 return (list.list);
1050}
1051
1052static char *get_string_from_profile(char **fs_types, const char *opt,
1053 const char *def_val)
1054{
1055 char *ret = 0;
1056 char **cpp;
1057 int i;
1058
1059 for (i=0; fs_types[i]; i++);
1060 for (i-=1; i >=0 ; i--) {
1061 profile_get_string(profile, "fs_types", fs_types[i],
1062 opt, 0, &ret);
1063 if (ret)
1064 return ret;
1065 }
1066 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1067 return (ret);
1068}
1069
1070static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1071{
1072 int ret;
1073 char **cpp;
1074
1075 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1076 for (cpp = fs_types; *cpp; cpp++)
1077 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1078 return ret;
1079}
1080
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001081static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1082{
1083 int ret;
1084 char **cpp;
1085
1086 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1087 for (cpp = fs_types; *cpp; cpp++)
1088 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1089 return ret;
1090}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001091
Theodore Ts'od48bc602007-07-04 14:10:46 -04001092extern const char *mke2fs_default_profile;
1093static const char *default_files[] = { "<default>", 0 };
1094
Theodore Ts'o3839e651997-04-26 13:21:57 +00001095static void PRS(int argc, char *argv[])
1096{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001097 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001098 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001099 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001100 int blocksize = 0;
1101 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001102 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -05001103 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001104 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001105 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001106 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001107 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001108 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001109 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -05001110 const char * fs_type = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001111 const char * usage_types = 0;
1112 char **fs_types;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001113 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -05001114#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001115 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001116#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001117 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001118 int s_opt = -1, r_opt = -1;
1119 char *fs_features = 0;
1120 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001121 char *newpath;
1122 int pathlen = sizeof(PATH_SET) + 1;
1123
1124 if (oldpath)
1125 pathlen += strlen(oldpath);
1126 newpath = malloc(pathlen);
1127 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001128
Theodore Ts'o3839e651997-04-26 13:21:57 +00001129 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001130 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001131 strcat (newpath, ":");
1132 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001133 }
1134 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001135
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001136 tmp = getenv("MKE2FS_SYNC");
1137 if (tmp)
1138 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001139
1140 /* Determine the system page size if possible */
1141#ifdef HAVE_SYSCONF
1142#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1143#define _SC_PAGESIZE _SC_PAGE_SIZE
1144#endif
1145#ifdef _SC_PAGESIZE
1146 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001147 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001148 sys_page_size = sysval;
1149#endif /* _SC_PAGESIZE */
1150#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001151
1152 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1153 config_fn[0] = tmp;
1154 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001155 retval = profile_init(config_fn, &profile);
1156 if (retval == ENOENT) {
1157 profile_init(default_files, &profile);
1158 profile_set_default(profile, mke2fs_default_profile);
1159 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001160
Theodore Ts'o3839e651997-04-26 13:21:57 +00001161 setbuf(stdout, NULL);
1162 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001163 add_error_table(&et_ext2_error_table);
1164 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001165 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1166 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001167
Theodore Ts'o756df352002-03-07 20:52:12 -05001168#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001169 if (uname(&ut)) {
1170 perror("uname");
1171 exit(1);
1172 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001173 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001174 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001175 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001176#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001177
1178 if (argc && *argv) {
1179 program_name = get_progname(*argv);
1180
1181 /* If called as mkfs.ext3, create a journal inode */
1182 if (!strcmp(program_name, "mkfs.ext3"))
1183 journal_size = -1;
1184 }
1185
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001186 while ((c = getopt (argc, argv,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001187 "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 +00001188 switch (c) {
1189 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001190 blocksize = strtol(optarg, &tmp, 0);
1191 b = (blocksize > 0) ? blocksize : -blocksize;
1192 if (b < EXT2_MIN_BLOCK_SIZE ||
1193 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001194 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001195 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001196 exit(1);
1197 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001198 if (blocksize > 4096)
1199 fprintf(stderr, _("Warning: blocksize %d not "
1200 "usable on most systems.\n"),
1201 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001202 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001203 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001204 int_log2(blocksize >>
1205 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001206 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001207 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001208 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001209 break;
1210 case 'f':
1211 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001212 if (size < EXT2_MIN_BLOCK_SIZE ||
1213 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001214 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001215 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001216 optarg);
1217 exit(1);
1218 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001219 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001220 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001221 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001222 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001223 break;
1224 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001225 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001226 if (*tmp) {
1227 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001228 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001229 exit(1);
1230 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001231 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001232 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001233 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001234 exit(1);
1235 }
1236 break;
1237 case 'i':
1238 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001239 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1240 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001241 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001242 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001243 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001244 optarg, EXT2_MIN_BLOCK_SIZE,
1245 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001246 exit(1);
1247 }
1248 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001249 case 'J':
1250 parse_journal_opts(optarg);
1251 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001252 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001253 if (!journal_size)
1254 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001255 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001256 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001257 bad_blocks_filename = malloc(strlen(optarg)+1);
1258 if (!bad_blocks_filename) {
1259 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001260 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001261 exit(1);
1262 }
1263 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001264 break;
1265 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001266 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001267 if (reserved_ratio > 50 || *tmp) {
1268 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001269 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001270 optarg);
1271 exit(1);
1272 }
1273 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001274 case 'n':
1275 noaction++;
1276 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001277 case 'o':
1278 creator_os = optarg;
1279 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001280 case 'q':
1281 quiet = 1;
1282 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001283 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001284 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001285 if (*tmp) {
1286 com_err(program_name, 0,
1287 _("bad revision level - %s"), optarg);
1288 exit(1);
1289 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001290 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001291 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001292 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001293 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001294 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001295 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001296 inode_size = strtoul(optarg, &tmp, 0);
1297 if (*tmp) {
1298 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001299 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001300 exit(1);
1301 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001302 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001303 case 'v':
1304 verbose = 1;
1305 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001306 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001307 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001308 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001309 case 'L':
1310 volume_label = optarg;
1311 break;
1312 case 'M':
1313 mount_dir = optarg;
1314 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001315 case 'N':
1316 num_inodes = strtoul(optarg, &tmp, 0);
1317 if (*tmp) {
1318 com_err(program_name, 0,
1319 _("bad num inodes - %s"), optarg);
1320 exit(1);
1321 }
1322 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001323 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001324 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001325 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001326 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001327 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001328 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001329 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001330 case 'S':
1331 super_only = 1;
1332 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001333 case 't':
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001334 fs_type = optarg;
1335 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001336 case 'T':
1337 usage_types = optarg;
1338 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001339 case 'V':
1340 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001341 show_version_only++;
1342 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001343 default:
1344 usage();
1345 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001346 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001347 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001348 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001349 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001350
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001351 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001352 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1353 E2FSPROGS_DATE);
1354
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001355 if (show_version_only) {
1356 fprintf(stderr, _("\tUsing %s\n"),
1357 error_message(EXT2_ET_BASE));
1358 exit(0);
1359 }
1360
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001361 /*
1362 * If there's no blocksize specified and there is a journal
1363 * device, use it to figure out the blocksize
1364 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001365 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001366 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001367 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001368
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001369#ifdef CONFIG_TESTIO_DEBUG
1370 io_ptr = test_io_manager;
1371 test_io_backing_manager = unix_io_manager;
1372#else
1373 io_ptr = unix_io_manager;
1374#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001375 retval = ext2fs_open(journal_device,
1376 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001377 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001378 if (retval) {
1379 com_err(program_name, retval,
1380 _("while trying to open journal device %s\n"),
1381 journal_device);
1382 exit(1);
1383 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001384 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001385 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001386 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001387 "minimum blocksize %d\n"), jfs->blocksize,
1388 -blocksize);
1389 exit(1);
1390 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001391 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001392 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001393 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1394 ext2fs_close(jfs);
1395 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001396
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001397 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001398 if (!force) {
1399 com_err(program_name, 0,
1400 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001401 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001402 proceed_question();
1403 }
1404 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1405 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001406 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001407 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001408 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001409 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1410 fs_param.s_log_block_size);
1411 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001412 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001413 argv[optind - 1]);
1414 exit(1);
1415 }
1416 }
1417 if (optind < argc)
1418 usage();
1419
Theodore Ts'o74becf31997-04-26 14:37:06 +00001420 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001421 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001422 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001423
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001424 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001425
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001426 if (noaction && fs_param.s_blocks_count) {
1427 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001428 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001429 } else {
1430 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001431 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001432 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001433 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001434 if ((retval == EFBIG) &&
1435 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001436 (fs_param.s_log_block_size == 0)) {
1437 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001438 blocksize = 4096;
1439 goto retry;
1440 }
1441 }
1442
Theodore Ts'oa789d841998-03-30 01:20:55 +00001443 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1444 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001445 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001446 exit(1);
1447 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001448 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001449 if (retval == EXT2_ET_UNIMPLEMENTED) {
1450 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001451 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001452 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001453 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001454 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001455 } else {
1456 if (dev_size == 0) {
1457 com_err(program_name, 0,
1458 _("Device size reported to be zero. "
1459 "Invalid partition specified, or\n\t"
1460 "partition table wasn't reread "
1461 "after running fdisk, due to\n\t"
1462 "a modified partition being busy "
1463 "and in use. You may need to reboot\n\t"
1464 "to re-read your partition table.\n"
1465 ));
1466 exit(1);
1467 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001468 fs_param.s_blocks_count = dev_size;
1469 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1470 fs_param.s_blocks_count &= ~((sys_page_size /
1471 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001472 }
1473
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001474 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001475 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001476 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001477 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001478 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001479
Theodore Ts'o3d438362008-02-19 08:32:58 -05001480 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1481 if (!fs_types) {
1482 fprintf(stderr, _("Failed to parse fs types list\n"));
1483 exit(1);
1484 }
1485 if (verbose) {
1486 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1487 print_str_list(fs_types);
1488 }
1489
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001490 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001491 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001492 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1493
Brian Behlendorfe0661502007-03-19 08:25:38 -04001494 if (fs_param.s_feature_incompat &
1495 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1496 fs_type = "journal";
1497 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001498 fs_type = "floppy";
1499 else if (megs <= 512)
1500 fs_type = "small";
1501 else
1502 fs_type = "default";
1503 }
1504
1505 /* Figure out what features should be enabled */
1506
Theodore Ts'o3d438362008-02-19 08:32:58 -05001507 tmp = NULL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001508 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001509 char **cpp;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001510
Theodore Ts'o3d438362008-02-19 08:32:58 -05001511 tmp = get_string_from_profile(fs_types, "base_features",
1512 "sparse_super,filetype,resize_inode,dir_index");
1513 edit_feature(tmp, &fs_param.s_feature_compat);
1514 free(tmp);
1515
1516 for (cpp = fs_types; *cpp; cpp++) {
1517 tmp = NULL;
1518 profile_get_string(profile, "fs_types", *cpp,
1519 "features", "", &tmp);
1520 if (tmp && *tmp)
1521 edit_feature(tmp, &fs_param.s_feature_compat);
1522 if (tmp)
1523 free(tmp);
1524 }
1525 tmp = get_string_from_profile(fs_types, "default_features",
1526 "");
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001527 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001528 edit_feature(fs_features ? fs_features : tmp,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001529 &fs_param.s_feature_compat);
1530 if (tmp)
1531 free(tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001532
1533 if (r_opt == EXT2_GOOD_OLD_REV &&
1534 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1535 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001536 fprintf(stderr, _("Filesystem features not supported "
1537 "with revision 0 filesystems\n"));
1538 exit(1);
1539 }
1540
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001541 if (s_opt > 0) {
1542 if (r_opt == EXT2_GOOD_OLD_REV) {
1543 fprintf(stderr, _("Sparse superblocks not supported "
1544 "with revision 0 filesystems\n"));
1545 exit(1);
1546 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001547 fs_param.s_feature_ro_compat |=
1548 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001549 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001550 fs_param.s_feature_ro_compat &=
1551 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1552
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001553 if (journal_size != 0) {
1554 if (r_opt == EXT2_GOOD_OLD_REV) {
1555 fprintf(stderr, _("Journals not supported "
1556 "with revision 0 filesystems\n"));
1557 exit(1);
1558 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001559 fs_param.s_feature_compat |=
1560 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001561 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001562
1563 if (fs_param.s_feature_incompat &
1564 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001565 reserved_ratio = 0;
1566 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1567 fs_param.s_feature_compat = 0;
1568 fs_param.s_feature_ro_compat = 0;
1569 }
Theodore Ts'od94cc2e2008-04-27 00:08:14 -04001570
1571 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1572 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1573 fprintf(stderr, _("The resize_inode and meta_bg features "
1574 "are not compatible.\n"
1575 "They can not be both enabled "
1576 "simultaneously.\n"));
1577 exit(1);
1578 }
1579
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001580 /* Set first meta blockgroup via an environment variable */
1581 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001582 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001583 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001584 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001585
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001586 /* Get the hardware sector size, if available */
1587 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1588 if (retval) {
1589 com_err(program_name, retval,
1590 _("while trying to determine hardware sector size"));
1591 exit(1);
1592 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001593
Theodore Ts'o54434922003-12-07 01:28:50 -05001594 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001595 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001596
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001597 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001598 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001599
1600 if (use_bsize == -1) {
1601 use_bsize = sys_page_size;
1602 if ((linux_version_code < (2*65536 + 6*256)) &&
1603 (use_bsize > 4096))
1604 use_bsize = 4096;
1605 }
1606 if (sector_size && use_bsize < sector_size)
1607 use_bsize = sector_size;
1608 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1609 use_bsize = -blocksize;
1610 blocksize = use_bsize;
1611 fs_param.s_blocks_count /= blocksize / 1024;
1612 }
1613
1614 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001615 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1616 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001617 if (inode_ratio < blocksize)
1618 inode_ratio = blocksize;
1619 }
1620
1621 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1622 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1623
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001624 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001625
1626 lazy_itable_init = get_bool_from_profile(fs_types,
1627 "lazy_itable_init", 0);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001628
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001629 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001630 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001631
1632 /* Since sparse_super is the default, we would only have a problem
1633 * here if it was explicitly disabled.
1634 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001635 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1636 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001637 com_err(program_name, 0,
1638 _("reserved online resize blocks not supported "
1639 "on non-sparse filesystem"));
1640 exit(1);
1641 }
1642
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001643 if (fs_param.s_blocks_per_group) {
1644 if (fs_param.s_blocks_per_group < 256 ||
1645 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001646 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001647 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001648 exit(1);
1649 }
1650 }
1651
Theodore Ts'o3d438362008-02-19 08:32:58 -05001652 if (inode_size == 0)
1653 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Andreas Dilger067911a2006-07-15 22:08:20 -04001654
1655 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001656 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001657 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001658 inode_size & (inode_size - 1)) {
1659 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001660 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001661 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001662 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001663 exit(1);
1664 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001665 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001666 }
1667
Eric Sandeenf3358642006-09-12 14:56:17 -04001668 /* Make sure number of inodes specified will fit in 32 bits */
1669 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001670 unsigned long long n;
1671 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001672 if (n > ~0U) {
1673 com_err(program_name, 0,
1674 _("too many inodes (%llu), raise inode ratio?"), n);
1675 exit(1);
1676 }
1677 } else if (num_inodes > ~0U) {
1678 com_err(program_name, 0,
1679 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001680 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001681 exit(1);
1682 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001683 /*
1684 * Calculate number of inodes based on the inode ratio
1685 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001686 fs_param.s_inodes_count = num_inodes ? num_inodes :
1687 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001688 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001689
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001690 if ((((long long)fs_param.s_inodes_count) *
1691 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1692 (((long long)fs_param.s_blocks_count) *
1693 EXT2_BLOCK_SIZE(&fs_param))) {
1694 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1695 "(%u) too big for a\n\t"
1696 "filesystem with %lu blocks, "
1697 "specify higher inode_ratio (-i)\n\t"
1698 "or lower inode count (-N).\n"),
1699 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001700 fs_param.s_inodes_count,
1701 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001702 exit(1);
1703 }
1704
Theodore Ts'o3839e651997-04-26 13:21:57 +00001705 /*
1706 * Calculate number of blocks to reserve
1707 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001708 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1709 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001710}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001711
Theodore Ts'o3839e651997-04-26 13:21:57 +00001712int main (int argc, char *argv[])
1713{
1714 errcode_t retval = 0;
1715 ext2_filsys fs;
1716 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001717 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001718 unsigned int i;
1719 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001720 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001721
1722#ifdef ENABLE_NLS
1723 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001724 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001725 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1726 textdomain(NLS_CAT_NAME);
1727#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001728 PRS(argc, argv);
1729
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001730#ifdef CONFIG_TESTIO_DEBUG
1731 io_ptr = test_io_manager;
1732 test_io_backing_manager = unix_io_manager;
1733#else
1734 io_ptr = unix_io_manager;
1735#endif
1736
Theodore Ts'o3839e651997-04-26 13:21:57 +00001737 /*
1738 * Initialize the superblock....
1739 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001740 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001741 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001742 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001743 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001744 exit(1);
1745 }
1746
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001747 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1748 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1749
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001750 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001751 * Wipe out the old on-disk superblock
1752 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001753 if (!noaction)
1754 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001755
1756 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001757 * Generate a UUID for it...
1758 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001759 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001760
1761 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001762 * Initialize the directory index variables
1763 */
1764 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1765 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1766
1767 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001768 * Add "jitter" to the superblock's check interval so that we
1769 * don't check all the filesystems at the same time. We use a
1770 * kludgy hack of using the UUID to derive a random jitter value.
1771 */
1772 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1773 val += fs->super->s_uuid[i];
1774 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1775
1776 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001777 * Override the creator OS, if applicable
1778 */
1779 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001780 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001781 exit(1);
1782 }
1783
1784 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001785 * For the Hurd, we will turn off filetype since it doesn't
1786 * support it.
1787 */
1788 if (fs->super->s_creator_os == EXT2_OS_HURD)
1789 fs->super->s_feature_incompat &=
1790 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1791
1792 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001793 * Set the volume label...
1794 */
1795 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001796 memset(fs->super->s_volume_name, 0,
1797 sizeof(fs->super->s_volume_name));
1798 strncpy(fs->super->s_volume_name, volume_label,
1799 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001800 }
1801
1802 /*
1803 * Set the last mount directory
1804 */
1805 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001806 memset(fs->super->s_last_mounted, 0,
1807 sizeof(fs->super->s_last_mounted));
1808 strncpy(fs->super->s_last_mounted, mount_dir,
1809 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001810 }
1811
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001812 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001813 show_stats(fs);
1814
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001815 if (noaction)
1816 exit(0);
1817
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001818 if (fs->super->s_feature_incompat &
1819 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1820 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001821 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001822 }
1823
Theodore Ts'o3839e651997-04-26 13:21:57 +00001824 if (bad_blocks_filename)
1825 read_bb_file(fs, &bb_list, bad_blocks_filename);
1826 if (cflag)
1827 test_disk(fs, &bb_list);
1828
1829 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001830 fs->stride = fs_stride = fs->super->s_raid_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001831 retval = ext2fs_allocate_tables(fs);
1832 if (retval) {
1833 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001834 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001835 exit(1);
1836 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001837 if (super_only) {
1838 fs->super->s_state |= EXT2_ERROR_FS;
1839 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1840 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001841 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001842 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001843 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001844 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001845 blk_t ret_blk;
1846
1847#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001848 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001849#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001850
1851 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001852 * Wipe out any old MD RAID (or other) metadata at the end
1853 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001854 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001855 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001856 start = (blocks & ~(rsv - 1));
1857 if (start > rsv)
1858 start -= rsv;
1859 if (start > 0)
1860 retval = zero_blocks(fs, start, blocks - start,
1861 NULL, &ret_blk, NULL);
1862
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001863 if (retval) {
1864 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001865 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001866 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001867 }
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001868 write_inode_tables(fs, lazy_itable_init);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001869 create_root_dir(fs);
1870 create_lost_and_found(fs);
1871 reserve_inodes(fs);
1872 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001873 if (fs->super->s_feature_compat &
1874 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1875 retval = ext2fs_create_resize_inode(fs);
1876 if (retval) {
1877 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001878 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001879 exit(1);
1880 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001881 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001882 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001883
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001884 if (journal_device) {
1885 ext2_filsys jfs;
1886
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001887 if (!force)
1888 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001889 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001890
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001891 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1892 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1893 fs->blocksize, unix_io_manager, &jfs);
1894 if (retval) {
1895 com_err(program_name, retval,
1896 _("while trying to open journal device %s\n"),
1897 journal_device);
1898 exit(1);
1899 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001900 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001901 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001902 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001903 fflush(stdout);
1904 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001905 retval = ext2fs_add_journal_device(fs, jfs);
1906 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001907 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001908 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001909 journal_device);
1910 exit(1);
1911 }
1912 if (!quiet)
1913 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001914 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001915 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001916 } else if ((journal_size) ||
1917 (fs_param.s_feature_compat &
1918 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001919 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001920
1921 if (!journal_blocks) {
1922 fs->super->s_feature_compat &=
1923 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1924 goto no_journal;
1925 }
1926 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001927 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001928 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001929 fflush(stdout);
1930 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001931 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1932 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001933 if (retval) {
1934 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001935 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001936 exit(1);
1937 }
1938 if (!quiet)
1939 printf(_("done\n"));
1940 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001941no_journal:
1942
Theodore Ts'o3839e651997-04-26 13:21:57 +00001943 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001944 printf(_("Writing superblocks and "
1945 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001946 retval = ext2fs_flush(fs);
1947 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001948 fprintf(stderr,
1949 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001950 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001951 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001952 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001953 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1954 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001955 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001956 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001957 remove_error_table(&et_ext2_error_table);
1958 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05001959 profile_release(profile);
Andreas Dilger568101f2001-10-13 01:22:25 -06001960 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001961}