blob: f7e48321cac6c89b7e8e1421566e2b74dfc1a078 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000023#ifdef linux
24#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Theodore Ts'o54c637d2001-05-14 11:45:38 +000047#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000048#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000049#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000050#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000052#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000054#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
56#define STRIDE_LENGTH 8
57
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000058#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000059#define ZAP_BOOTBLOCK
60#endif
61
Theodore Ts'o3839e651997-04-26 13:21:57 +000062extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000063extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
65const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000066const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000069int cflag;
70int verbose;
71int quiet;
72int super_only;
73int force;
74int noaction;
75int journal_size;
76int journal_flags;
77char *bad_blocks_filename;
78__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079
80struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000081char *creator_os;
82char *volume_label;
83char *mount_dir;
84char *journal_device;
85int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'o63985322001-01-03 17:02:13 +000087static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000088{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000089 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000090 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000091 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000093 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +000094 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 program_name);
96 exit(1);
97}
98
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000099static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100{
101 int l = 0;
102
103 arg >>= 1;
104 while (arg) {
105 l++;
106 arg >>= 1;
107 }
108 return l;
109}
110
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000111static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000112{
113 int l;
114
115 for (l=0; arg ; l++)
116 arg = arg / 10;
117 return l;
118}
119
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000121 * This function sets the default parameters for a filesystem
122 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000123 * The type is specified by the user. The size is the maximum size
124 * (in megabytes) for which a set of parameters applies, with a size
125 * of zero meaning that it is the default parameter for the type.
126 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000127 */
128static char default_str[] = "default";
129struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000130 const char *type;
131 int size;
132 int blocksize;
133 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000134} settings[] = {
135 { default_str, 0, 4096, 8192 },
136 { default_str, 512, 1024, 4096 },
137 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000138 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000139 { "news", 0, 4096, 4096 },
Theodore Ts'o44c09c02001-01-14 17:02:09 +0000140 { "largefile", 0, 4096, 1024 * 1024 },
141 { "largefile4", 0, 4096, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000142 { 0, 0, 0, 0},
143};
144
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000145static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000146 int blocksize, int *inode_ratio)
147{
148 int megs;
149 int ratio = 0;
150 struct mke2fs_defaults *p;
151
Theodore Ts'o9094f281999-10-26 16:42:50 +0000152 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000153 1024);
154 if (inode_ratio)
155 ratio = *inode_ratio;
156 if (!fs_type)
157 fs_type = default_str;
158 for (p = settings; p->type; p++) {
159 if ((strcmp(p->type, fs_type) != 0) &&
160 (strcmp(p->type, default_str) != 0))
161 continue;
162 if ((p->size != 0) &&
163 (megs > p->size))
164 continue;
165 if (ratio == 0)
166 *inode_ratio = p->inode_ratio;
167 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000168 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000169 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000170 }
171 }
172 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000173 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000174}
175
176/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 * Helper function for read_bb_file and test_disk
178 */
179static void invalid_block(ext2_filsys fs, blk_t blk)
180{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000181 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 return;
183}
184
185/*
186 * Reads the bad blocks list from a file
187 */
188static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
189 const char *bad_blocks_file)
190{
191 FILE *f;
192 errcode_t retval;
193
194 f = fopen(bad_blocks_file, "r");
195 if (!f) {
196 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000197 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 exit(1);
199 }
200 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
201 fclose (f);
202 if (retval) {
203 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000204 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 exit(1);
206 }
207}
208
209/*
210 * Runs the badblocks program to test the disk
211 */
212static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
213{
214 FILE *f;
215 errcode_t retval;
216 char buf[1024];
217
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000218 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
219 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 fs->super->s_blocks_count);
221 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000222 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 f = popen(buf, "r");
224 if (!f) {
225 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000226 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227 exit(1);
228 }
229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000230 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000231 if (retval) {
232 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000233 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234 exit(1);
235 }
236}
237
238static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
239{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000240 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 int must_be_good;
242 blk_t blk;
243 badblocks_iterate bb_iter;
244 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000245 blk_t group_block;
246 int group;
247 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248
249 if (!bb_list)
250 return;
251
252 /*
253 * The primary superblock and group descriptors *must* be
254 * good; if not, abort.
255 */
256 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
257 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000258 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000259 fprintf(stderr, _("Block %d in primary "
260 "superblock/group descriptor area bad.\n"), i);
261 fprintf(stderr, _("Blocks %d through %d must be good "
262 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000264 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 exit(1);
266 }
267 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000268
269 /*
270 * See if any of the bad blocks are showing up in the backup
271 * superblocks and/or group descriptors. If so, issue a
272 * warning and adjust the block counts appropriately.
273 */
274 group_block = fs->super->s_first_data_block +
275 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000276
277 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000278 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000279 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000280 if (ext2fs_badblocks_list_test(bb_list,
281 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000282 if (!group_bad)
283 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000284_("Warning: the backup superblock/group descriptors at block %d contain\n"
285" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286 group_block);
287 group_bad++;
288 group = ext2fs_group_of_blk(fs, group_block+j);
289 fs->group_desc[group].bg_free_blocks_count++;
290 fs->super->s_free_blocks_count++;
291 }
292 }
293 group_block += fs->super->s_blocks_per_group;
294 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295
296 /*
297 * Mark all the bad blocks as used...
298 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000299 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000301 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000302 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000303 exit(1);
304 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000305 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000306 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000307 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308}
309
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000310/*
311 * These functions implement a generalized progress meter.
312 */
313struct progress_struct {
314 char format[20];
315 char backup[80];
316 __u32 max;
317};
318
319static void progress_init(struct progress_struct *progress,
320 char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000322 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000323
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000324 memset(progress, 0, sizeof(struct progress_struct));
325 if (quiet)
326 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000327
328 /*
329 * Figure out how many digits we need
330 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000331 i = int_log10(max);
332 sprintf(progress->format, "%%%dd/%%%dld", i, i);
333 memset(progress->backup, '\b', sizeof(progress->backup)-1);
334 progress->backup[sizeof(progress->backup)-1] = 0;
335 if ((2*i)+1 < sizeof(progress->backup))
336 progress->backup[(2*i)+1] = 0;
337 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000338
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000339 fputs(label, stdout);
340 fflush(stdout);
341}
342
343static void progress_update(struct progress_struct *progress, __u32 val)
344{
345 if (progress->format[0] == 0)
346 return;
347 printf(progress->format, val, progress->max);
348 fputs(progress->backup, stdout);
349}
350
351static void progress_close(struct progress_struct *progress)
352{
353 if (progress->format[0] == 0)
354 return;
355 fputs(_("done \n"), stdout);
356}
357
358
359/*
360 * Helper function which zeros out _num_ blocks starting at _blk_. In
361 * case of an error, the details of the error is returned via _ret_blk_
362 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
363 * success, and an error code on an error.
364 *
365 * As a special case, if the first argument is NULL, then it will
366 * attempt to free the static zeroizing buffer. (This is to keep
367 * programs that check for memory leaks happy.)
368 */
369static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
370 struct progress_struct *progress,
371 blk_t *ret_blk, int *ret_count)
372{
373 int j, count, next_update, next_update_incr;
374 static char *buf;
375 errcode_t retval;
376
377 /* If fs is null, clean up the static buffer and return */
378 if (!fs) {
379 if (buf) {
380 free(buf);
381 buf = 0;
382 }
383 return 0;
384 }
385 /* Allocate the zeroizing buffer if necessary */
386 if (!buf) {
387 buf = malloc(fs->blocksize * STRIDE_LENGTH);
388 if (!buf) {
389 com_err("malloc", ENOMEM,
390 _("while allocating zeroizing buffer"));
391 exit(1);
392 }
393 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
394 }
395 /* OK, do the write loop */
396 next_update = 0;
397 next_update_incr = num / 100;
398 if (next_update_incr < 1)
399 next_update_incr = 1;
400 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
401 if (num-j > STRIDE_LENGTH)
402 count = STRIDE_LENGTH;
403 else
404 count = num - j;
405 retval = io_channel_write_blk(fs->io, blk, count, buf);
406 if (retval) {
407 if (ret_count)
408 *ret_count = count;
409 if (ret_blk)
410 *ret_blk = blk;
411 return retval;
412 }
413 if (progress && j > next_update) {
414 next_update += num / 100;
415 progress_update(progress, blk);
416 }
417 }
418 return 0;
419}
420
421static void write_inode_tables(ext2_filsys fs)
422{
423 errcode_t retval;
424 blk_t blk;
425 int i, num;
426 struct progress_struct progress;
427
428 if (quiet)
429 memset(&progress, 0, sizeof(progress));
430 else
431 progress_init(&progress, _("Writing inode tables: "),
432 fs->group_desc_count);
433
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000435 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000437 blk = fs->group_desc[i].bg_inode_table;
438 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000439
440 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
441 if (retval) {
442 printf(_("\nCould not write %d blocks "
443 "in inode table starting at %d: %s\n"),
444 num, blk, error_message(retval));
445 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000446 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000447 if (sync_kludge) {
448 if (sync_kludge == 1)
449 sync();
450 else if ((i % sync_kludge) == 0)
451 sync();
452 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000454 zero_blocks(0, 0, 0, 0, 0, 0);
455 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456}
457
458static void create_root_dir(ext2_filsys fs)
459{
460 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000461 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462
463 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
464 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000465 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 exit(1);
467 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000468 if (geteuid()) {
469 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
470 if (retval) {
471 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000472 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 exit(1);
474 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000475 inode.i_uid = getuid();
476 if (inode.i_uid)
477 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000478 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
479 if (retval) {
480 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000481 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 exit(1);
483 }
484 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485}
486
487static void create_lost_and_found(ext2_filsys fs)
488{
489 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000490 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 const char *name = "lost+found";
492 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000493 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494
495 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
496 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000497 com_err("ext2fs_mkdir", retval,
498 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 exit(1);
500 }
501
502 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
503 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000504 com_err("ext2_lookup", retval,
505 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 exit(1);
507 }
508
509 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000510 if ((lpf_size += fs->blocksize) >= 16*1024)
511 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 retval = ext2fs_expand_dir(fs, ino);
513 if (retval) {
514 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000515 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 exit(1);
517 }
518 }
519}
520
521static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
522{
523 errcode_t retval;
524
Theodore Ts'of3db3561997-04-26 13:34:30 +0000525 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 fs->group_desc[0].bg_free_inodes_count--;
527 fs->super->s_free_inodes_count--;
528 retval = ext2fs_update_bb_inode(fs, bb_list);
529 if (retval) {
530 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000531 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 exit(1);
533 }
534
535}
536
537static void reserve_inodes(ext2_filsys fs)
538{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000539 ext2_ino_t i;
540 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000542 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000543 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 group = ext2fs_group_of_ino(fs, i);
545 fs->group_desc[group].bg_free_inodes_count--;
546 fs->super->s_free_inodes_count--;
547 }
548 ext2fs_mark_ib_dirty(fs);
549}
550
Theodore Ts'o04a96852001-08-30 21:55:26 -0400551static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000552{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400553 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000554 int retval;
555
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400556 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600557 if (!buf) {
558 printf(_("Warning: out of memory erasing sectors %d-%d: %s\n"),
559 sect, sect + nsect - 1, error_message(retval));
560 exit(1);
561 }
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400562 memset(buf, 0, 512*nsect);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000564 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400565 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000566 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400567 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000569 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000570 error_message(retval));
571}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000572
573static void create_journal_dev(ext2_filsys fs)
574{
575 struct progress_struct progress;
576 errcode_t retval;
577 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000578 blk_t blk;
579 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000580
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000581 retval = ext2fs_create_journal_superblock(fs,
582 fs->super->s_blocks_count, 0, &buf);
583 if (retval) {
584 com_err("create_journal_dev", retval,
585 _("while initializing journal superblock"));
586 exit(1);
587 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000588 if (quiet)
589 memset(&progress, 0, sizeof(progress));
590 else
591 progress_init(&progress, _("Zeroing journal device: "),
592 fs->super->s_blocks_count);
593
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000594 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
595 &progress, &blk, &count);
596 if (retval) {
597 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000598 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000599 blk, count);
600 exit(1);
601 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000602 zero_blocks(0, 0, 0, 0, 0, 0);
603
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000604 retval = io_channel_write_blk(fs->io,
605 fs->super->s_first_data_block+1,
606 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000607 if (retval) {
608 com_err("create_journal_dev", retval,
609 _("while writing journal superblock"));
610 exit(1);
611 }
612 progress_close(&progress);
613}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000614
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615static void show_stats(ext2_filsys fs)
616{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000617 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000618 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000620 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621
622 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000623 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000625
626 memset(buf, 0, sizeof(buf));
627 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000628 printf(_("Filesystem label=%s\n"), buf);
629 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000630 switch (fs->super->s_creator_os) {
631 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000632 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000633 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000634 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000635 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000636 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000637 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000638 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000639 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000640 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000641 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000643 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644 s->s_r_blocks_count,
645 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000646 printf(_("First data block=%u\n"), s->s_first_data_block);
647 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000648 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000649 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000650 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000651 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000653 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654
655 if (fs->group_desc_count == 1) {
656 printf("\n");
657 return;
658 }
659
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000660 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 group_block = s->s_first_data_block;
662 col_left = 0;
663 for (i = 1; i < fs->group_desc_count; i++) {
664 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000665 if (!ext2fs_bg_has_super(fs, i))
666 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000667 if (i != 1)
668 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000669 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000670 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000672 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000674 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000675 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676 }
677 printf("\n\n");
678}
679
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000680/*
681 * Set the S_CREATOR_OS field. Return true if OS is known,
682 * otherwise, 0.
683 */
684static int set_os(struct ext2_super_block *sb, char *os)
685{
686 if (isdigit (*os))
687 sb->s_creator_os = atoi (os);
688 else if (strcasecmp(os, "linux") == 0)
689 sb->s_creator_os = EXT2_OS_LINUX;
690 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
691 sb->s_creator_os = EXT2_OS_HURD;
692 else if (strcasecmp(os, "masix") == 0)
693 sb->s_creator_os = EXT2_OS_MASIX;
694 else
695 return 0;
696 return 1;
697}
698
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000699#define PATH_SET "PATH=/sbin"
700
Theodore Ts'od163b091997-10-03 17:42:28 +0000701static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000702{
703 char *buf, *token, *next, *p, *arg;
704 int len;
705 int raid_usage = 0;
706
707 len = strlen(opts);
708 buf = malloc(len+1);
709 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000710 fprintf(stderr, _("Couldn't allocate memory to parse "
711 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000712 exit(1);
713 }
714 strcpy(buf, opts);
715 for (token = buf; token && *token; token = next) {
716 p = strchr(token, ',');
717 next = 0;
718 if (p) {
719 *p = 0;
720 next = p+1;
721 }
722 arg = strchr(token, '=');
723 if (arg) {
724 *arg = 0;
725 arg++;
726 }
727 if (strcmp(token, "stride") == 0) {
728 if (!arg) {
729 raid_usage++;
730 continue;
731 }
732 fs_stride = strtoul(arg, &p, 0);
733 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000734 fprintf(stderr,
735 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000736 raid_usage++;
737 continue;
738 }
739 } else
740 raid_usage++;
741 }
742 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000743 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000744 "Raid options are separated by commas, "
745 "and may take an argument which\n"
746 "\tis set off by an equals ('=') sign.\n\n"
747 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000748 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000749 exit(1);
750 }
751}
752
Theodore Ts'o896938d1999-10-23 01:04:50 +0000753static __u32 ok_features[3] = {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000754 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000755 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
756 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000757 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
758};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000759
760
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761static void PRS(int argc, char *argv[])
762{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000763 int c;
764 int size;
765 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000766 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000767 int blocksize = 0;
768 int inode_ratio = 0;
769 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000770 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000771 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000772 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000773 char * raid_opts = 0;
774 char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400775 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000776 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000777#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000778 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000779#endif
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000780
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000782 if (oldpath) {
783 char *newpath;
784
785 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
786 strcpy (newpath, PATH_SET);
787 strcat (newpath, ":");
788 strcat (newpath, oldpath);
789 putenv (newpath);
790 } else
791 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000792
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000793 tmp = getenv("MKE2FS_SYNC");
794 if (tmp)
795 sync_kludge = atoi(tmp);
796
Theodore Ts'o3839e651997-04-26 13:21:57 +0000797 setbuf(stdout, NULL);
798 setbuf(stderr, NULL);
799 initialize_ext2_error_table();
800 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000801 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400802 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
803 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
804
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000805#ifdef linux
806 if (uname(&ut)) {
807 perror("uname");
808 exit(1);
809 }
810 if ((ut.release[0] == '1') ||
811 (ut.release[0] == '2' && ut.release[1] == '.' &&
812 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000813 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400814 param.s_feature_incompat = 0;
815 param.s_feature_compat = 0;
816 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000817 }
818#endif
Andreas Dilger9d8e6342001-08-31 10:24:46 -0600819 fprintf (stderr, "mke2fs %s (%s)\n",
Theodore Ts'o04a96852001-08-30 21:55:26 -0400820 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821 if (argc && *argv)
822 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000823 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000824 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825 switch (c) {
826 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000827 blocksize = strtoul(optarg, &tmp, 0);
828 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000829 com_err(program_name, 0,
830 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000831 exit(1);
832 }
833 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000834 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000835 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000836 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000837 case 'c': /* Check for bad blocks */
838 case 't': /* deprecated */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000839 cflag = 1;
840 break;
841 case 'f':
842 size = strtoul(optarg, &tmp, 0);
843 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000844 com_err(program_name, 0,
845 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000846 optarg);
847 exit(1);
848 }
849 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000850 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000851 printf(_("Warning: fragments not supported. "
852 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000853 break;
854 case 'g':
855 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000856 if (*tmp) {
857 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000858 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000859 exit(1);
860 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000861 if ((param.s_blocks_per_group % 8) != 0) {
862 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000863 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000864 exit(1);
865 }
866 break;
867 case 'i':
868 inode_ratio = strtoul(optarg, &tmp, 0);
Theodore Ts'o44c09c02001-01-14 17:02:09 +0000869 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000870 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000871 com_err(program_name, 0,
872 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000873 exit(1);
874 }
875 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000876 case 'J':
877 parse_journal_opts(optarg);
878 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000879 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000880 param.s_feature_compat |=
881 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000882 if (!journal_size)
883 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000884 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000885 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000886 bad_blocks_filename = malloc(strlen(optarg)+1);
887 if (!bad_blocks_filename) {
888 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000889 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000890 exit(1);
891 }
892 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000893 break;
894 case 'm':
895 reserved_ratio = strtoul(optarg, &tmp, 0);
896 if (reserved_ratio > 50 || *tmp) {
897 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000898 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 optarg);
900 exit(1);
901 }
902 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000903 case 'n':
904 noaction++;
905 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000906 case 'o':
907 creator_os = optarg;
908 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000909 case 'r':
910 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000911 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400912 param.s_feature_incompat = 0;
913 param.s_feature_compat = 0;
914 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000915 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000916 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000917 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400918 if (atoi(optarg))
919 param.s_feature_ro_compat |=
920 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
921 else
922 param.s_feature_ro_compat &=
923 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000924 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000925#ifdef EXT2_DYNAMIC_REV
926 case 'I':
927 param.s_inode_size = atoi(optarg);
928 break;
929#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000930 case 'N':
931 num_inodes = atoi(optarg);
932 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933 case 'v':
934 verbose = 1;
935 break;
936 case 'q':
937 quiet = 1;
938 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000939 case 'F':
940 force = 1;
941 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000942 case 'L':
943 volume_label = optarg;
944 break;
945 case 'M':
946 mount_dir = optarg;
947 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000948 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400949 if (!strcmp(optarg, "none") || default_features) {
950 param.s_feature_compat = 0;
951 param.s_feature_incompat = 0;
952 param.s_feature_ro_compat = 0;
953 default_features = 0;
954 }
955 if (!strcmp(optarg, "none"))
956 break;
957 if (e2p_edit_feature(optarg,
958 &param.s_feature_compat,
959 ok_features)) {
960 fprintf(stderr,
961 _("Invalid filesystem option set: %s\n"), optarg);
962 exit(1);
963 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000964 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000965 case 'R':
966 raid_opts = optarg;
967 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000968 case 'S':
969 super_only = 1;
970 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000971 case 'T':
972 fs_type = optarg;
973 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000974 case 'V':
975 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000976 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000977 error_message(EXT2_ET_BASE));
978 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000979 default:
980 usage();
981 }
982 if (optind == argc)
983 usage();
984 device_name = argv[optind];
985 optind++;
986 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000987 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
988
989 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000990 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000991 argv[optind - 1]);
992 exit(1);
993 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000994 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000995 }
996 if (optind < argc)
997 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000998
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000999 if (raid_opts)
1000 parse_raid_opts(raid_opts);
1001
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001002 /*
1003 * If there's no blocksize specified and there is a journal
1004 * device, use it to figure out the blocksize
1005 */
1006 if (blocksize == 0 && journal_device) {
1007 ext2_filsys jfs;
1008
1009 retval = ext2fs_open(journal_device,
1010 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1011 0, unix_io_manager, &jfs);
1012 if (retval) {
1013 com_err(program_name, retval,
1014 _("while trying to open journal device %s\n"),
1015 journal_device);
1016 exit(1);
1017 }
1018 blocksize = jfs->blocksize;
1019 param.s_log_block_size =
1020 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1021 ext2fs_close(jfs);
1022 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001023
Andreas Dilgerfa724582001-08-04 01:18:34 -06001024 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1025 if (!fs_type)
1026 fs_type = "journal";
1027 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001028 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1029 param.s_feature_compat = 0;
1030 param.s_feature_ro_compat = 0;
1031 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001032 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1033 (param.s_feature_compat || param.s_feature_ro_compat ||
1034 param.s_feature_incompat))
1035 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1036
Theodore Ts'o74becf31997-04-26 14:37:06 +00001037 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001038 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001039 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001040
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 param.s_log_frag_size = param.s_log_block_size;
1042
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001043 if (noaction && param.s_blocks_count) {
1044 dev_size = param.s_blocks_count;
1045 retval = 0;
1046 } else
1047 retval = ext2fs_get_device_size(device_name,
1048 EXT2_BLOCK_SIZE(&param),
1049 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001050 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1051 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001052 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001053 exit(1);
1054 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001055 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001056 if (retval == EXT2_ET_UNIMPLEMENTED) {
1057 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001058 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001059 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001060 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001061 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001062 } else {
1063 if (dev_size == 0) {
1064 com_err(program_name, 0,
1065 _("Device size reported to be zero. "
1066 "Invalid partition specified, or\n\t"
1067 "partition table wasn't reread "
1068 "after running fdisk, due to\n\t"
1069 "a modified partition being busy "
1070 "and in use. You may need to reboot\n\t"
1071 "to re-read your partition table.\n"
1072 ));
1073 exit(1);
1074 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001075 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001076 }
1077
Theodore Ts'oa789d841998-03-30 01:20:55 +00001078 } else if (!force && (param.s_blocks_count > dev_size)) {
1079 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001080 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001081 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001082 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001083
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001084 /*
1085 * If the user asked for HAS_JOURNAL, then make sure a journal
1086 * gets created.
1087 */
1088 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1089 !journal_size)
1090 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001091
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001092 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001093
Theodore Ts'o521e3681997-04-29 17:48:10 +00001094 if (param.s_blocks_per_group) {
1095 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001096 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001097 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001098 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001099 exit(1);
1100 }
1101 }
1102
Theodore Ts'o3839e651997-04-26 13:21:57 +00001103 /*
1104 * Calculate number of inodes based on the inode ratio
1105 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001106 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001107 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001108 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001109
1110 /*
1111 * Calculate number of blocks to reserve
1112 */
1113 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001114
Theodore Ts'o3839e651997-04-26 13:21:57 +00001115}
1116
1117int main (int argc, char *argv[])
1118{
1119 errcode_t retval = 0;
1120 ext2_filsys fs;
1121 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001122 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001123 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001124
1125#ifdef ENABLE_NLS
1126 setlocale(LC_MESSAGES, "");
1127 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1128 textdomain(NLS_CAT_NAME);
1129#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001130 PRS(argc, argv);
1131
Theodore Ts'o3839e651997-04-26 13:21:57 +00001132 /*
1133 * Initialize the superblock....
1134 */
1135 retval = ext2fs_initialize(device_name, 0, &param,
1136 unix_io_manager, &fs);
1137 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001138 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001139 exit(1);
1140 }
1141
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001142 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001143 * Wipe out the old on-disk superblock
1144 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001145 if (!noaction)
1146 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001147
1148 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001149 * Generate a UUID for it...
1150 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001151 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001152
1153 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001154 * Add "jitter" to the superblock's check interval so that we
1155 * don't check all the filesystems at the same time. We use a
1156 * kludgy hack of using the UUID to derive a random jitter value.
1157 */
1158 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1159 val += fs->super->s_uuid[i];
1160 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1161
1162 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001163 * Override the creator OS, if applicable
1164 */
1165 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001166 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001167 exit(1);
1168 }
1169
1170 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001171 * For the Hurd, we will turn off filetype since it doesn't
1172 * support it.
1173 */
1174 if (fs->super->s_creator_os == EXT2_OS_HURD)
1175 fs->super->s_feature_incompat &=
1176 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1177
1178 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001179 * Set the volume label...
1180 */
1181 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001182 memset(fs->super->s_volume_name, 0,
1183 sizeof(fs->super->s_volume_name));
1184 strncpy(fs->super->s_volume_name, volume_label,
1185 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001186 }
1187
1188 /*
1189 * Set the last mount directory
1190 */
1191 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001192 memset(fs->super->s_last_mounted, 0,
1193 sizeof(fs->super->s_last_mounted));
1194 strncpy(fs->super->s_last_mounted, mount_dir,
1195 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001196 }
1197
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001198 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001199 show_stats(fs);
1200
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001201 if (noaction)
1202 exit(0);
1203
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001204 if (fs->super->s_feature_incompat &
1205 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1206 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001207 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001208 }
1209
Theodore Ts'o3839e651997-04-26 13:21:57 +00001210 if (bad_blocks_filename)
1211 read_bb_file(fs, &bb_list, bad_blocks_filename);
1212 if (cflag)
1213 test_disk(fs, &bb_list);
1214
1215 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001216 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001217 retval = ext2fs_allocate_tables(fs);
1218 if (retval) {
1219 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001220 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001221 exit(1);
1222 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001223 if (super_only) {
1224 fs->super->s_state |= EXT2_ERROR_FS;
1225 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1226 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001227 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001228 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001229 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001230 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001231 blk_t ret_blk;
1232
1233#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001234 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001235#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001236
1237 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001238 * Wipe out any old MD RAID (or other) metadata at the end
1239 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001240 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001241 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001242 start = (blocks & ~(rsv - 1));
1243 if (start > rsv)
1244 start -= rsv;
1245 if (start > 0)
1246 retval = zero_blocks(fs, start, blocks - start,
1247 NULL, &ret_blk, NULL);
1248
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001249 if (retval) {
1250 com_err(program_name, retval,
1251 _("zeroing block %u at end of filesystem"),
1252 ret_blk);
1253 exit(1);
1254 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001255 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001256 create_root_dir(fs);
1257 create_lost_and_found(fs);
1258 reserve_inodes(fs);
1259 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001260 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001261
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001262 if (journal_device) {
1263 ext2_filsys jfs;
1264
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001265 if (!force)
1266 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001267 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001268
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001269 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1270 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1271 fs->blocksize, unix_io_manager, &jfs);
1272 if (retval) {
1273 com_err(program_name, retval,
1274 _("while trying to open journal device %s\n"),
1275 journal_device);
1276 exit(1);
1277 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001278 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001279 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001280 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001281 fflush(stdout);
1282 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001283 retval = ext2fs_add_journal_device(fs, jfs);
1284 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001285 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001286 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001287 journal_device);
1288 exit(1);
1289 }
1290 if (!quiet)
1291 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001292 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001293 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001294 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001295 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001296
1297 if (!journal_blocks) {
1298 fs->super->s_feature_compat &=
1299 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1300 goto no_journal;
1301 }
1302 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001303 printf(_("Creating journal (%d blocks): "),
1304 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001305 fflush(stdout);
1306 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001307 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1308 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001309 if (retval) {
1310 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001311 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001312 exit(1);
1313 }
1314 if (!quiet)
1315 printf(_("done\n"));
1316 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001317no_journal:
1318
Theodore Ts'o3839e651997-04-26 13:21:57 +00001319 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001320 printf(_("Writing superblocks and "
1321 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001322 retval = ext2fs_flush(fs);
1323 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001324 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001325 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001326 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001327 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001328 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001329 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001330 val = ext2fs_close(fs);
1331 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001332}