blob: 5c59b488b30fc1a8fdcbd3f452074a3662fd857c [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'o3839e651997-04-26 13:21:57 +000055#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000056#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
58#define STRIDE_LENGTH 8
59
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000060#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000061#define ZAP_BOOTBLOCK
62#endif
63
Theodore Ts'o3839e651997-04-26 13:21:57 +000064extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000065extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
67const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000068const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
70/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000071int cflag;
72int verbose;
73int quiet;
74int super_only;
75int force;
76int noaction;
77int journal_size;
78int journal_flags;
79char *bad_blocks_filename;
80__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050082struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000083char *creator_os;
84char *volume_label;
85char *mount_dir;
86char *journal_device;
87int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000088
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050089profile_t profile;
90
Theodore Ts'o31e29a12002-05-17 10:53:07 -040091int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040092int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040093
Theodore Ts'o63985322001-01-03 17:02:13 +000094static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000095{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000097 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000098 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
99 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +0000100 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500101 "[-r fs-revision] [-R options] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 program_name);
103 exit(1);
104}
105
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000106static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107{
108 int l = 0;
109
110 arg >>= 1;
111 while (arg) {
112 l++;
113 arg >>= 1;
114 }
115 return l;
116}
117
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000118static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000119{
120 int l;
121
122 for (l=0; arg ; l++)
123 arg = arg / 10;
124 return l;
125}
126
Theodore Ts'od99225e2004-09-25 07:40:12 -0400127static int parse_version_number(const char *s)
128{
129 int major, minor, rev;
130 char *endptr;
131 const char *cp = s;
132
133 if (!s)
134 return 0;
135 major = strtol(cp, &endptr, 10);
136 if (cp == endptr || *endptr != '.')
137 return 0;
138 cp = endptr + 1;
139 minor = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
141 return 0;
142 cp = endptr + 1;
143 rev = strtol(cp, &endptr, 10);
144 if (cp == endptr)
145 return 0;
146 return ((((major * 256) + minor) * 256) + rev);
147}
148
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000149/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 * Helper function for read_bb_file and test_disk
151 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500152static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153{
Theodore Ts'o66938372002-03-08 00:14:46 -0500154 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 return;
156}
157
158/*
159 * Reads the bad blocks list from a file
160 */
161static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
162 const char *bad_blocks_file)
163{
164 FILE *f;
165 errcode_t retval;
166
167 f = fopen(bad_blocks_file, "r");
168 if (!f) {
169 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000170 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171 exit(1);
172 }
173 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
174 fclose (f);
175 if (retval) {
176 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000177 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 exit(1);
179 }
180}
181
182/*
183 * Runs the badblocks program to test the disk
184 */
185static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
186{
187 FILE *f;
188 errcode_t retval;
189 char buf[1024];
190
Theodore Ts'of63978a2006-05-13 09:25:47 -0400191 sprintf(buf, "badblocks -b %d -X %s%s%s %d", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500192 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
193 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000195 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 f = popen(buf, "r");
197 if (!f) {
198 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400199 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 exit(1);
201 }
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000203 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 if (retval) {
205 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000206 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 exit(1);
208 }
209}
210
211static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
212{
Theodore Ts'o54434922003-12-07 01:28:50 -0500213 dgrp_t i;
214 blk_t j;
215 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000216 blk_t blk;
217 badblocks_iterate bb_iter;
218 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000219 blk_t group_block;
220 int group;
221 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222
223 if (!bb_list)
224 return;
225
226 /*
227 * The primary superblock and group descriptors *must* be
228 * good; if not, abort.
229 */
230 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
231 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000232 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000233 fprintf(stderr, _("Block %d in primary "
234 "superblock/group descriptor area bad.\n"), i);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500235 fprintf(stderr, _("Blocks %u through %d must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000236 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500238 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 exit(1);
240 }
241 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000242
243 /*
244 * See if any of the bad blocks are showing up in the backup
245 * superblocks and/or group descriptors. If so, issue a
246 * warning and adjust the block counts appropriately.
247 */
248 group_block = fs->super->s_first_data_block +
249 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000250
251 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000252 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000253 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000254 if (ext2fs_badblocks_list_test(bb_list,
255 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000256 if (!group_bad)
257 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500258_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000259" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000260 group_block);
261 group_bad++;
262 group = ext2fs_group_of_blk(fs, group_block+j);
263 fs->group_desc[group].bg_free_blocks_count++;
264 fs->super->s_free_blocks_count++;
265 }
266 }
267 group_block += fs->super->s_blocks_per_group;
268 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269
270 /*
271 * Mark all the bad blocks as used...
272 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000273 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000275 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000276 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 exit(1);
278 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000279 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282}
283
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000284/*
285 * These functions implement a generalized progress meter.
286 */
287struct progress_struct {
288 char format[20];
289 char backup[80];
290 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400291 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000292};
293
294static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500295 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000297 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000298
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000299 memset(progress, 0, sizeof(struct progress_struct));
300 if (quiet)
301 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000302
303 /*
304 * Figure out how many digits we need
305 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000306 i = int_log10(max);
307 sprintf(progress->format, "%%%dd/%%%dld", i, i);
308 memset(progress->backup, '\b', sizeof(progress->backup)-1);
309 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500310 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000311 progress->backup[(2*i)+1] = 0;
312 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000313
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400314 progress->skip_progress = 0;
315 if (getenv("MKE2FS_SKIP_PROGRESS"))
316 progress->skip_progress++;
317
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000318 fputs(label, stdout);
319 fflush(stdout);
320}
321
322static void progress_update(struct progress_struct *progress, __u32 val)
323{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400324 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000325 return;
326 printf(progress->format, val, progress->max);
327 fputs(progress->backup, stdout);
328}
329
330static void progress_close(struct progress_struct *progress)
331{
332 if (progress->format[0] == 0)
333 return;
334 fputs(_("done \n"), stdout);
335}
336
337
338/*
339 * Helper function which zeros out _num_ blocks starting at _blk_. In
340 * case of an error, the details of the error is returned via _ret_blk_
341 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
342 * success, and an error code on an error.
343 *
344 * As a special case, if the first argument is NULL, then it will
345 * attempt to free the static zeroizing buffer. (This is to keep
346 * programs that check for memory leaks happy.)
347 */
348static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
349 struct progress_struct *progress,
350 blk_t *ret_blk, int *ret_count)
351{
352 int j, count, next_update, next_update_incr;
353 static char *buf;
354 errcode_t retval;
355
356 /* If fs is null, clean up the static buffer and return */
357 if (!fs) {
358 if (buf) {
359 free(buf);
360 buf = 0;
361 }
362 return 0;
363 }
364 /* Allocate the zeroizing buffer if necessary */
365 if (!buf) {
366 buf = malloc(fs->blocksize * STRIDE_LENGTH);
367 if (!buf) {
368 com_err("malloc", ENOMEM,
369 _("while allocating zeroizing buffer"));
370 exit(1);
371 }
372 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
373 }
374 /* OK, do the write loop */
375 next_update = 0;
376 next_update_incr = num / 100;
377 if (next_update_incr < 1)
378 next_update_incr = 1;
379 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400380 count = num - j;
381 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000382 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000383 retval = io_channel_write_blk(fs->io, blk, count, buf);
384 if (retval) {
385 if (ret_count)
386 *ret_count = count;
387 if (ret_blk)
388 *ret_blk = blk;
389 return retval;
390 }
391 if (progress && j > next_update) {
392 next_update += num / 100;
393 progress_update(progress, blk);
394 }
395 }
396 return 0;
397}
398
399static void write_inode_tables(ext2_filsys fs)
400{
401 errcode_t retval;
402 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500403 dgrp_t i;
404 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000405 struct progress_struct progress;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400406 int lazy_flag = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407
408 if (quiet)
409 memset(&progress, 0, sizeof(progress));
410 else
411 progress_init(&progress, _("Writing inode tables: "),
412 fs->group_desc_count);
413
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400414 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
415 EXT2_FEATURE_COMPAT_LAZY_BG))
416 lazy_flag = 1;
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'of5fa2002006-05-08 20:17:26 -0400424 if (!(lazy_flag &&
425 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
426 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
427 if (retval) {
428 fprintf(stderr, _("\nCould not write %d "
429 "blocks in inode table starting at %u: %s\n"),
430 num, blk, error_message(retval));
431 exit(1);
432 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000433 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000434 if (sync_kludge) {
435 if (sync_kludge == 1)
436 sync();
437 else if ((i % sync_kludge) == 0)
438 sync();
439 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000441 zero_blocks(0, 0, 0, 0, 0, 0);
442 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443}
444
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400445static void setup_lazy_bg(ext2_filsys fs)
446{
447 dgrp_t i;
448 int blks;
449 struct ext2_super_block *sb = fs->super;
450 struct ext2_group_desc *bg = fs->group_desc;
451
452 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
453 EXT2_FEATURE_COMPAT_LAZY_BG)) {
454 for (i = 0; i < fs->group_desc_count; i++, bg++) {
455 if ((i == 0) ||
456 (i == fs->group_desc_count-1))
457 continue;
458 if (bg->bg_free_inodes_count ==
459 sb->s_inodes_per_group) {
460 bg->bg_free_inodes_count = 0;
461 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
462 sb->s_free_inodes_count -=
463 sb->s_inodes_per_group;
464 }
465 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
466 if (bg->bg_free_blocks_count == blks) {
467 bg->bg_free_blocks_count = 0;
468 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
469 sb->s_free_blocks_count -= blks;
470 }
471 }
472 }
473}
474
475
Theodore Ts'o3839e651997-04-26 13:21:57 +0000476static void create_root_dir(ext2_filsys fs)
477{
478 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480
481 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
482 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000483 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 exit(1);
485 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000486 if (geteuid()) {
487 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
488 if (retval) {
489 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000490 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000491 exit(1);
492 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000493 inode.i_uid = getuid();
494 if (inode.i_uid)
495 inode.i_gid = getgid();
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500496 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000497 if (retval) {
498 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000499 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000500 exit(1);
501 }
502 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503}
504
505static void create_lost_and_found(ext2_filsys fs)
506{
507 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000508 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 const char *name = "lost+found";
510 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000511 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512
Theodore Ts'o6a525062001-12-24 09:40:00 -0500513 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
515 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000516 com_err("ext2fs_mkdir", retval,
517 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 exit(1);
519 }
520
521 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
522 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000523 com_err("ext2_lookup", retval,
524 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 exit(1);
526 }
527
528 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000529 if ((lpf_size += fs->blocksize) >= 16*1024)
530 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531 retval = ext2fs_expand_dir(fs, ino);
532 if (retval) {
533 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000534 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 exit(1);
536 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500537 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538}
539
540static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
541{
542 errcode_t retval;
543
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 fs->group_desc[0].bg_free_inodes_count--;
546 fs->super->s_free_inodes_count--;
547 retval = ext2fs_update_bb_inode(fs, bb_list);
548 if (retval) {
549 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000550 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 exit(1);
552 }
553
554}
555
556static void reserve_inodes(ext2_filsys fs)
557{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000558 ext2_ino_t i;
559 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000561 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000562 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000563 group = ext2fs_group_of_ino(fs, i);
564 fs->group_desc[group].bg_free_inodes_count--;
565 fs->super->s_free_inodes_count--;
566 }
567 ext2fs_mark_ib_dirty(fs);
568}
569
Theodore Ts'o756df352002-03-07 20:52:12 -0500570#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500571#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500572#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500573
Theodore Ts'o04a96852001-08-30 21:55:26 -0400574static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000575{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400576 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000577 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500578 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000579
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400580 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600581 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500582 printf(_("Out of memory erasing sectors %d-%d\n"),
583 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600584 exit(1);
585 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500586
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500587 if (sect == 0) {
588 /* Check for a BSD disklabel, and don't erase it if so */
589 retval = io_channel_read_blk(fs->io, 0, -512, buf);
590 if (retval)
591 fprintf(stderr,
592 _("Warning: could not read block 0: %s\n"),
593 error_message(retval));
594 else {
595 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
596 if ((*magic == BSD_DISKMAGIC) ||
597 (*magic == BSD_MAGICDISK))
598 return;
599 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500600 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500601
Theodore Ts'o70988102002-07-14 08:00:00 -0400602 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000603 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400604 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000605 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400606 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000607 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500608 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
609 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000610}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000611
612static void create_journal_dev(ext2_filsys fs)
613{
614 struct progress_struct progress;
615 errcode_t retval;
616 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000617 blk_t blk;
618 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000619
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000620 retval = ext2fs_create_journal_superblock(fs,
621 fs->super->s_blocks_count, 0, &buf);
622 if (retval) {
623 com_err("create_journal_dev", retval,
624 _("while initializing journal superblock"));
625 exit(1);
626 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000627 if (quiet)
628 memset(&progress, 0, sizeof(progress));
629 else
630 progress_init(&progress, _("Zeroing journal device: "),
631 fs->super->s_blocks_count);
632
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000633 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
634 &progress, &blk, &count);
635 if (retval) {
636 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000637 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000638 blk, count);
639 exit(1);
640 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000641 zero_blocks(0, 0, 0, 0, 0, 0);
642
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000643 retval = io_channel_write_blk(fs->io,
644 fs->super->s_first_data_block+1,
645 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000646 if (retval) {
647 com_err("create_journal_dev", retval,
648 _("while writing journal superblock"));
649 exit(1);
650 }
651 progress_close(&progress);
652}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000653
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654static void show_stats(ext2_filsys fs)
655{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000656 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000657 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500658 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500660 dgrp_t i;
661 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500663 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500664 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500665 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000666
667 memset(buf, 0, sizeof(buf));
668 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000669 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500670 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500671 os = e2p_os2string(fs->super->s_creator_os);
672 fputs(os, stdout);
673 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000674 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000675 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000676 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000678 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000681 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682 s->s_r_blocks_count,
683 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000684 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500685 if (s->s_reserved_gdt_blocks)
686 printf(_("Maximum filesystem blocks=%lu\n"),
687 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
688 (fs->blocksize / sizeof(struct ext2_group_desc)) *
689 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000690 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000691 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000692 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000693 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000694 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000696 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697
698 if (fs->group_desc_count == 1) {
699 printf("\n");
700 return;
701 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500702
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000703 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704 group_block = s->s_first_data_block;
705 col_left = 0;
706 for (i = 1; i < fs->group_desc_count; i++) {
707 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000708 if (!ext2fs_bg_has_super(fs, i))
709 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000710 if (i != 1)
711 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000712 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000713 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000715 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000716 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000717 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000718 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 }
720 printf("\n\n");
721}
722
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000723/*
724 * Set the S_CREATOR_OS field. Return true if OS is known,
725 * otherwise, 0.
726 */
727static int set_os(struct ext2_super_block *sb, char *os)
728{
729 if (isdigit (*os))
730 sb->s_creator_os = atoi (os);
731 else if (strcasecmp(os, "linux") == 0)
732 sb->s_creator_os = EXT2_OS_LINUX;
733 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
734 sb->s_creator_os = EXT2_OS_HURD;
735 else if (strcasecmp(os, "masix") == 0)
736 sb->s_creator_os = EXT2_OS_MASIX;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500737 else if (strcasecmp(os, "freebsd") == 0)
738 sb->s_creator_os = EXT2_OS_FREEBSD;
739 else if (strcasecmp(os, "lites") == 0)
740 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000741 else
742 return 0;
743 return 1;
744}
745
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000746#define PATH_SET "PATH=/sbin"
747
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500748static void parse_extended_opts(struct ext2_super_block *param,
749 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000750{
751 char *buf, *token, *next, *p, *arg;
752 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500753 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000754
755 len = strlen(opts);
756 buf = malloc(len+1);
757 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500758 fprintf(stderr,
759 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000760 exit(1);
761 }
762 strcpy(buf, opts);
763 for (token = buf; token && *token; token = next) {
764 p = strchr(token, ',');
765 next = 0;
766 if (p) {
767 *p = 0;
768 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500769 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000770 arg = strchr(token, '=');
771 if (arg) {
772 *arg = 0;
773 arg++;
774 }
775 if (strcmp(token, "stride") == 0) {
776 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500777 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000778 continue;
779 }
780 fs_stride = strtoul(arg, &p, 0);
781 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000782 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400783 _("Invalid stride parameter: %s\n"),
784 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500785 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000786 continue;
787 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500788 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500789 unsigned long resize, bpg, rsv_groups;
790 unsigned long group_desc_count, desc_blocks;
791 unsigned int gdpb, blocksize;
792 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500793
794 if (!arg) {
795 r_usage++;
796 continue;
797 }
798
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500799 resize = parse_num_blocks(arg,
800 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500801
802 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500803 fprintf(stderr,
804 _("Invalid resize parameter: %s\n"),
805 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500806 r_usage++;
807 continue;
808 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500809 if (resize <= param->s_blocks_count) {
810 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400811 _("The resize maximum must be greater "
812 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500813 r_usage++;
814 continue;
815 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500816
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500817 blocksize = EXT2_BLOCK_SIZE(param);
818 bpg = param->s_blocks_per_group;
819 if (!bpg)
820 bpg = blocksize * 8;
821 gdpb = blocksize / sizeof(struct ext2_group_desc);
822 group_desc_count = (param->s_blocks_count +
823 bpg - 1) / bpg;
824 desc_blocks = (group_desc_count +
825 gdpb - 1) / gdpb;
826 rsv_groups = (resize + bpg - 1) / bpg;
827 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
828 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500829 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500830 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
831
832 if (rsv_gdb > 0) {
833 param->s_feature_compat |=
834 EXT2_FEATURE_COMPAT_RESIZE_INODE;
835
836 param->s_reserved_gdt_blocks = rsv_gdb;
837 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000838 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500839 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000840 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500841 if (r_usage) {
842 fprintf(stderr, _("\nBad options specified.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400843 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000844 "and may take an argument which\n"
845 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400846 "Valid extended options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500847 "\tstride=<stride length in blocks>\n"
848 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000849 exit(1);
850 }
851}
852
Theodore Ts'o896938d1999-10-23 01:04:50 +0000853static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400854 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500855 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400856 EXT2_FEATURE_COMPAT_DIR_INDEX |
857 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000858 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400859 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
860 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000861 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
862};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000863
864
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500865static void syntax_err_report(const char *filename, long err, int line_num)
866{
867 fprintf(stderr,
868 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
869 filename, line_num, error_message(err));
870 exit(1);
871}
872
873static const char *config_fn[] = { "/etc/mke2fs.conf", 0 };
874
875static void edit_feature(const char *str, __u32 *compat_array)
876{
877 if (!str)
878 return;
879
880 if (e2p_edit_feature(str, compat_array, ok_features)) {
881 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
882 str);
883 exit(1);
884 }
885}
886
Theodore Ts'o3839e651997-04-26 13:21:57 +0000887static void PRS(int argc, char *argv[])
888{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400889 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000890 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500891 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000892 int blocksize = 0;
893 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600894 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500895 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400896 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500897 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000898 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000899 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000900 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500901 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500902 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000903 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500904#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000905 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000906#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400907 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500908 int s_opt = -1, r_opt = -1;
909 char *fs_features = 0;
910 int use_bsize;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000911
Theodore Ts'o3839e651997-04-26 13:21:57 +0000912 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000913 if (oldpath) {
914 char *newpath;
915
916 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
917 strcpy (newpath, PATH_SET);
918 strcat (newpath, ":");
919 strcat (newpath, oldpath);
920 putenv (newpath);
921 } else
922 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000923
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000924 tmp = getenv("MKE2FS_SYNC");
925 if (tmp)
926 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400927
928 /* Determine the system page size if possible */
929#ifdef HAVE_SYSCONF
930#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
931#define _SC_PAGESIZE _SC_PAGE_SIZE
932#endif
933#ifdef _SC_PAGESIZE
934 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600935 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400936 sys_page_size = sysval;
937#endif /* _SC_PAGESIZE */
938#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500939
940 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
941 config_fn[0] = tmp;
942 profile_set_syntax_err_cb(syntax_err_report);
943 profile_init(config_fn, &profile);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000944
Theodore Ts'o3839e651997-04-26 13:21:57 +0000945 setbuf(stdout, NULL);
946 setbuf(stderr, NULL);
947 initialize_ext2_error_table();
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500948 memset(&fs_param, 0, sizeof(struct ext2_super_block));
949 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400950
Theodore Ts'o756df352002-03-07 20:52:12 -0500951#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000952 if (uname(&ut)) {
953 perror("uname");
954 exit(1);
955 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400956 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500957 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500958 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000959#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700960
961 if (argc && *argv) {
962 program_name = get_progname(*argv);
963
964 /* If called as mkfs.ext3, create a journal inode */
965 if (!strcmp(program_name, "mkfs.ext3"))
966 journal_size = -1;
967 }
968
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000969 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500970 "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 +0000971 switch (c) {
972 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400973 blocksize = strtol(optarg, &tmp, 0);
974 b = (blocksize > 0) ? blocksize : -blocksize;
975 if (b < EXT2_MIN_BLOCK_SIZE ||
976 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000977 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400978 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000979 exit(1);
980 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600981 if (blocksize > 4096)
982 fprintf(stderr, _("Warning: blocksize %d not "
983 "usable on most systems.\n"),
984 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400985 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500986 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400987 int_log2(blocksize >>
988 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000989 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000990 case 'c': /* Check for bad blocks */
991 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500992 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993 break;
994 case 'f':
995 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600996 if (size < EXT2_MIN_BLOCK_SIZE ||
997 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000998 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -0400999 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 optarg);
1001 exit(1);
1002 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001003 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001004 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001005 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001006 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007 break;
1008 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001009 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001010 if (*tmp) {
1011 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001012 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001013 exit(1);
1014 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001015 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001016 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001017 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001018 exit(1);
1019 }
1020 break;
1021 case 'i':
1022 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001023 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1024 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001025 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001026 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001027 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001028 optarg, EXT2_MIN_BLOCK_SIZE,
1029 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001030 exit(1);
1031 }
1032 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001033 case 'J':
1034 parse_journal_opts(optarg);
1035 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001036 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001037 if (!journal_size)
1038 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001039 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001040 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001041 bad_blocks_filename = malloc(strlen(optarg)+1);
1042 if (!bad_blocks_filename) {
1043 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001044 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001045 exit(1);
1046 }
1047 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001048 break;
1049 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001050 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001051 if (reserved_ratio > 50 || *tmp) {
1052 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001053 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001054 optarg);
1055 exit(1);
1056 }
1057 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001058 case 'n':
1059 noaction++;
1060 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001061 case 'o':
1062 creator_os = optarg;
1063 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001064 case 'q':
1065 quiet = 1;
1066 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001067 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001068 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001069 if (*tmp) {
1070 com_err(program_name, 0,
1071 _("bad revision level - %s"), optarg);
1072 exit(1);
1073 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001074 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001075 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001076 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001077 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001078 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001079#ifdef EXT2_DYNAMIC_REV
1080 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001081 inode_size = strtoul(optarg, &tmp, 0);
1082 if (*tmp) {
1083 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001084 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001085 exit(1);
1086 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001087 break;
1088#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001089 case 'v':
1090 verbose = 1;
1091 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001092 case 'F':
1093 force = 1;
1094 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001095 case 'L':
1096 volume_label = optarg;
1097 break;
1098 case 'M':
1099 mount_dir = optarg;
1100 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001101 case 'N':
1102 num_inodes = strtoul(optarg, &tmp, 0);
1103 if (*tmp) {
1104 com_err(program_name, 0,
1105 _("bad num inodes - %s"), optarg);
1106 exit(1);
1107 }
1108 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001109 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001110 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001111 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001112 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001113 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001114 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001115 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001116 case 'S':
1117 super_only = 1;
1118 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001119 case 'T':
1120 fs_type = optarg;
1121 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001122 case 'V':
1123 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001124 show_version_only++;
1125 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001126 default:
1127 usage();
1128 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001129 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001130 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001131 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001132 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001133
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001134 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001135 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1136 E2FSPROGS_DATE);
1137
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001138 if (show_version_only) {
1139 fprintf(stderr, _("\tUsing %s\n"),
1140 error_message(EXT2_ET_BASE));
1141 exit(0);
1142 }
1143
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001144 /*
1145 * If there's no blocksize specified and there is a journal
1146 * device, use it to figure out the blocksize
1147 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001148 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001149 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001150 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001151
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001152#ifdef CONFIG_TESTIO_DEBUG
1153 io_ptr = test_io_manager;
1154 test_io_backing_manager = unix_io_manager;
1155#else
1156 io_ptr = unix_io_manager;
1157#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001158 retval = ext2fs_open(journal_device,
1159 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001160 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001161 if (retval) {
1162 com_err(program_name, retval,
1163 _("while trying to open journal device %s\n"),
1164 journal_device);
1165 exit(1);
1166 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001167 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001168 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001169 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001170 "minimum blocksize %d\n"), jfs->blocksize,
1171 -blocksize);
1172 exit(1);
1173 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001174 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001175 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001176 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1177 ext2fs_close(jfs);
1178 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001179
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001180 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001181 if (!force) {
1182 com_err(program_name, 0,
1183 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001184 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001185 proceed_question();
1186 }
1187 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1188 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001189 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001190 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001191 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001192 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1193 fs_param.s_log_block_size);
1194 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001195 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001196 argv[optind - 1]);
1197 exit(1);
1198 }
1199 }
1200 if (optind < argc)
1201 usage();
1202
Theodore Ts'o74becf31997-04-26 14:37:06 +00001203 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001204 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001205 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001206
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001207 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001208
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001209 if (noaction && fs_param.s_blocks_count) {
1210 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001211 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001212 } else {
1213 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001214 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001215 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001216 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001217 if ((retval == EFBIG) &&
1218 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001219 (fs_param.s_log_block_size == 0)) {
1220 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001221 blocksize = 4096;
1222 goto retry;
1223 }
1224 }
1225
Theodore Ts'oa789d841998-03-30 01:20:55 +00001226 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1227 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001228 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001229 exit(1);
1230 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001231 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001232 if (retval == EXT2_ET_UNIMPLEMENTED) {
1233 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001234 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001235 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001236 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001237 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001238 } else {
1239 if (dev_size == 0) {
1240 com_err(program_name, 0,
1241 _("Device size reported to be zero. "
1242 "Invalid partition specified, or\n\t"
1243 "partition table wasn't reread "
1244 "after running fdisk, due to\n\t"
1245 "a modified partition being busy "
1246 "and in use. You may need to reboot\n\t"
1247 "to re-read your partition table.\n"
1248 ));
1249 exit(1);
1250 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001251 fs_param.s_blocks_count = dev_size;
1252 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1253 fs_param.s_blocks_count &= ~((sys_page_size /
1254 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001255 }
1256
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001257 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001258 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001259 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001260 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001261 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001262
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001263 if (!fs_type) {
1264 int megs = fs_param.s_blocks_count *
1265 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1266
1267 if (megs <= 3)
1268 fs_type = "floppy";
1269 else if (megs <= 512)
1270 fs_type = "small";
1271 else
1272 fs_type = "default";
1273 }
1274
1275 /* Figure out what features should be enabled */
1276
1277 if (r_opt == EXT2_GOOD_OLD_REV && fs_features) {
1278 fprintf(stderr, _("Filesystem features not supported "
1279 "with revision 0 filesystems\n"));
1280 exit(1);
1281 }
1282
1283 profile_get_string(profile, "defaults", "base_features", 0,
1284 "filetype,sparse_super", &tmp);
1285 profile_get_string(profile, "fs_types", fs_type, "base_features",
1286 tmp, &tmp2);
1287 edit_feature(tmp2, &fs_param.s_feature_compat);
1288 free(tmp);
1289 free(tmp2);
1290
1291 profile_get_string(profile, "defaults", "default_features", 0,
1292 "", &tmp);
1293 profile_get_string(profile, "fs_types", fs_type,
1294 "default_features", tmp, &tmp2);
1295 edit_feature(fs_features ? fs_features : tmp2,
1296 &fs_param.s_feature_compat);
1297 free(tmp);
1298 free(tmp2);
1299
1300 if (s_opt > 0)
1301 fs_param.s_feature_ro_compat |=
1302 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1303 else if (s_opt == 0)
1304 fs_param.s_feature_ro_compat &=
1305 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1306
1307 if (journal_size != 0)
1308 fs_param.s_feature_compat |=
1309 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1310
1311 if (fs_param.s_feature_incompat &
1312 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1313 if (!fs_type)
1314 fs_type = "journal";
1315 reserved_ratio = 0;
1316 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1317 fs_param.s_feature_compat = 0;
1318 fs_param.s_feature_ro_compat = 0;
1319 }
1320
1321 if (fs_param.s_rev_level == EXT2_GOOD_OLD_REV) {
1322 fs_param.s_feature_incompat = 0;
1323 fs_param.s_feature_compat = 0;
1324 fs_param.s_feature_ro_compat = 0;
1325 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001326
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001327 /* Set first meta blockgroup via an environment variable */
1328 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001329 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001330 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001331 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001332
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001333 /* Get the hardware sector size, if available */
1334 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1335 if (retval) {
1336 com_err(program_name, retval,
1337 _("while trying to determine hardware sector size"));
1338 exit(1);
1339 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001340
Theodore Ts'o54434922003-12-07 01:28:50 -05001341 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001342 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001343
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001344 if (blocksize <= 0) {
1345 profile_get_integer(profile, "defaults", "blocksize", 0,
1346 1024, &use_bsize);
1347 profile_get_integer(profile, "fs_types", fs_type,
1348 "blocksize", use_bsize, &use_bsize);
1349
1350 if (use_bsize == -1) {
1351 use_bsize = sys_page_size;
1352 if ((linux_version_code < (2*65536 + 6*256)) &&
1353 (use_bsize > 4096))
1354 use_bsize = 4096;
1355 }
1356 if (sector_size && use_bsize < sector_size)
1357 use_bsize = sector_size;
1358 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1359 use_bsize = -blocksize;
1360 blocksize = use_bsize;
1361 fs_param.s_blocks_count /= blocksize / 1024;
1362 }
1363
1364 if (inode_ratio == 0) {
1365 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1366 8192, &inode_ratio);
1367 profile_get_integer(profile, "fs_types", fs_type,
1368 "inode_ratio", inode_ratio,
1369 &inode_ratio);
1370
1371 if (inode_ratio < blocksize)
1372 inode_ratio = blocksize;
1373 }
1374
1375 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1376 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1377
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001378 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001379
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001380 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001381 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001382
1383 /* Since sparse_super is the default, we would only have a problem
1384 * here if it was explicitly disabled.
1385 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001386 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1387 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001388 com_err(program_name, 0,
1389 _("reserved online resize blocks not supported "
1390 "on non-sparse filesystem"));
1391 exit(1);
1392 }
1393
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001394 if (fs_param.s_blocks_per_group) {
1395 if (fs_param.s_blocks_per_group < 256 ||
1396 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001397 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001398 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001399 exit(1);
1400 }
1401 }
1402
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001403 if (!force && fs_param.s_blocks_count >= (1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001404 com_err(program_name, 0,
1405 _("Filesystem too large. No more than 2**31-1 blocks\n"
1406 "\t (8TB using a blocksize of 4k) are currently supported."));
1407 exit(1);
1408 }
1409
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001410 if ((blocksize > 4096) &&
1411 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1412 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1413 "blocksizes greater than 4096\n\tusing ext3. "
1414 "Use -b 4096 if this is an issue for you.\n\n"));
1415
Andreas Dilger932a4892002-05-16 03:20:07 -06001416 if (inode_size) {
1417 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001418 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001419 inode_size & (inode_size - 1)) {
1420 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001421 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001422 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001423 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001424 exit(1);
1425 }
1426 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1427 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1428 "on most systems\n"),
1429 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001430 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001431 }
1432
Theodore Ts'o3839e651997-04-26 13:21:57 +00001433 /*
1434 * Calculate number of inodes based on the inode ratio
1435 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001436 fs_param.s_inodes_count = num_inodes ? num_inodes :
1437 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001438 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439
1440 /*
1441 * Calculate number of blocks to reserve
1442 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001443 fs_param.s_r_blocks_count = (fs_param.s_blocks_count * reserved_ratio)
1444 / 100;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001445}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001446
Theodore Ts'o3839e651997-04-26 13:21:57 +00001447int main (int argc, char *argv[])
1448{
1449 errcode_t retval = 0;
1450 ext2_filsys fs;
1451 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001452 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001453 unsigned int i;
1454 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001455 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001456
1457#ifdef ENABLE_NLS
1458 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001459 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001460 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1461 textdomain(NLS_CAT_NAME);
1462#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001463 PRS(argc, argv);
1464
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001465#ifdef CONFIG_TESTIO_DEBUG
1466 io_ptr = test_io_manager;
1467 test_io_backing_manager = unix_io_manager;
1468#else
1469 io_ptr = unix_io_manager;
1470#endif
1471
Theodore Ts'o3839e651997-04-26 13:21:57 +00001472 /*
1473 * Initialize the superblock....
1474 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001475 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001476 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001477 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001478 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001479 exit(1);
1480 }
1481
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001482 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001483 * Wipe out the old on-disk superblock
1484 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001485 if (!noaction)
1486 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001487
1488 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001489 * Generate a UUID for it...
1490 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001491 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001492
1493 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001494 * Initialize the directory index variables
1495 */
1496 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1497 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1498
1499 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001500 * Add "jitter" to the superblock's check interval so that we
1501 * don't check all the filesystems at the same time. We use a
1502 * kludgy hack of using the UUID to derive a random jitter value.
1503 */
1504 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1505 val += fs->super->s_uuid[i];
1506 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1507
1508 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001509 * Override the creator OS, if applicable
1510 */
1511 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001512 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001513 exit(1);
1514 }
1515
1516 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001517 * For the Hurd, we will turn off filetype since it doesn't
1518 * support it.
1519 */
1520 if (fs->super->s_creator_os == EXT2_OS_HURD)
1521 fs->super->s_feature_incompat &=
1522 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1523
1524 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001525 * Set the volume label...
1526 */
1527 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001528 memset(fs->super->s_volume_name, 0,
1529 sizeof(fs->super->s_volume_name));
1530 strncpy(fs->super->s_volume_name, volume_label,
1531 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001532 }
1533
1534 /*
1535 * Set the last mount directory
1536 */
1537 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001538 memset(fs->super->s_last_mounted, 0,
1539 sizeof(fs->super->s_last_mounted));
1540 strncpy(fs->super->s_last_mounted, mount_dir,
1541 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001542 }
1543
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001544 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001545 show_stats(fs);
1546
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001547 if (noaction)
1548 exit(0);
1549
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001550 if (fs->super->s_feature_incompat &
1551 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1552 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001553 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001554 }
1555
Theodore Ts'o3839e651997-04-26 13:21:57 +00001556 if (bad_blocks_filename)
1557 read_bb_file(fs, &bb_list, bad_blocks_filename);
1558 if (cflag)
1559 test_disk(fs, &bb_list);
1560
1561 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001562 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001563 retval = ext2fs_allocate_tables(fs);
1564 if (retval) {
1565 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001566 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001567 exit(1);
1568 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001569 if (super_only) {
1570 fs->super->s_state |= EXT2_ERROR_FS;
1571 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1572 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001573 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001574 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001575 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001576 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001577 blk_t ret_blk;
1578
1579#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001580 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001581#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001582
1583 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001584 * Wipe out any old MD RAID (or other) metadata at the end
1585 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001586 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001587 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001588 start = (blocks & ~(rsv - 1));
1589 if (start > rsv)
1590 start -= rsv;
1591 if (start > 0)
1592 retval = zero_blocks(fs, start, blocks - start,
1593 NULL, &ret_blk, NULL);
1594
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001595 if (retval) {
1596 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001597 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001598 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001599 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001600 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001601 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001602 create_root_dir(fs);
1603 create_lost_and_found(fs);
1604 reserve_inodes(fs);
1605 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001606 if (fs->super->s_feature_compat &
1607 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1608 retval = ext2fs_create_resize_inode(fs);
1609 if (retval) {
1610 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001611 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001612 exit(1);
1613 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001614 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001615 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001616
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001617 if (journal_device) {
1618 ext2_filsys jfs;
1619
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001620 if (!force)
1621 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001622 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001623
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001624 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1625 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1626 fs->blocksize, unix_io_manager, &jfs);
1627 if (retval) {
1628 com_err(program_name, retval,
1629 _("while trying to open journal device %s\n"),
1630 journal_device);
1631 exit(1);
1632 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001633 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001634 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001635 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001636 fflush(stdout);
1637 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001638 retval = ext2fs_add_journal_device(fs, jfs);
1639 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001640 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001641 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001642 journal_device);
1643 exit(1);
1644 }
1645 if (!quiet)
1646 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001647 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001648 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001649 } else if ((journal_size) ||
1650 (fs_param.s_feature_compat &
1651 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001652 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001653
1654 if (!journal_blocks) {
1655 fs->super->s_feature_compat &=
1656 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1657 goto no_journal;
1658 }
1659 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001660 printf(_("Creating journal (%d blocks): "),
1661 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001662 fflush(stdout);
1663 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001664 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1665 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001666 if (retval) {
1667 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001668 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001669 exit(1);
1670 }
1671 if (!quiet)
1672 printf(_("done\n"));
1673 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001674no_journal:
1675
Theodore Ts'o3839e651997-04-26 13:21:57 +00001676 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001677 printf(_("Writing superblocks and "
1678 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001679 retval = ext2fs_flush(fs);
1680 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001681 fprintf(stderr,
1682 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001683 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001684 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001685 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001686 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1687 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001688 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001689 val = ext2fs_close(fs);
1690 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001691}