blob: b0cbc42d4c5c53c6e4ffaa50d5313d328d43085b [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;
80char *bad_blocks_filename;
81__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050083struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000084char *creator_os;
85char *volume_label;
86char *mount_dir;
87char *journal_device;
88int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000089
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050090profile_t profile;
91
Theodore Ts'o31e29a12002-05-17 10:53:07 -040092int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040093int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040094
Theodore Ts'o63985322001-01-03 17:02:13 +000095static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000096{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000097 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -040098 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
99 "[-j] [-J journal-options]\n"
100 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
101 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
102 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'of3059182007-10-15 16:20:54 -0400103 "[-r fs-revision] [-E extended-option[,...]] [-qvSV]\n\tdevice [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104 program_name);
105 exit(1);
106}
107
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000108static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109{
110 int l = 0;
111
112 arg >>= 1;
113 while (arg) {
114 l++;
115 arg >>= 1;
116 }
117 return l;
118}
119
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000120static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000121{
122 int l;
123
124 for (l=0; arg ; l++)
125 arg = arg / 10;
126 return l;
127}
128
Theodore Ts'od99225e2004-09-25 07:40:12 -0400129static int parse_version_number(const char *s)
130{
131 int major, minor, rev;
132 char *endptr;
133 const char *cp = s;
134
135 if (!s)
136 return 0;
137 major = strtol(cp, &endptr, 10);
138 if (cp == endptr || *endptr != '.')
139 return 0;
140 cp = endptr + 1;
141 minor = strtol(cp, &endptr, 10);
142 if (cp == endptr || *endptr != '.')
143 return 0;
144 cp = endptr + 1;
145 rev = strtol(cp, &endptr, 10);
146 if (cp == endptr)
147 return 0;
148 return ((((major * 256) + minor) * 256) + rev);
149}
150
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000151/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 * Helper function for read_bb_file and test_disk
153 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500154static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155{
Theodore Ts'o66938372002-03-08 00:14:46 -0500156 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 return;
158}
159
160/*
161 * Reads the bad blocks list from a file
162 */
163static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
164 const char *bad_blocks_file)
165{
166 FILE *f;
167 errcode_t retval;
168
169 f = fopen(bad_blocks_file, "r");
170 if (!f) {
171 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000172 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 exit(1);
174 }
175 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
176 fclose (f);
177 if (retval) {
178 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000179 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 exit(1);
181 }
182}
183
184/*
185 * Runs the badblocks program to test the disk
186 */
187static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
188{
189 FILE *f;
190 errcode_t retval;
191 char buf[1024];
192
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400193 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500194 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500195 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000197 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 f = popen(buf, "r");
199 if (!f) {
200 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400201 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 exit(1);
203 }
204 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000205 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 if (retval) {
207 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000208 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 exit(1);
210 }
211}
212
213static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
214{
Theodore Ts'o54434922003-12-07 01:28:50 -0500215 dgrp_t i;
216 blk_t j;
217 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 blk_t blk;
219 badblocks_iterate bb_iter;
220 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000221 blk_t group_block;
222 int group;
223 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224
225 if (!bb_list)
226 return;
227
228 /*
229 * The primary superblock and group descriptors *must* be
230 * good; if not, abort.
231 */
232 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
233 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000234 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000235 fprintf(stderr, _("Block %d in primary "
236 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400237 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000238 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500240 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 exit(1);
242 }
243 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000244
245 /*
246 * See if any of the bad blocks are showing up in the backup
247 * superblocks and/or group descriptors. If so, issue a
248 * warning and adjust the block counts appropriately.
249 */
250 group_block = fs->super->s_first_data_block +
251 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000252
253 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000254 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000256 if (ext2fs_badblocks_list_test(bb_list,
257 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258 if (!group_bad)
259 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500260_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000261" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000262 group_block);
263 group_bad++;
264 group = ext2fs_group_of_blk(fs, group_block+j);
265 fs->group_desc[group].bg_free_blocks_count++;
266 fs->super->s_free_blocks_count++;
267 }
268 }
269 group_block += fs->super->s_blocks_per_group;
270 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000271
272 /*
273 * Mark all the bad blocks as used...
274 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000275 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000277 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000278 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 exit(1);
280 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000283 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284}
285
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000286/*
287 * These functions implement a generalized progress meter.
288 */
289struct progress_struct {
290 char format[20];
291 char backup[80];
292 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400293 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000294};
295
296static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500297 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000299 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000300
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000301 memset(progress, 0, sizeof(struct progress_struct));
302 if (quiet)
303 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000304
305 /*
306 * Figure out how many digits we need
307 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000308 i = int_log10(max);
309 sprintf(progress->format, "%%%dd/%%%dld", i, i);
310 memset(progress->backup, '\b', sizeof(progress->backup)-1);
311 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500312 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000313 progress->backup[(2*i)+1] = 0;
314 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000315
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400316 progress->skip_progress = 0;
317 if (getenv("MKE2FS_SKIP_PROGRESS"))
318 progress->skip_progress++;
319
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000320 fputs(label, stdout);
321 fflush(stdout);
322}
323
324static void progress_update(struct progress_struct *progress, __u32 val)
325{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400326 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000327 return;
328 printf(progress->format, val, progress->max);
329 fputs(progress->backup, stdout);
330}
331
332static void progress_close(struct progress_struct *progress)
333{
334 if (progress->format[0] == 0)
335 return;
336 fputs(_("done \n"), stdout);
337}
338
339
340/*
341 * Helper function which zeros out _num_ blocks starting at _blk_. In
342 * case of an error, the details of the error is returned via _ret_blk_
343 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
344 * success, and an error code on an error.
345 *
346 * As a special case, if the first argument is NULL, then it will
347 * attempt to free the static zeroizing buffer. (This is to keep
348 * programs that check for memory leaks happy.)
349 */
350static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
351 struct progress_struct *progress,
352 blk_t *ret_blk, int *ret_count)
353{
354 int j, count, next_update, next_update_incr;
355 static char *buf;
356 errcode_t retval;
357
358 /* If fs is null, clean up the static buffer and return */
359 if (!fs) {
360 if (buf) {
361 free(buf);
362 buf = 0;
363 }
364 return 0;
365 }
366 /* Allocate the zeroizing buffer if necessary */
367 if (!buf) {
368 buf = malloc(fs->blocksize * STRIDE_LENGTH);
369 if (!buf) {
370 com_err("malloc", ENOMEM,
371 _("while allocating zeroizing buffer"));
372 exit(1);
373 }
374 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
375 }
376 /* OK, do the write loop */
377 next_update = 0;
378 next_update_incr = num / 100;
379 if (next_update_incr < 1)
380 next_update_incr = 1;
381 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400382 count = num - j;
383 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000384 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000385 retval = io_channel_write_blk(fs->io, blk, count, buf);
386 if (retval) {
387 if (ret_count)
388 *ret_count = count;
389 if (ret_blk)
390 *ret_blk = blk;
391 return retval;
392 }
393 if (progress && j > next_update) {
394 next_update += num / 100;
395 progress_update(progress, blk);
396 }
397 }
398 return 0;
399}
400
401static void write_inode_tables(ext2_filsys fs)
402{
403 errcode_t retval;
404 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500405 dgrp_t i;
406 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407 struct progress_struct progress;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400408 int lazy_flag = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000409
410 if (quiet)
411 memset(&progress, 0, sizeof(progress));
412 else
413 progress_init(&progress, _("Writing inode tables: "),
414 fs->group_desc_count);
415
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400416 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
417 EXT2_FEATURE_COMPAT_LAZY_BG))
418 lazy_flag = 1;
419
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000421 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000423 blk = fs->group_desc[i].bg_inode_table;
424 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000425
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400426 if (!(lazy_flag &&
427 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
428 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
429 if (retval) {
430 fprintf(stderr, _("\nCould not write %d "
431 "blocks in inode table starting at %u: %s\n"),
432 num, blk, error_message(retval));
433 exit(1);
434 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000435 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000436 if (sync_kludge) {
437 if (sync_kludge == 1)
438 sync();
439 else if ((i % sync_kludge) == 0)
440 sync();
441 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000443 zero_blocks(0, 0, 0, 0, 0, 0);
444 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445}
446
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400447static void setup_lazy_bg(ext2_filsys fs)
448{
449 dgrp_t i;
450 int blks;
451 struct ext2_super_block *sb = fs->super;
452 struct ext2_group_desc *bg = fs->group_desc;
453
454 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
455 EXT2_FEATURE_COMPAT_LAZY_BG)) {
456 for (i = 0; i < fs->group_desc_count; i++, bg++) {
457 if ((i == 0) ||
458 (i == fs->group_desc_count-1))
459 continue;
460 if (bg->bg_free_inodes_count ==
461 sb->s_inodes_per_group) {
462 bg->bg_free_inodes_count = 0;
463 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
464 sb->s_free_inodes_count -=
465 sb->s_inodes_per_group;
466 }
467 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
468 if (bg->bg_free_blocks_count == blks) {
469 bg->bg_free_blocks_count = 0;
470 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
471 sb->s_free_blocks_count -= blks;
472 }
473 }
474 }
475}
476
477
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478static void create_root_dir(ext2_filsys fs)
479{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400480 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000481 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400482 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483
484 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
485 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000486 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 exit(1);
488 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000489 if (geteuid()) {
490 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
491 if (retval) {
492 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000493 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000494 exit(1);
495 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400496 uid = getuid();
497 inode.i_uid = uid;
498 inode.i_uid_high = uid >> 16;
499 if (uid) {
500 gid = getgid();
501 inode.i_gid = gid;
502 inode.i_gid_high = gid >> 16;
503 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500504 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000505 if (retval) {
506 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000507 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000508 exit(1);
509 }
510 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511}
512
513static void create_lost_and_found(ext2_filsys fs)
514{
515 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000516 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 const char *name = "lost+found";
518 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000519 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520
Theodore Ts'o6a525062001-12-24 09:40:00 -0500521 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
523 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000524 com_err("ext2fs_mkdir", retval,
525 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 exit(1);
527 }
528
529 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
530 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000531 com_err("ext2_lookup", retval,
532 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 exit(1);
534 }
535
536 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100537 /* Ensure that lost+found is at least 2 blocks, so we always
538 * test large empty blocks for big-block filesystems. */
539 if ((lpf_size += fs->blocksize) >= 16*1024 &&
540 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000541 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 retval = ext2fs_expand_dir(fs, ino);
543 if (retval) {
544 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000545 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 exit(1);
547 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500548 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549}
550
551static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
552{
553 errcode_t retval;
554
Theodore Ts'of3db3561997-04-26 13:34:30 +0000555 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 fs->group_desc[0].bg_free_inodes_count--;
557 fs->super->s_free_inodes_count--;
558 retval = ext2fs_update_bb_inode(fs, bb_list);
559 if (retval) {
560 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000561 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562 exit(1);
563 }
564
565}
566
567static void reserve_inodes(ext2_filsys fs)
568{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000569 ext2_ino_t i;
570 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000572 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000573 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 group = ext2fs_group_of_ino(fs, i);
575 fs->group_desc[group].bg_free_inodes_count--;
576 fs->super->s_free_inodes_count--;
577 }
578 ext2fs_mark_ib_dirty(fs);
579}
580
Theodore Ts'o756df352002-03-07 20:52:12 -0500581#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500582#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500583#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500584
Theodore Ts'o04a96852001-08-30 21:55:26 -0400585static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000586{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400587 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000588 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500589 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000590
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400591 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600592 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500593 printf(_("Out of memory erasing sectors %d-%d\n"),
594 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600595 exit(1);
596 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500597
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500598 if (sect == 0) {
599 /* Check for a BSD disklabel, and don't erase it if so */
600 retval = io_channel_read_blk(fs->io, 0, -512, buf);
601 if (retval)
602 fprintf(stderr,
603 _("Warning: could not read block 0: %s\n"),
604 error_message(retval));
605 else {
606 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
607 if ((*magic == BSD_DISKMAGIC) ||
608 (*magic == BSD_MAGICDISK))
609 return;
610 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500611 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500612
Theodore Ts'o70988102002-07-14 08:00:00 -0400613 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000614 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400615 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000616 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400617 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000618 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500619 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
620 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000621}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000622
623static void create_journal_dev(ext2_filsys fs)
624{
625 struct progress_struct progress;
626 errcode_t retval;
627 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000628 blk_t blk;
629 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000630
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000631 retval = ext2fs_create_journal_superblock(fs,
632 fs->super->s_blocks_count, 0, &buf);
633 if (retval) {
634 com_err("create_journal_dev", retval,
635 _("while initializing journal superblock"));
636 exit(1);
637 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000638 if (quiet)
639 memset(&progress, 0, sizeof(progress));
640 else
641 progress_init(&progress, _("Zeroing journal device: "),
642 fs->super->s_blocks_count);
643
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000644 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
645 &progress, &blk, &count);
646 if (retval) {
647 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000648 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000649 blk, count);
650 exit(1);
651 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000652 zero_blocks(0, 0, 0, 0, 0, 0);
653
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000654 retval = io_channel_write_blk(fs->io,
655 fs->super->s_first_data_block+1,
656 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000657 if (retval) {
658 com_err("create_journal_dev", retval,
659 _("while writing journal superblock"));
660 exit(1);
661 }
662 progress_close(&progress);
663}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000664
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665static void show_stats(ext2_filsys fs)
666{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000667 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000668 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500669 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500671 dgrp_t i;
672 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500674 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500675 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500676 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000677
678 memset(buf, 0, sizeof(buf));
679 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000680 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500681 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500682 os = e2p_os2string(fs->super->s_creator_os);
683 fputs(os, stdout);
684 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000685 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000686 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000687 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000688 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000689 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000690 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000692 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693 s->s_r_blocks_count,
694 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000695 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500696 if (s->s_reserved_gdt_blocks)
697 printf(_("Maximum filesystem blocks=%lu\n"),
698 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200699 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000700 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000701 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000702 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000703 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000704 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000706 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707
708 if (fs->group_desc_count == 1) {
709 printf("\n");
710 return;
711 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500712
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000713 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 group_block = s->s_first_data_block;
715 col_left = 0;
716 for (i = 1; i < fs->group_desc_count; i++) {
717 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000718 if (!ext2fs_bg_has_super(fs, i))
719 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000720 if (i != 1)
721 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000722 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000723 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000724 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000725 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000727 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000728 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729 }
730 printf("\n\n");
731}
732
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000733/*
734 * Set the S_CREATOR_OS field. Return true if OS is known,
735 * otherwise, 0.
736 */
737static int set_os(struct ext2_super_block *sb, char *os)
738{
739 if (isdigit (*os))
740 sb->s_creator_os = atoi (os);
741 else if (strcasecmp(os, "linux") == 0)
742 sb->s_creator_os = EXT2_OS_LINUX;
743 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
744 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500745 else if (strcasecmp(os, "freebsd") == 0)
746 sb->s_creator_os = EXT2_OS_FREEBSD;
747 else if (strcasecmp(os, "lites") == 0)
748 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000749 else
750 return 0;
751 return 1;
752}
753
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000754#define PATH_SET "PATH=/sbin"
755
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500756static void parse_extended_opts(struct ext2_super_block *param,
757 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000758{
759 char *buf, *token, *next, *p, *arg;
760 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500761 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000762
763 len = strlen(opts);
764 buf = malloc(len+1);
765 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500766 fprintf(stderr,
767 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000768 exit(1);
769 }
770 strcpy(buf, opts);
771 for (token = buf; token && *token; token = next) {
772 p = strchr(token, ',');
773 next = 0;
774 if (p) {
775 *p = 0;
776 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500777 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000778 arg = strchr(token, '=');
779 if (arg) {
780 *arg = 0;
781 arg++;
782 }
783 if (strcmp(token, "stride") == 0) {
784 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500785 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000786 continue;
787 }
788 fs_stride = strtoul(arg, &p, 0);
789 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000790 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400791 _("Invalid stride parameter: %s\n"),
792 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500793 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000794 continue;
795 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500796 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500797 unsigned long resize, bpg, rsv_groups;
798 unsigned long group_desc_count, desc_blocks;
799 unsigned int gdpb, blocksize;
800 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500801
802 if (!arg) {
803 r_usage++;
804 continue;
805 }
806
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500807 resize = parse_num_blocks(arg,
808 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500809
810 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500811 fprintf(stderr,
812 _("Invalid resize parameter: %s\n"),
813 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500814 r_usage++;
815 continue;
816 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500817 if (resize <= param->s_blocks_count) {
818 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400819 _("The resize maximum must be greater "
820 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500821 r_usage++;
822 continue;
823 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500824
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500825 blocksize = EXT2_BLOCK_SIZE(param);
826 bpg = param->s_blocks_per_group;
827 if (!bpg)
828 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200829 gdpb = EXT2_DESC_PER_BLOCK(param);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400830 group_desc_count =
831 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500832 desc_blocks = (group_desc_count +
833 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400834 rsv_groups = ext2fs_div_ceil(resize, bpg);
835 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500836 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500837 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500838 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
839
840 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400841 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
842 fprintf(stderr,
843 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400844 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400845 exit(1);
846 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500847 param->s_feature_compat |=
848 EXT2_FEATURE_COMPAT_RESIZE_INODE;
849
850 param->s_reserved_gdt_blocks = rsv_gdb;
851 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000852 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500853 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000854 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500855 if (r_usage) {
856 fprintf(stderr, _("\nBad options specified.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400857 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000858 "and may take an argument which\n"
859 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400860 "Valid extended options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500861 "\tstride=<stride length in blocks>\n"
862 "\tresize=<resize maximum size in blocks>\n\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400863 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000864 exit(1);
865 }
Brian Behlendorf21400382007-05-31 11:30:47 -0400866 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000867}
868
Theodore Ts'o896938d1999-10-23 01:04:50 +0000869static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400870 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500871 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400872 EXT2_FEATURE_COMPAT_DIR_INDEX |
873 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000874 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400875 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500876 EXT2_FEATURE_INCOMPAT_META_BG|
877 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000878 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
879};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000880
881
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500882static void syntax_err_report(const char *filename, long err, int line_num)
883{
884 fprintf(stderr,
885 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
886 filename, line_num, error_message(err));
887 exit(1);
888}
889
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200890static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500891
892static void edit_feature(const char *str, __u32 *compat_array)
893{
894 if (!str)
895 return;
896
897 if (e2p_edit_feature(str, compat_array, ok_features)) {
898 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
899 str);
900 exit(1);
901 }
902}
903
Theodore Ts'od48bc602007-07-04 14:10:46 -0400904extern const char *mke2fs_default_profile;
905static const char *default_files[] = { "<default>", 0 };
906
Theodore Ts'o3839e651997-04-26 13:21:57 +0000907static void PRS(int argc, char *argv[])
908{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400909 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000910 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500911 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000912 int blocksize = 0;
913 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600914 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500915 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400916 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500917 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400918 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000919 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000920 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500921 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500922 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000923 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500924#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000925 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000926#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400927 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500928 int s_opt = -1, r_opt = -1;
929 char *fs_features = 0;
930 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500931 char *newpath;
932 int pathlen = sizeof(PATH_SET) + 1;
933
934 if (oldpath)
935 pathlen += strlen(oldpath);
936 newpath = malloc(pathlen);
937 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000938
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000940 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000941 strcat (newpath, ":");
942 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500943 }
944 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000945
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000946 tmp = getenv("MKE2FS_SYNC");
947 if (tmp)
948 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400949
950 /* Determine the system page size if possible */
951#ifdef HAVE_SYSCONF
952#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
953#define _SC_PAGESIZE _SC_PAGE_SIZE
954#endif
955#ifdef _SC_PAGESIZE
956 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600957 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400958 sys_page_size = sysval;
959#endif /* _SC_PAGESIZE */
960#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500961
962 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
963 config_fn[0] = tmp;
964 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -0400965 retval = profile_init(config_fn, &profile);
966 if (retval == ENOENT) {
967 profile_init(default_files, &profile);
968 profile_set_default(profile, mke2fs_default_profile);
969 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000970
Theodore Ts'o3839e651997-04-26 13:21:57 +0000971 setbuf(stdout, NULL);
972 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500973 add_error_table(&et_ext2_error_table);
974 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500975 memset(&fs_param, 0, sizeof(struct ext2_super_block));
976 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400977
Theodore Ts'o756df352002-03-07 20:52:12 -0500978#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000979 if (uname(&ut)) {
980 perror("uname");
981 exit(1);
982 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400983 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500984 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500985 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000986#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700987
988 if (argc && *argv) {
989 program_name = get_progname(*argv);
990
991 /* If called as mkfs.ext3, create a journal inode */
992 if (!strcmp(program_name, "mkfs.ext3"))
993 journal_size = -1;
994 }
995
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000996 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500997 "b:cf:g:i:jl:m:no:qr:s:tvE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000998 switch (c) {
999 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001000 blocksize = strtol(optarg, &tmp, 0);
1001 b = (blocksize > 0) ? blocksize : -blocksize;
1002 if (b < EXT2_MIN_BLOCK_SIZE ||
1003 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001004 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001005 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 exit(1);
1007 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001008 if (blocksize > 4096)
1009 fprintf(stderr, _("Warning: blocksize %d not "
1010 "usable on most systems.\n"),
1011 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001012 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001013 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001014 int_log2(blocksize >>
1015 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001016 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001017 case 'c': /* Check for bad blocks */
1018 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001019 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001020 break;
1021 case 'f':
1022 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001023 if (size < EXT2_MIN_BLOCK_SIZE ||
1024 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001025 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001026 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001027 optarg);
1028 exit(1);
1029 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001030 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001031 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001032 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001033 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001034 break;
1035 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001036 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001037 if (*tmp) {
1038 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001039 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001040 exit(1);
1041 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001042 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001043 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001044 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001045 exit(1);
1046 }
1047 break;
1048 case 'i':
1049 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001050 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1051 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001053 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001054 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001055 optarg, EXT2_MIN_BLOCK_SIZE,
1056 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001057 exit(1);
1058 }
1059 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001060 case 'J':
1061 parse_journal_opts(optarg);
1062 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001063 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001064 if (!journal_size)
1065 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001066 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001067 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001068 bad_blocks_filename = malloc(strlen(optarg)+1);
1069 if (!bad_blocks_filename) {
1070 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001071 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001072 exit(1);
1073 }
1074 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001075 break;
1076 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001077 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001078 if (reserved_ratio > 50 || *tmp) {
1079 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001080 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001081 optarg);
1082 exit(1);
1083 }
1084 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001085 case 'n':
1086 noaction++;
1087 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001088 case 'o':
1089 creator_os = optarg;
1090 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001091 case 'q':
1092 quiet = 1;
1093 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001094 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001095 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001096 if (*tmp) {
1097 com_err(program_name, 0,
1098 _("bad revision level - %s"), optarg);
1099 exit(1);
1100 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001101 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001102 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001103 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001104 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001105 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001106 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001107 inode_size = strtoul(optarg, &tmp, 0);
1108 if (*tmp) {
1109 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001110 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001111 exit(1);
1112 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001113 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001114 case 'v':
1115 verbose = 1;
1116 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001117 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001118 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001119 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001120 case 'L':
1121 volume_label = optarg;
1122 break;
1123 case 'M':
1124 mount_dir = optarg;
1125 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001126 case 'N':
1127 num_inodes = strtoul(optarg, &tmp, 0);
1128 if (*tmp) {
1129 com_err(program_name, 0,
1130 _("bad num inodes - %s"), optarg);
1131 exit(1);
1132 }
1133 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001134 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001135 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001136 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001137 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001138 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001139 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001140 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001141 case 'S':
1142 super_only = 1;
1143 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001144 case 'T':
1145 fs_type = optarg;
1146 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001147 case 'V':
1148 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001149 show_version_only++;
1150 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001151 default:
1152 usage();
1153 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001154 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001155 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001156 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001157 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001158
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001159 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001160 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1161 E2FSPROGS_DATE);
1162
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001163 if (show_version_only) {
1164 fprintf(stderr, _("\tUsing %s\n"),
1165 error_message(EXT2_ET_BASE));
1166 exit(0);
1167 }
1168
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001169 /*
1170 * If there's no blocksize specified and there is a journal
1171 * device, use it to figure out the blocksize
1172 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001173 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001174 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001175 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001176
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001177#ifdef CONFIG_TESTIO_DEBUG
1178 io_ptr = test_io_manager;
1179 test_io_backing_manager = unix_io_manager;
1180#else
1181 io_ptr = unix_io_manager;
1182#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001183 retval = ext2fs_open(journal_device,
1184 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001185 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001186 if (retval) {
1187 com_err(program_name, retval,
1188 _("while trying to open journal device %s\n"),
1189 journal_device);
1190 exit(1);
1191 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001192 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001193 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001194 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001195 "minimum blocksize %d\n"), jfs->blocksize,
1196 -blocksize);
1197 exit(1);
1198 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001199 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001200 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001201 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1202 ext2fs_close(jfs);
1203 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001204
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001205 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001206 if (!force) {
1207 com_err(program_name, 0,
1208 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001209 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001210 proceed_question();
1211 }
1212 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1213 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001214 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001215 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001216 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001217 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1218 fs_param.s_log_block_size);
1219 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001220 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001221 argv[optind - 1]);
1222 exit(1);
1223 }
1224 }
1225 if (optind < argc)
1226 usage();
1227
Theodore Ts'o74becf31997-04-26 14:37:06 +00001228 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001229 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001230 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001231
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001232 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001233
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001234 if (noaction && fs_param.s_blocks_count) {
1235 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001236 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001237 } else {
1238 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001239 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001240 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001241 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001242 if ((retval == EFBIG) &&
1243 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001244 (fs_param.s_log_block_size == 0)) {
1245 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001246 blocksize = 4096;
1247 goto retry;
1248 }
1249 }
1250
Theodore Ts'oa789d841998-03-30 01:20:55 +00001251 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1252 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001253 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001254 exit(1);
1255 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001256 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001257 if (retval == EXT2_ET_UNIMPLEMENTED) {
1258 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001259 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001260 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001261 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001262 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001263 } else {
1264 if (dev_size == 0) {
1265 com_err(program_name, 0,
1266 _("Device size reported to be zero. "
1267 "Invalid partition specified, or\n\t"
1268 "partition table wasn't reread "
1269 "after running fdisk, due to\n\t"
1270 "a modified partition being busy "
1271 "and in use. You may need to reboot\n\t"
1272 "to re-read your partition table.\n"
1273 ));
1274 exit(1);
1275 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001276 fs_param.s_blocks_count = dev_size;
1277 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1278 fs_param.s_blocks_count &= ~((sys_page_size /
1279 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001280 }
1281
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001282 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001283 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001284 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001285 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001286 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001287
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001288 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001289 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001290 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1291
Brian Behlendorfe0661502007-03-19 08:25:38 -04001292 if (fs_param.s_feature_incompat &
1293 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1294 fs_type = "journal";
1295 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001296 fs_type = "floppy";
1297 else if (megs <= 512)
1298 fs_type = "small";
1299 else
1300 fs_type = "default";
1301 }
1302
1303 /* Figure out what features should be enabled */
1304
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001305 tmp = tmp2 = NULL;
1306 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1307 profile_get_string(profile, "defaults", "base_features", 0,
Eric Sandeena25d2312007-05-07 23:38:14 -04001308 "sparse_super,filetype,resize_inode,dir_index",
1309 &tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001310 profile_get_string(profile, "fs_types", fs_type,
1311 "base_features", tmp, &tmp2);
1312 edit_feature(tmp2, &fs_param.s_feature_compat);
1313 free(tmp);
1314 free(tmp2);
1315
1316 tmp = tmp2 = NULL;
1317 profile_get_string(profile, "defaults", "default_features", 0,
1318 "", &tmp);
1319 profile_get_string(profile, "fs_types", fs_type,
1320 "default_features", tmp, &tmp2);
1321 }
1322 edit_feature(fs_features ? fs_features : tmp2,
1323 &fs_param.s_feature_compat);
1324 if (tmp)
1325 free(tmp);
1326 if (tmp2)
1327 free(tmp2);
1328
1329 if (r_opt == EXT2_GOOD_OLD_REV &&
1330 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1331 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001332 fprintf(stderr, _("Filesystem features not supported "
1333 "with revision 0 filesystems\n"));
1334 exit(1);
1335 }
1336
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001337 if (s_opt > 0) {
1338 if (r_opt == EXT2_GOOD_OLD_REV) {
1339 fprintf(stderr, _("Sparse superblocks not supported "
1340 "with revision 0 filesystems\n"));
1341 exit(1);
1342 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001343 fs_param.s_feature_ro_compat |=
1344 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001345 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001346 fs_param.s_feature_ro_compat &=
1347 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1348
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001349 if (journal_size != 0) {
1350 if (r_opt == EXT2_GOOD_OLD_REV) {
1351 fprintf(stderr, _("Journals not supported "
1352 "with revision 0 filesystems\n"));
1353 exit(1);
1354 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001355 fs_param.s_feature_compat |=
1356 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001357 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001358
1359 if (fs_param.s_feature_incompat &
1360 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001361 reserved_ratio = 0;
1362 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1363 fs_param.s_feature_compat = 0;
1364 fs_param.s_feature_ro_compat = 0;
1365 }
1366
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001367 /* Set first meta blockgroup via an environment variable */
1368 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001369 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001370 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001371 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001372
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001373 /* Get the hardware sector size, if available */
1374 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1375 if (retval) {
1376 com_err(program_name, retval,
1377 _("while trying to determine hardware sector size"));
1378 exit(1);
1379 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001380
Theodore Ts'o54434922003-12-07 01:28:50 -05001381 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001382 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001383
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001384 if (blocksize <= 0) {
1385 profile_get_integer(profile, "defaults", "blocksize", 0,
Eric Sandeena25d2312007-05-07 23:38:14 -04001386 4096, &use_bsize);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001387 profile_get_integer(profile, "fs_types", fs_type,
1388 "blocksize", use_bsize, &use_bsize);
1389
1390 if (use_bsize == -1) {
1391 use_bsize = sys_page_size;
1392 if ((linux_version_code < (2*65536 + 6*256)) &&
1393 (use_bsize > 4096))
1394 use_bsize = 4096;
1395 }
1396 if (sector_size && use_bsize < sector_size)
1397 use_bsize = sector_size;
1398 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1399 use_bsize = -blocksize;
1400 blocksize = use_bsize;
1401 fs_param.s_blocks_count /= blocksize / 1024;
1402 }
1403
1404 if (inode_ratio == 0) {
1405 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1406 8192, &inode_ratio);
1407 profile_get_integer(profile, "fs_types", fs_type,
1408 "inode_ratio", inode_ratio,
1409 &inode_ratio);
1410
1411 if (inode_ratio < blocksize)
1412 inode_ratio = blocksize;
1413 }
1414
1415 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1416 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1417
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001418 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001419
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001420 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001421 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001422
1423 /* Since sparse_super is the default, we would only have a problem
1424 * here if it was explicitly disabled.
1425 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001426 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1427 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001428 com_err(program_name, 0,
1429 _("reserved online resize blocks not supported "
1430 "on non-sparse filesystem"));
1431 exit(1);
1432 }
1433
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001434 if (fs_param.s_blocks_per_group) {
1435 if (fs_param.s_blocks_per_group < 256 ||
1436 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001437 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001438 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001439 exit(1);
1440 }
1441 }
1442
Theodore Ts'o642935c2006-11-14 23:38:17 -05001443 if (!force && fs_param.s_blocks_count >= ((unsigned) 1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001444 com_err(program_name, 0,
1445 _("Filesystem too large. No more than 2**31-1 blocks\n"
1446 "\t (8TB using a blocksize of 4k) are currently supported."));
1447 exit(1);
1448 }
1449
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001450 if ((blocksize > 4096) &&
1451 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1452 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1453 "blocksizes greater than 4096\n\tusing ext3. "
1454 "Use -b 4096 if this is an issue for you.\n\n"));
1455
Andreas Dilger067911a2006-07-15 22:08:20 -04001456 if (inode_size == 0) {
1457 profile_get_integer(profile, "defaults", "inode_size", NULL,
1458 0, &inode_size);
1459 profile_get_integer(profile, "fs_types", fs_type,
1460 "inode_size", inode_size,
1461 &inode_size);
1462 }
1463
1464 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001465 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001466 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001467 inode_size & (inode_size - 1)) {
1468 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001469 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001470 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001471 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001472 exit(1);
1473 }
1474 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1475 fprintf(stderr, _("Warning: %d-byte inodes not usable "
Andreas Dilger067911a2006-07-15 22:08:20 -04001476 "on older systems\n"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001477 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001478 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001479 }
1480
Eric Sandeenf3358642006-09-12 14:56:17 -04001481 /* Make sure number of inodes specified will fit in 32 bits */
1482 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001483 unsigned long long n;
1484 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001485 if (n > ~0U) {
1486 com_err(program_name, 0,
1487 _("too many inodes (%llu), raise inode ratio?"), n);
1488 exit(1);
1489 }
1490 } else if (num_inodes > ~0U) {
1491 com_err(program_name, 0,
1492 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001493 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001494 exit(1);
1495 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001496 /*
1497 * Calculate number of inodes based on the inode ratio
1498 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001499 fs_param.s_inodes_count = num_inodes ? num_inodes :
1500 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001501 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001502
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001503 if ((((long long)fs_param.s_inodes_count) *
1504 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1505 (((long long)fs_param.s_blocks_count) *
1506 EXT2_BLOCK_SIZE(&fs_param))) {
1507 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1508 "(%u) too big for a\n\t"
1509 "filesystem with %lu blocks, "
1510 "specify higher inode_ratio (-i)\n\t"
1511 "or lower inode count (-N).\n"),
1512 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001513 fs_param.s_inodes_count,
1514 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001515 exit(1);
1516 }
1517
Theodore Ts'o3839e651997-04-26 13:21:57 +00001518 /*
1519 * Calculate number of blocks to reserve
1520 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001521 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1522 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001523}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001524
Theodore Ts'o3839e651997-04-26 13:21:57 +00001525int main (int argc, char *argv[])
1526{
1527 errcode_t retval = 0;
1528 ext2_filsys fs;
1529 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001530 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001531 unsigned int i;
1532 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001533 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001534
1535#ifdef ENABLE_NLS
1536 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001537 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001538 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1539 textdomain(NLS_CAT_NAME);
1540#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001541 PRS(argc, argv);
1542
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001543#ifdef CONFIG_TESTIO_DEBUG
1544 io_ptr = test_io_manager;
1545 test_io_backing_manager = unix_io_manager;
1546#else
1547 io_ptr = unix_io_manager;
1548#endif
1549
Theodore Ts'o3839e651997-04-26 13:21:57 +00001550 /*
1551 * Initialize the superblock....
1552 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001553 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001554 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001555 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001556 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001557 exit(1);
1558 }
1559
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001560 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001561 * Wipe out the old on-disk superblock
1562 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001563 if (!noaction)
1564 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001565
1566 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001567 * Generate a UUID for it...
1568 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001569 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001570
1571 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001572 * Initialize the directory index variables
1573 */
1574 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1575 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1576
1577 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001578 * Add "jitter" to the superblock's check interval so that we
1579 * don't check all the filesystems at the same time. We use a
1580 * kludgy hack of using the UUID to derive a random jitter value.
1581 */
1582 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1583 val += fs->super->s_uuid[i];
1584 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1585
1586 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001587 * Override the creator OS, if applicable
1588 */
1589 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001590 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001591 exit(1);
1592 }
1593
1594 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001595 * For the Hurd, we will turn off filetype since it doesn't
1596 * support it.
1597 */
1598 if (fs->super->s_creator_os == EXT2_OS_HURD)
1599 fs->super->s_feature_incompat &=
1600 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1601
1602 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001603 * Set the volume label...
1604 */
1605 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001606 memset(fs->super->s_volume_name, 0,
1607 sizeof(fs->super->s_volume_name));
1608 strncpy(fs->super->s_volume_name, volume_label,
1609 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001610 }
1611
1612 /*
1613 * Set the last mount directory
1614 */
1615 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001616 memset(fs->super->s_last_mounted, 0,
1617 sizeof(fs->super->s_last_mounted));
1618 strncpy(fs->super->s_last_mounted, mount_dir,
1619 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001620 }
1621
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001622 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001623 show_stats(fs);
1624
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001625 if (noaction)
1626 exit(0);
1627
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001628 if (fs->super->s_feature_incompat &
1629 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1630 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001631 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001632 }
1633
Theodore Ts'o3839e651997-04-26 13:21:57 +00001634 if (bad_blocks_filename)
1635 read_bb_file(fs, &bb_list, bad_blocks_filename);
1636 if (cflag)
1637 test_disk(fs, &bb_list);
1638
1639 handle_bad_blocks(fs, bb_list);
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -04001640 fs->stride = fs->super->s_raid_stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001641 retval = ext2fs_allocate_tables(fs);
1642 if (retval) {
1643 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001644 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001645 exit(1);
1646 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001647 if (super_only) {
1648 fs->super->s_state |= EXT2_ERROR_FS;
1649 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1650 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001651 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001652 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001653 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001654 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001655 blk_t ret_blk;
1656
1657#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001658 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001659#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001660
1661 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001662 * Wipe out any old MD RAID (or other) metadata at the end
1663 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001664 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001665 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001666 start = (blocks & ~(rsv - 1));
1667 if (start > rsv)
1668 start -= rsv;
1669 if (start > 0)
1670 retval = zero_blocks(fs, start, blocks - start,
1671 NULL, &ret_blk, NULL);
1672
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001673 if (retval) {
1674 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001675 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001676 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001677 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001678 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001679 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001680 create_root_dir(fs);
1681 create_lost_and_found(fs);
1682 reserve_inodes(fs);
1683 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001684 if (fs->super->s_feature_compat &
1685 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1686 retval = ext2fs_create_resize_inode(fs);
1687 if (retval) {
1688 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001689 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001690 exit(1);
1691 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001692 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001693 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001694
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001695 if (journal_device) {
1696 ext2_filsys jfs;
1697
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001698 if (!force)
1699 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001700 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001701
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001702 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1703 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1704 fs->blocksize, unix_io_manager, &jfs);
1705 if (retval) {
1706 com_err(program_name, retval,
1707 _("while trying to open journal device %s\n"),
1708 journal_device);
1709 exit(1);
1710 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001711 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001712 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001713 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001714 fflush(stdout);
1715 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001716 retval = ext2fs_add_journal_device(fs, jfs);
1717 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001718 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001719 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001720 journal_device);
1721 exit(1);
1722 }
1723 if (!quiet)
1724 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001725 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001726 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001727 } else if ((journal_size) ||
1728 (fs_param.s_feature_compat &
1729 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001730 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001731
1732 if (!journal_blocks) {
1733 fs->super->s_feature_compat &=
1734 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1735 goto no_journal;
1736 }
1737 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001738 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001739 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001740 fflush(stdout);
1741 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001742 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1743 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001744 if (retval) {
1745 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001746 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001747 exit(1);
1748 }
1749 if (!quiet)
1750 printf(_("done\n"));
1751 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001752no_journal:
1753
Theodore Ts'o3839e651997-04-26 13:21:57 +00001754 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001755 printf(_("Writing superblocks and "
1756 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001757 retval = ext2fs_flush(fs);
1758 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001759 fprintf(stderr,
1760 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001761 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001762 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001763 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001764 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1765 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001766 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001767 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001768 remove_error_table(&et_ext2_error_table);
1769 remove_error_table(&et_prof_error_table);
Andreas Dilger568101f2001-10-13 01:22:25 -06001770 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001771}