blob: c55aab0ea96b08cef9e129bb021131fd5401d148 [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'o4ea7bd02001-12-16 23:23:37 -0500145static void set_fs_defaults(const char *fs_type,
146 struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000147 int blocksize, int *inode_ratio)
148{
149 int megs;
150 int ratio = 0;
151 struct mke2fs_defaults *p;
152
Theodore Ts'o9094f281999-10-26 16:42:50 +0000153 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000154 1024);
155 if (inode_ratio)
156 ratio = *inode_ratio;
157 if (!fs_type)
158 fs_type = default_str;
159 for (p = settings; p->type; p++) {
160 if ((strcmp(p->type, fs_type) != 0) &&
161 (strcmp(p->type, default_str) != 0))
162 continue;
163 if ((p->size != 0) &&
164 (megs > p->size))
165 continue;
166 if (ratio == 0)
167 *inode_ratio = p->inode_ratio;
168 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000169 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000170 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000171 }
172 }
173 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000174 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000175}
176
177/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 * Helper function for read_bb_file and test_disk
179 */
180static void invalid_block(ext2_filsys fs, blk_t blk)
181{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000182 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 return;
184}
185
186/*
187 * Reads the bad blocks list from a file
188 */
189static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
190 const char *bad_blocks_file)
191{
192 FILE *f;
193 errcode_t retval;
194
195 f = fopen(bad_blocks_file, "r");
196 if (!f) {
197 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000198 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 exit(1);
200 }
201 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
202 fclose (f);
203 if (retval) {
204 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000205 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 exit(1);
207 }
208}
209
210/*
211 * Runs the badblocks program to test the disk
212 */
213static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
214{
215 FILE *f;
216 errcode_t retval;
217 char buf[1024];
218
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500219 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
220 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
221 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000223 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224 f = popen(buf, "r");
225 if (!f) {
226 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000227 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 exit(1);
229 }
230 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000231 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 if (retval) {
233 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000234 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000235 exit(1);
236 }
237}
238
239static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
240{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000241 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242 int must_be_good;
243 blk_t blk;
244 badblocks_iterate bb_iter;
245 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000246 blk_t group_block;
247 int group;
248 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249
250 if (!bb_list)
251 return;
252
253 /*
254 * The primary superblock and group descriptors *must* be
255 * good; if not, abort.
256 */
257 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
258 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000259 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000260 fprintf(stderr, _("Block %d in primary "
261 "superblock/group descriptor area bad.\n"), i);
262 fprintf(stderr, _("Blocks %d through %d must be good "
263 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000265 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 exit(1);
267 }
268 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000269
270 /*
271 * See if any of the bad blocks are showing up in the backup
272 * superblocks and/or group descriptors. If so, issue a
273 * warning and adjust the block counts appropriately.
274 */
275 group_block = fs->super->s_first_data_block +
276 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000277
278 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000279 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 if (ext2fs_badblocks_list_test(bb_list,
282 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000283 if (!group_bad)
284 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000285_("Warning: the backup superblock/group descriptors at block %d contain\n"
286" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000287 group_block);
288 group_bad++;
289 group = ext2fs_group_of_blk(fs, group_block+j);
290 fs->group_desc[group].bg_free_blocks_count++;
291 fs->super->s_free_blocks_count++;
292 }
293 }
294 group_block += fs->super->s_blocks_per_group;
295 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296
297 /*
298 * Mark all the bad blocks as used...
299 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000300 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000302 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000303 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304 exit(1);
305 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000306 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000307 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000308 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309}
310
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000311/*
312 * These functions implement a generalized progress meter.
313 */
314struct progress_struct {
315 char format[20];
316 char backup[80];
317 __u32 max;
318};
319
320static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500321 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000323 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000324
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000325 memset(progress, 0, sizeof(struct progress_struct));
326 if (quiet)
327 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000328
329 /*
330 * Figure out how many digits we need
331 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000332 i = int_log10(max);
333 sprintf(progress->format, "%%%dd/%%%dld", i, i);
334 memset(progress->backup, '\b', sizeof(progress->backup)-1);
335 progress->backup[sizeof(progress->backup)-1] = 0;
336 if ((2*i)+1 < sizeof(progress->backup))
337 progress->backup[(2*i)+1] = 0;
338 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000339
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000340 fputs(label, stdout);
341 fflush(stdout);
342}
343
344static void progress_update(struct progress_struct *progress, __u32 val)
345{
346 if (progress->format[0] == 0)
347 return;
348 printf(progress->format, val, progress->max);
349 fputs(progress->backup, stdout);
350}
351
352static void progress_close(struct progress_struct *progress)
353{
354 if (progress->format[0] == 0)
355 return;
356 fputs(_("done \n"), stdout);
357}
358
359
360/*
361 * Helper function which zeros out _num_ blocks starting at _blk_. In
362 * case of an error, the details of the error is returned via _ret_blk_
363 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
364 * success, and an error code on an error.
365 *
366 * As a special case, if the first argument is NULL, then it will
367 * attempt to free the static zeroizing buffer. (This is to keep
368 * programs that check for memory leaks happy.)
369 */
370static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
371 struct progress_struct *progress,
372 blk_t *ret_blk, int *ret_count)
373{
374 int j, count, next_update, next_update_incr;
375 static char *buf;
376 errcode_t retval;
377
378 /* If fs is null, clean up the static buffer and return */
379 if (!fs) {
380 if (buf) {
381 free(buf);
382 buf = 0;
383 }
384 return 0;
385 }
386 /* Allocate the zeroizing buffer if necessary */
387 if (!buf) {
388 buf = malloc(fs->blocksize * STRIDE_LENGTH);
389 if (!buf) {
390 com_err("malloc", ENOMEM,
391 _("while allocating zeroizing buffer"));
392 exit(1);
393 }
394 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
395 }
396 /* OK, do the write loop */
397 next_update = 0;
398 next_update_incr = num / 100;
399 if (next_update_incr < 1)
400 next_update_incr = 1;
401 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
402 if (num-j > STRIDE_LENGTH)
403 count = STRIDE_LENGTH;
404 else
405 count = num - j;
406 retval = io_channel_write_blk(fs->io, blk, count, buf);
407 if (retval) {
408 if (ret_count)
409 *ret_count = count;
410 if (ret_blk)
411 *ret_blk = blk;
412 return retval;
413 }
414 if (progress && j > next_update) {
415 next_update += num / 100;
416 progress_update(progress, blk);
417 }
418 }
419 return 0;
420}
421
422static void write_inode_tables(ext2_filsys fs)
423{
424 errcode_t retval;
425 blk_t blk;
426 int i, num;
427 struct progress_struct progress;
428
429 if (quiet)
430 memset(&progress, 0, sizeof(progress));
431 else
432 progress_init(&progress, _("Writing inode tables: "),
433 fs->group_desc_count);
434
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000436 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000438 blk = fs->group_desc[i].bg_inode_table;
439 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000440
441 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
442 if (retval) {
443 printf(_("\nCould not write %d blocks "
444 "in inode table starting at %d: %s\n"),
445 num, blk, error_message(retval));
446 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000447 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000448 if (sync_kludge) {
449 if (sync_kludge == 1)
450 sync();
451 else if ((i % sync_kludge) == 0)
452 sync();
453 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000455 zero_blocks(0, 0, 0, 0, 0, 0);
456 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457}
458
459static void create_root_dir(ext2_filsys fs)
460{
461 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463
464 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
465 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000466 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 exit(1);
468 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 if (geteuid()) {
470 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
471 if (retval) {
472 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000473 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000474 exit(1);
475 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000476 inode.i_uid = getuid();
477 if (inode.i_uid)
478 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
480 if (retval) {
481 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000482 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000483 exit(1);
484 }
485 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486}
487
488static void create_lost_and_found(ext2_filsys fs)
489{
490 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000491 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 const char *name = "lost+found";
493 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000494 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495
Theodore Ts'o6a525062001-12-24 09:40:00 -0500496 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
498 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000499 com_err("ext2fs_mkdir", retval,
500 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 exit(1);
502 }
503
504 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
505 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000506 com_err("ext2_lookup", retval,
507 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 exit(1);
509 }
510
511 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000512 if ((lpf_size += fs->blocksize) >= 16*1024)
513 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 retval = ext2fs_expand_dir(fs, ino);
515 if (retval) {
516 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000517 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 exit(1);
519 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500520 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521}
522
523static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
524{
525 errcode_t retval;
526
Theodore Ts'of3db3561997-04-26 13:34:30 +0000527 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 fs->group_desc[0].bg_free_inodes_count--;
529 fs->super->s_free_inodes_count--;
530 retval = ext2fs_update_bb_inode(fs, bb_list);
531 if (retval) {
532 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000533 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 exit(1);
535 }
536
537}
538
539static void reserve_inodes(ext2_filsys fs)
540{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000541 ext2_ino_t i;
542 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000544 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 group = ext2fs_group_of_ino(fs, i);
547 fs->group_desc[group].bg_free_inodes_count--;
548 fs->super->s_free_inodes_count--;
549 }
550 ext2fs_mark_ib_dirty(fs);
551}
552
Theodore Ts'o04a96852001-08-30 21:55:26 -0400553static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000554{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400555 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000556 int retval;
557
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400558 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600559 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500560 printf(_("Out of memory erasing sectors %d-%d\n"),
561 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600562 exit(1);
563 }
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400564 memset(buf, 0, 512*nsect);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000566 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400567 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000568 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400569 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000570 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000571 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000572 error_message(retval));
573}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000574
575static void create_journal_dev(ext2_filsys fs)
576{
577 struct progress_struct progress;
578 errcode_t retval;
579 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000580 blk_t blk;
581 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000582
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000583 retval = ext2fs_create_journal_superblock(fs,
584 fs->super->s_blocks_count, 0, &buf);
585 if (retval) {
586 com_err("create_journal_dev", retval,
587 _("while initializing journal superblock"));
588 exit(1);
589 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000590 if (quiet)
591 memset(&progress, 0, sizeof(progress));
592 else
593 progress_init(&progress, _("Zeroing journal device: "),
594 fs->super->s_blocks_count);
595
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000596 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
597 &progress, &blk, &count);
598 if (retval) {
599 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000600 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000601 blk, count);
602 exit(1);
603 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000604 zero_blocks(0, 0, 0, 0, 0, 0);
605
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000606 retval = io_channel_write_blk(fs->io,
607 fs->super->s_first_data_block+1,
608 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000609 if (retval) {
610 com_err("create_journal_dev", retval,
611 _("while writing journal superblock"));
612 exit(1);
613 }
614 progress_close(&progress);
615}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000616
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617static void show_stats(ext2_filsys fs)
618{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000619 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000620 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000622 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623
624 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000625 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000627
628 memset(buf, 0, sizeof(buf));
629 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000630 printf(_("Filesystem label=%s\n"), buf);
631 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000632 switch (fs->super->s_creator_os) {
633 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000634 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000635 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000636 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000637 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000638 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000639 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000640 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000641 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000642 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000643 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000645 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 s->s_r_blocks_count,
647 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000648 printf(_("First data block=%u\n"), s->s_first_data_block);
649 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000650 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000651 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000652 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000653 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000655 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656
657 if (fs->group_desc_count == 1) {
658 printf("\n");
659 return;
660 }
661
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000662 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 group_block = s->s_first_data_block;
664 col_left = 0;
665 for (i = 1; i < fs->group_desc_count; i++) {
666 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000667 if (!ext2fs_bg_has_super(fs, i))
668 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000669 if (i != 1)
670 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000671 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000672 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000674 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000675 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000676 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000677 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678 }
679 printf("\n\n");
680}
681
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000682/*
683 * Set the S_CREATOR_OS field. Return true if OS is known,
684 * otherwise, 0.
685 */
686static int set_os(struct ext2_super_block *sb, char *os)
687{
688 if (isdigit (*os))
689 sb->s_creator_os = atoi (os);
690 else if (strcasecmp(os, "linux") == 0)
691 sb->s_creator_os = EXT2_OS_LINUX;
692 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
693 sb->s_creator_os = EXT2_OS_HURD;
694 else if (strcasecmp(os, "masix") == 0)
695 sb->s_creator_os = EXT2_OS_MASIX;
696 else
697 return 0;
698 return 1;
699}
700
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000701#define PATH_SET "PATH=/sbin"
702
Theodore Ts'od163b091997-10-03 17:42:28 +0000703static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000704{
705 char *buf, *token, *next, *p, *arg;
706 int len;
707 int raid_usage = 0;
708
709 len = strlen(opts);
710 buf = malloc(len+1);
711 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000712 fprintf(stderr, _("Couldn't allocate memory to parse "
713 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000714 exit(1);
715 }
716 strcpy(buf, opts);
717 for (token = buf; token && *token; token = next) {
718 p = strchr(token, ',');
719 next = 0;
720 if (p) {
721 *p = 0;
722 next = p+1;
723 }
724 arg = strchr(token, '=');
725 if (arg) {
726 *arg = 0;
727 arg++;
728 }
729 if (strcmp(token, "stride") == 0) {
730 if (!arg) {
731 raid_usage++;
732 continue;
733 }
734 fs_stride = strtoul(arg, &p, 0);
735 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000736 fprintf(stderr,
737 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000738 raid_usage++;
739 continue;
740 }
741 } else
742 raid_usage++;
743 }
744 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000745 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000746 "Raid options are separated by commas, "
747 "and may take an argument which\n"
748 "\tis set off by an equals ('=') sign.\n\n"
749 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000750 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000751 exit(1);
752 }
753}
754
Theodore Ts'o896938d1999-10-23 01:04:50 +0000755static __u32 ok_features[3] = {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000756 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000757 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
758 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000759 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
760};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000761
762
Theodore Ts'o3839e651997-04-26 13:21:57 +0000763static void PRS(int argc, char *argv[])
764{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000765 int c;
766 int size;
767 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000768 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000769 int blocksize = 0;
770 int inode_ratio = 0;
771 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000772 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000773 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000774 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000775 char * raid_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500776 const char * fs_type = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400777 int default_features = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000778 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000779#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000780 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000781#endif
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000782
Theodore Ts'o3839e651997-04-26 13:21:57 +0000783 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000784 if (oldpath) {
785 char *newpath;
786
787 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
788 strcpy (newpath, PATH_SET);
789 strcat (newpath, ":");
790 strcat (newpath, oldpath);
791 putenv (newpath);
792 } else
793 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000794
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000795 tmp = getenv("MKE2FS_SYNC");
796 if (tmp)
797 sync_kludge = atoi(tmp);
798
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799 setbuf(stdout, NULL);
800 setbuf(stderr, NULL);
801 initialize_ext2_error_table();
802 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000803 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400804 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
805 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
806
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000807#ifdef linux
808 if (uname(&ut)) {
809 perror("uname");
810 exit(1);
811 }
812 if ((ut.release[0] == '1') ||
813 (ut.release[0] == '2' && ut.release[1] == '.' &&
814 ut.release[2] < '2' && ut.release[3] == '.')) {
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000815 param.s_rev_level = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400816 param.s_feature_incompat = 0;
817 param.s_feature_compat = 0;
818 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000819 }
820#endif
Andreas Dilger9d8e6342001-08-31 10:24:46 -0600821 fprintf (stderr, "mke2fs %s (%s)\n",
Theodore Ts'o04a96852001-08-30 21:55:26 -0400822 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 if (argc && *argv)
824 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000825 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000826 "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 +0000827 switch (c) {
828 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000829 blocksize = strtoul(optarg, &tmp, 0);
830 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000831 com_err(program_name, 0,
832 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833 exit(1);
834 }
835 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000836 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000837 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000839 case 'c': /* Check for bad blocks */
840 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500841 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000842 break;
843 case 'f':
844 size = strtoul(optarg, &tmp, 0);
845 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000846 com_err(program_name, 0,
847 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000848 optarg);
849 exit(1);
850 }
851 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000852 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000853 printf(_("Warning: fragments not supported. "
854 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855 break;
856 case 'g':
857 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000858 if (*tmp) {
859 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000860 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000861 exit(1);
862 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000863 if ((param.s_blocks_per_group % 8) != 0) {
864 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000865 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000866 exit(1);
867 }
868 break;
869 case 'i':
870 inode_ratio = strtoul(optarg, &tmp, 0);
Theodore Ts'o44c09c02001-01-14 17:02:09 +0000871 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000873 com_err(program_name, 0,
874 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000875 exit(1);
876 }
877 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000878 case 'J':
879 parse_journal_opts(optarg);
880 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000881 case 'j':
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000882 param.s_feature_compat |=
883 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000884 if (!journal_size)
885 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000886 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000887 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000888 bad_blocks_filename = malloc(strlen(optarg)+1);
889 if (!bad_blocks_filename) {
890 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000891 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000892 exit(1);
893 }
894 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895 break;
896 case 'm':
897 reserved_ratio = strtoul(optarg, &tmp, 0);
898 if (reserved_ratio > 50 || *tmp) {
899 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000900 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000901 optarg);
902 exit(1);
903 }
904 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000905 case 'n':
906 noaction++;
907 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000908 case 'o':
909 creator_os = optarg;
910 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000911 case 'r':
912 param.s_rev_level = atoi(optarg);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000913 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400914 param.s_feature_incompat = 0;
915 param.s_feature_compat = 0;
916 param.s_feature_ro_compat = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000917 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000918 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000919 case 's': /* deprecated */
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400920 if (atoi(optarg))
921 param.s_feature_ro_compat |=
922 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
923 else
924 param.s_feature_ro_compat &=
925 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000926 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000927#ifdef EXT2_DYNAMIC_REV
928 case 'I':
929 param.s_inode_size = atoi(optarg);
930 break;
931#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000932 case 'N':
933 num_inodes = atoi(optarg);
934 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000935 case 'v':
936 verbose = 1;
937 break;
938 case 'q':
939 quiet = 1;
940 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000941 case 'F':
942 force = 1;
943 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000944 case 'L':
945 volume_label = optarg;
946 break;
947 case 'M':
948 mount_dir = optarg;
949 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000950 case 'O':
Theodore Ts'of3561ed2001-08-15 11:58:16 -0400951 if (!strcmp(optarg, "none") || default_features) {
952 param.s_feature_compat = 0;
953 param.s_feature_incompat = 0;
954 param.s_feature_ro_compat = 0;
955 default_features = 0;
956 }
957 if (!strcmp(optarg, "none"))
958 break;
959 if (e2p_edit_feature(optarg,
960 &param.s_feature_compat,
961 ok_features)) {
962 fprintf(stderr,
963 _("Invalid filesystem option set: %s\n"), optarg);
964 exit(1);
965 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000966 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000967 case 'R':
968 raid_opts = optarg;
969 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000970 case 'S':
971 super_only = 1;
972 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000973 case 'T':
974 fs_type = optarg;
975 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000976 case 'V':
977 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000978 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000979 error_message(EXT2_ET_BASE));
980 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000981 default:
982 usage();
983 }
984 if (optind == argc)
985 usage();
986 device_name = argv[optind];
987 optind++;
988 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000989 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
990
991 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000992 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993 argv[optind - 1]);
994 exit(1);
995 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000996 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000997 }
998 if (optind < argc)
999 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001000
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001001 if (raid_opts)
1002 parse_raid_opts(raid_opts);
1003
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001004 /*
1005 * If there's no blocksize specified and there is a journal
1006 * device, use it to figure out the blocksize
1007 */
1008 if (blocksize == 0 && journal_device) {
1009 ext2_filsys jfs;
1010
1011 retval = ext2fs_open(journal_device,
1012 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1013 0, unix_io_manager, &jfs);
1014 if (retval) {
1015 com_err(program_name, retval,
1016 _("while trying to open journal device %s\n"),
1017 journal_device);
1018 exit(1);
1019 }
1020 blocksize = jfs->blocksize;
1021 param.s_log_block_size =
1022 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1023 ext2fs_close(jfs);
1024 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001025
Andreas Dilgerfa724582001-08-04 01:18:34 -06001026 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1027 if (!fs_type)
1028 fs_type = "journal";
1029 reserved_ratio = 0;
Theodore Ts'of3561ed2001-08-15 11:58:16 -04001030 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1031 param.s_feature_compat = 0;
1032 param.s_feature_ro_compat = 0;
1033 }
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001034 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1035 (param.s_feature_compat || param.s_feature_ro_compat ||
1036 param.s_feature_incompat))
1037 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1038
Theodore Ts'o74becf31997-04-26 14:37:06 +00001039 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001040 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001041 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001042
Theodore Ts'o3839e651997-04-26 13:21:57 +00001043 param.s_log_frag_size = param.s_log_block_size;
1044
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001045 if (noaction && param.s_blocks_count) {
1046 dev_size = param.s_blocks_count;
1047 retval = 0;
1048 } else
1049 retval = ext2fs_get_device_size(device_name,
1050 EXT2_BLOCK_SIZE(&param),
1051 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001052 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1053 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001054 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001055 exit(1);
1056 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001057 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001058 if (retval == EXT2_ET_UNIMPLEMENTED) {
1059 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001060 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001061 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001062 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001063 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001064 } else {
1065 if (dev_size == 0) {
1066 com_err(program_name, 0,
1067 _("Device size reported to be zero. "
1068 "Invalid partition specified, or\n\t"
1069 "partition table wasn't reread "
1070 "after running fdisk, due to\n\t"
1071 "a modified partition being busy "
1072 "and in use. You may need to reboot\n\t"
1073 "to re-read your partition table.\n"
1074 ));
1075 exit(1);
1076 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001077 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001078 }
1079
Theodore Ts'oa789d841998-03-30 01:20:55 +00001080 } else if (!force && (param.s_blocks_count > dev_size)) {
1081 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001082 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001083 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001084 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001085
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001086 /*
1087 * If the user asked for HAS_JOURNAL, then make sure a journal
1088 * gets created.
1089 */
1090 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1091 !journal_size)
1092 journal_size = -1;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001093
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001094 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001095
Theodore Ts'o521e3681997-04-29 17:48:10 +00001096 if (param.s_blocks_per_group) {
1097 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001098 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001099 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001100 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001101 exit(1);
1102 }
1103 }
1104
Theodore Ts'o3839e651997-04-26 13:21:57 +00001105 /*
1106 * Calculate number of inodes based on the inode ratio
1107 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001108 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001109 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001110 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001111
1112 /*
1113 * Calculate number of blocks to reserve
1114 */
1115 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001116
Theodore Ts'o3839e651997-04-26 13:21:57 +00001117}
1118
1119int main (int argc, char *argv[])
1120{
1121 errcode_t retval = 0;
1122 ext2_filsys fs;
1123 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001124 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001125 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001126
1127#ifdef ENABLE_NLS
1128 setlocale(LC_MESSAGES, "");
1129 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1130 textdomain(NLS_CAT_NAME);
1131#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001132 PRS(argc, argv);
1133
Theodore Ts'o3839e651997-04-26 13:21:57 +00001134 /*
1135 * Initialize the superblock....
1136 */
1137 retval = ext2fs_initialize(device_name, 0, &param,
1138 unix_io_manager, &fs);
1139 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001140 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001141 exit(1);
1142 }
1143
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001144 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001145 * Wipe out the old on-disk superblock
1146 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001147 if (!noaction)
1148 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001149
1150 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001151 * Generate a UUID for it...
1152 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001153 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001154
1155 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001156 * Add "jitter" to the superblock's check interval so that we
1157 * don't check all the filesystems at the same time. We use a
1158 * kludgy hack of using the UUID to derive a random jitter value.
1159 */
1160 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1161 val += fs->super->s_uuid[i];
1162 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1163
1164 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001165 * Override the creator OS, if applicable
1166 */
1167 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001168 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001169 exit(1);
1170 }
1171
1172 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001173 * For the Hurd, we will turn off filetype since it doesn't
1174 * support it.
1175 */
1176 if (fs->super->s_creator_os == EXT2_OS_HURD)
1177 fs->super->s_feature_incompat &=
1178 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1179
1180 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001181 * Set the volume label...
1182 */
1183 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001184 memset(fs->super->s_volume_name, 0,
1185 sizeof(fs->super->s_volume_name));
1186 strncpy(fs->super->s_volume_name, volume_label,
1187 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001188 }
1189
1190 /*
1191 * Set the last mount directory
1192 */
1193 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001194 memset(fs->super->s_last_mounted, 0,
1195 sizeof(fs->super->s_last_mounted));
1196 strncpy(fs->super->s_last_mounted, mount_dir,
1197 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001198 }
1199
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001200 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001201 show_stats(fs);
1202
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001203 if (noaction)
1204 exit(0);
1205
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001206 if (fs->super->s_feature_incompat &
1207 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1208 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001209 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001210 }
1211
Theodore Ts'o3839e651997-04-26 13:21:57 +00001212 if (bad_blocks_filename)
1213 read_bb_file(fs, &bb_list, bad_blocks_filename);
1214 if (cflag)
1215 test_disk(fs, &bb_list);
1216
1217 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001218 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001219 retval = ext2fs_allocate_tables(fs);
1220 if (retval) {
1221 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001222 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001223 exit(1);
1224 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001225 if (super_only) {
1226 fs->super->s_state |= EXT2_ERROR_FS;
1227 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1228 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001229 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001230 int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001231 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001232 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001233 blk_t ret_blk;
1234
1235#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001236 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001237#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001238
1239 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001240 * Wipe out any old MD RAID (or other) metadata at the end
1241 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001242 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001243 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001244 start = (blocks & ~(rsv - 1));
1245 if (start > rsv)
1246 start -= rsv;
1247 if (start > 0)
1248 retval = zero_blocks(fs, start, blocks - start,
1249 NULL, &ret_blk, NULL);
1250
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001251 if (retval) {
1252 com_err(program_name, retval,
1253 _("zeroing block %u at end of filesystem"),
1254 ret_blk);
1255 exit(1);
1256 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001257 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001258 create_root_dir(fs);
1259 create_lost_and_found(fs);
1260 reserve_inodes(fs);
1261 create_bad_block_inode(fs, bb_list);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001262 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001263
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001264 if (journal_device) {
1265 ext2_filsys jfs;
1266
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001267 if (!force)
1268 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001269 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001270
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001271 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1272 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1273 fs->blocksize, unix_io_manager, &jfs);
1274 if (retval) {
1275 com_err(program_name, retval,
1276 _("while trying to open journal device %s\n"),
1277 journal_device);
1278 exit(1);
1279 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001280 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001281 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001282 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001283 fflush(stdout);
1284 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001285 retval = ext2fs_add_journal_device(fs, jfs);
1286 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001287 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001288 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001289 journal_device);
1290 exit(1);
1291 }
1292 if (!quiet)
1293 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001294 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001295 free(journal_device);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001296 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001297 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001298
1299 if (!journal_blocks) {
1300 fs->super->s_feature_compat &=
1301 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1302 goto no_journal;
1303 }
1304 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001305 printf(_("Creating journal (%d blocks): "),
1306 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001307 fflush(stdout);
1308 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001309 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1310 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001311 if (retval) {
1312 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001313 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001314 exit(1);
1315 }
1316 if (!quiet)
1317 printf(_("done\n"));
1318 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001319no_journal:
1320
Theodore Ts'o3839e651997-04-26 13:21:57 +00001321 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001322 printf(_("Writing superblocks and "
1323 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001324 retval = ext2fs_flush(fs);
1325 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001326 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001327 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001328 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001329 printf(_("done\n\n"));
Theodore Ts'o66cf2f62001-06-14 06:42:44 +00001330 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001331 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001332 val = ext2fs_close(fs);
1333 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001334}