blob: 0aad8e549774ce8a04d68de62cdc9fb82e205d99 [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'oa418d3a1997-04-26 14:00:26 +000028#endif
29#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_ERRNO_H
36#include <errno.h>
37#endif
38#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000040#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000041#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000042#include <sys/types.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000043#include <sys/stat.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000044
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000045#ifdef HAVE_LINUX_FS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000046#include <linux/fs.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000047#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000048#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000049#ifdef HAVE_LINUX_MAJOR_H
50#include <linux/major.h>
51#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
53#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000054#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055#include "ext2fs/ext2fs.h"
56#include "../version.h"
57
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000058/* Everything is STDC, these days */
59#define NOARGS void
60
Theodore Ts'o3839e651997-04-26 13:21:57 +000061#define STRIDE_LENGTH 8
62
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000063#ifndef sparc
64#define ZAP_BOOTBLOCK
65#endif
66
Theodore Ts'o3839e651997-04-26 13:21:57 +000067extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000068extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
70const char * program_name = "mke2fs";
71const char * device_name = NULL;
72
73/* Command line options */
74int cflag = 0;
75int verbose = 0;
76int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000077int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000078int force = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +000079int noaction = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000081__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082
83struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000084char *creator_os = NULL;
85char *volume_label = NULL;
86char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000087
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000088static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
89
Theodore Ts'o3839e651997-04-26 13:21:57 +000090static void usage(NOARGS)
91{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000092 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
93 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000094 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
95 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
96 "[-M last-mounted-directory] [-r fs-revision]\n\t[-R raid_opts]"
97 "[-s sparse-super-flag] [-qvSV] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 program_name);
99 exit(1);
100}
101
102static int log2(int arg)
103{
104 int l = 0;
105
106 arg >>= 1;
107 while (arg) {
108 l++;
109 arg >>= 1;
110 }
111 return l;
112}
113
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000114static int log10(unsigned int arg)
115{
116 int l;
117
118 for (l=0; arg ; l++)
119 arg = arg / 10;
120 return l;
121}
122
Theodore Ts'oa789d841998-03-30 01:20:55 +0000123static void proceed_question(NOARGS)
124{
125 fflush(stdout);
126 fflush(stderr);
127 printf("Proceed anyway? (y,n) ");
128 if (getchar() != 'y')
129 exit(1);
130}
131
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000132#ifndef SCSI_BLK_MAJOR
133#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
134#endif
135
Theodore Ts'o74becf31997-04-26 14:37:06 +0000136static void check_plausibility(NOARGS)
137{
138#ifdef HAVE_LINUX_MAJOR_H
139 int val;
140 struct stat s;
141
142 val = stat(device_name, &s);
143
144 if(val == -1) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000145 fprintf(stderr, "Could not stat %s --- %s\n",
146 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000147 if (errno == ENOENT)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000148 fprintf(stderr, "\nThe device apparently does "
149 "not exist; did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000150 exit(1);
151 }
152 if(!S_ISBLK(s.st_mode)) {
153 printf("%s is not a block special device.\n", device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000154 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000155 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000156 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
157 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000158 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
159 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000160 printf("%s is entire device, not just one partition!\n",
161 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000162 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000163 }
164#endif
165}
166
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167static void check_mount(NOARGS)
168{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000169 errcode_t retval;
170 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000172 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
173 if (retval) {
174 com_err("ext2fs_check_if_mount", retval,
175 "while determining whether %s is mounted.",
176 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000178 }
179 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000181
182 fprintf(stderr, "%s is mounted; ", device_name);
183 if (force) {
184 fprintf(stderr, "mke2fs forced anyway. "
185 "Hope /etc/mtab is incorrect.\n");
186 } else {
187 fprintf(stderr, "will not make a filesystem here!\n");
188 exit(1);
189 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190}
191
192/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000193 * This function sets the default parameters for a filesystem
194 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000195 * The type is specified by the user. The size is the maximum size
196 * (in megabytes) for which a set of parameters applies, with a size
197 * of zero meaning that it is the default parameter for the type.
198 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000199 */
200static char default_str[] = "default";
201struct mke2fs_defaults {
202 char *type;
203 int size;
204 int blocksize;
205 int inode_ratio;
206} settings[] = {
207 { default_str, 0, 4096, 8192 },
208 { default_str, 512, 1024, 4096 },
209 { default_str, 3, 1024, 8192 },
210 { "news", 0, 4096, 4096 },
211 { 0, 0, 0, 0},
212};
213
214static void set_fs_defaults(char *fs_type, struct ext2fs_sb *param,
215 int blocksize, int *inode_ratio)
216{
217 int megs;
218 int ratio = 0;
219 struct mke2fs_defaults *p;
220
221 megs = (param->s_blocks_count * (EXT2_BLOCK_SIZE(param) / 1024) /
222 1024);
223 if (inode_ratio)
224 ratio = *inode_ratio;
225 if (!fs_type)
226 fs_type = default_str;
227 for (p = settings; p->type; p++) {
228 if ((strcmp(p->type, fs_type) != 0) &&
229 (strcmp(p->type, default_str) != 0))
230 continue;
231 if ((p->size != 0) &&
232 (megs > p->size))
233 continue;
234 if (ratio == 0)
235 *inode_ratio = p->inode_ratio;
236 if (blocksize == 0) {
237 param->s_log_frag_size = param->s_log_block_size =
238 log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
239 }
240 }
241 if (blocksize == 0)
242 param->s_blocks_count /= EXT2_BLOCK_SIZE(param) / 1024;
243}
244
245/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246 * Helper function for read_bb_file and test_disk
247 */
248static void invalid_block(ext2_filsys fs, blk_t blk)
249{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000250 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 return;
252}
253
254/*
255 * Reads the bad blocks list from a file
256 */
257static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
258 const char *bad_blocks_file)
259{
260 FILE *f;
261 errcode_t retval;
262
263 f = fopen(bad_blocks_file, "r");
264 if (!f) {
265 com_err("read_bad_blocks_file", errno,
266 "while trying to open %s", bad_blocks_file);
267 exit(1);
268 }
269 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
270 fclose (f);
271 if (retval) {
272 com_err("ext2fs_read_bb_FILE", retval,
273 "while reading in list of bad blocks from file");
274 exit(1);
275 }
276}
277
278/*
279 * Runs the badblocks program to test the disk
280 */
281static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
282{
283 FILE *f;
284 errcode_t retval;
285 char buf[1024];
286
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000287 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
288 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289 fs->super->s_blocks_count);
290 if (verbose)
291 printf("Running command: %s\n", buf);
292 f = popen(buf, "r");
293 if (!f) {
294 com_err("popen", errno,
295 "while trying run '%s'", buf);
296 exit(1);
297 }
298 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000299 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 if (retval) {
301 com_err("ext2fs_read_bb_FILE", retval,
302 "while processing list of bad blocks from program");
303 exit(1);
304 }
305}
306
307static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
308{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000309 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 int must_be_good;
311 blk_t blk;
312 badblocks_iterate bb_iter;
313 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000314 blk_t group_block;
315 int group;
316 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317
318 if (!bb_list)
319 return;
320
321 /*
322 * The primary superblock and group descriptors *must* be
323 * good; if not, abort.
324 */
325 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
326 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
327 if (badblocks_list_test(bb_list, i)) {
328 fprintf(stderr, "Block %d in primary superblock/group "
329 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000330 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331 "in order to build a filesystem.\n",
332 fs->super->s_first_data_block, must_be_good);
333 fprintf(stderr, "Aborting....\n");
334 exit(1);
335 }
336 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000337
338 /*
339 * See if any of the bad blocks are showing up in the backup
340 * superblocks and/or group descriptors. If so, issue a
341 * warning and adjust the block counts appropriately.
342 */
343 group_block = fs->super->s_first_data_block +
344 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000345
346 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000347 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000348 for (j=0; j < fs->desc_blocks+1; j++) {
349 if (badblocks_list_test(bb_list, group_block +
350 j)) {
351 if (!group_bad)
352 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000353"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000354" bad blocks.\n\n",
355 group_block);
356 group_bad++;
357 group = ext2fs_group_of_blk(fs, group_block+j);
358 fs->group_desc[group].bg_free_blocks_count++;
359 fs->super->s_free_blocks_count++;
360 }
361 }
362 group_block += fs->super->s_blocks_per_group;
363 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364
365 /*
366 * Mark all the bad blocks as used...
367 */
368 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
369 if (retval) {
370 com_err("badblocks_list_iterate_begin", retval,
371 "while marking bad blocks as used");
372 exit(1);
373 }
374 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000375 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376 badblocks_list_iterate_end(bb_iter);
377}
378
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000379static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380{
381 errcode_t retval;
382 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000383 int i, j, num, count;
384 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000385 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000386 int sync_kludge = 0;
387 char *mke2fs_sync;
388
389 mke2fs_sync = getenv("MKE2FS_SYNC");
390 if (mke2fs_sync)
391 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392
393 buf = malloc(fs->blocksize * STRIDE_LENGTH);
394 if (!buf) {
395 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
396 exit(1);
397 }
398 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000399
400 /*
401 * Figure out how many digits we need
402 */
403 i = log10(fs->group_desc_count);
404 sprintf(format, "%%%dd/%%%dld", i, i);
405 memset(backup, '\b', sizeof(backup)-1);
406 backup[sizeof(backup)-1] = 0;
407 if ((2*i)+1 < sizeof(backup))
408 backup[(2*i)+1] = 0;
409
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 if (!quiet)
411 printf("Writing inode tables: ");
412 for (i = 0; i < fs->group_desc_count; i++) {
413 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000414 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000416 blk = fs->group_desc[i].bg_inode_table;
417 num = fs->inode_blocks_per_group;
418
419 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
420 if (num-j > STRIDE_LENGTH)
421 count = STRIDE_LENGTH;
422 else
423 count = num - j;
424 retval = io_channel_write_blk(fs->io, blk, count, buf);
425 if (retval)
426 printf("Warning: could not write %d blocks "
427 "in inode table starting at %d: %s\n",
428 count, blk, error_message(retval));
429 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000431 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000432 if (sync_kludge) {
433 if (sync_kludge == 1)
434 sync();
435 else if ((i % sync_kludge) == 0)
436 sync();
437 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000439 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000441 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442}
443
444static void create_root_dir(ext2_filsys fs)
445{
446 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000447 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448
449 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
450 if (retval) {
451 com_err("ext2fs_mkdir", retval, "while creating root dir");
452 exit(1);
453 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000454 if (geteuid()) {
455 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
456 if (retval) {
457 com_err("ext2fs_read_inode", retval,
458 "while reading root inode");
459 exit(1);
460 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000461 inode.i_uid = getuid();
462 if (inode.i_uid)
463 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000464 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
465 if (retval) {
466 com_err("ext2fs_write_inode", retval,
467 "while setting root inode ownership");
468 exit(1);
469 }
470 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471}
472
473static void create_lost_and_found(ext2_filsys fs)
474{
475 errcode_t retval;
476 ino_t ino;
477 const char *name = "lost+found";
478 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000479 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480
481 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
482 if (retval) {
483 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
484 exit(1);
485 }
486
487 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
488 if (retval) {
489 com_err("ext2_lookup", retval, "while looking up /lost+found");
490 exit(1);
491 }
492
493 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000494 if ((lpf_size += fs->blocksize) >= 16*1024)
495 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 retval = ext2fs_expand_dir(fs, ino);
497 if (retval) {
498 com_err("ext2fs_expand_dir", retval,
499 "while expanding /lost+found");
500 exit(1);
501 }
502 }
503}
504
505static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
506{
507 errcode_t retval;
508
Theodore Ts'of3db3561997-04-26 13:34:30 +0000509 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 fs->group_desc[0].bg_free_inodes_count--;
511 fs->super->s_free_inodes_count--;
512 retval = ext2fs_update_bb_inode(fs, bb_list);
513 if (retval) {
514 com_err("ext2fs_update_bb_inode", retval,
515 "while setting bad block inode");
516 exit(1);
517 }
518
519}
520
521static void reserve_inodes(ext2_filsys fs)
522{
523 ino_t i;
524 int group;
525
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000526 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000527 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 group = ext2fs_group_of_ino(fs, i);
529 fs->group_desc[group].bg_free_inodes_count--;
530 fs->super->s_free_inodes_count--;
531 }
532 ext2fs_mark_ib_dirty(fs);
533}
534
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000535#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000536static void zap_bootblock(ext2_filsys fs)
537{
538 char buf[512];
539 int retval;
540
541 memset(buf, 0, 512);
542
543 retval = io_channel_write_blk(fs->io, 0, -512, buf);
544 if (retval)
545 printf("Warning: could not erase block 0: %s\n",
546 error_message(retval));
547}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000548#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000549
550
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551static void show_stats(ext2_filsys fs)
552{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000553 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
554 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000556 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557
558 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000559 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000561
562 memset(buf, 0, sizeof(buf));
563 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
564 printf("Filesystem label=%s\n", buf);
565 printf("OS type: ");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000566 switch (fs->super->s_creator_os) {
567 case EXT2_OS_LINUX: printf ("Linux"); break;
568 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
569 case EXT2_OS_MASIX: printf ("Masix"); break;
570 default: printf ("(unknown os)");
571 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000572 printf("\n");
573 printf("Block size=%u (log=%u)\n", fs->blocksize,
574 s->s_log_block_size);
575 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
576 s->s_log_frag_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000577 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000579 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 s->s_r_blocks_count,
581 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000582 printf("First data block=%u\n", s->s_first_data_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000583 printf("%lu block group%s\n", fs->group_desc_count,
584 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000585 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000587 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588
589 if (fs->group_desc_count == 1) {
590 printf("\n");
591 return;
592 }
593
594 printf("Superblock backups stored on blocks: ");
595 group_block = s->s_first_data_block;
596 col_left = 0;
597 for (i = 1; i < fs->group_desc_count; i++) {
598 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000599 if (!ext2fs_bg_has_super(fs, i))
600 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000601 if (i != 1)
602 printf(", ");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000603 need = log10(group_block) + 2;
604 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000606 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000608 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000609 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 }
611 printf("\n\n");
612}
613
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000614#ifndef HAVE_STRCASECMP
615static int strcasecmp (char *s1, char *s2)
616{
617 while (*s1 && *s2) {
618 int ch1 = *s1++, ch2 = *s2++;
619 if (isupper (ch1))
620 ch1 = tolower (ch1);
621 if (isupper (ch2))
622 ch2 = tolower (ch2);
623 if (ch1 != ch2)
624 return ch1 - ch2;
625 }
626 return *s1 ? 1 : *s2 ? -1 : 0;
627}
628#endif
629
630/*
631 * Set the S_CREATOR_OS field. Return true if OS is known,
632 * otherwise, 0.
633 */
634static int set_os(struct ext2_super_block *sb, char *os)
635{
636 if (isdigit (*os))
637 sb->s_creator_os = atoi (os);
638 else if (strcasecmp(os, "linux") == 0)
639 sb->s_creator_os = EXT2_OS_LINUX;
640 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
641 sb->s_creator_os = EXT2_OS_HURD;
642 else if (strcasecmp(os, "masix") == 0)
643 sb->s_creator_os = EXT2_OS_MASIX;
644 else
645 return 0;
646 return 1;
647}
648
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000649#define PATH_SET "PATH=/sbin"
650
Theodore Ts'od163b091997-10-03 17:42:28 +0000651static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000652{
653 char *buf, *token, *next, *p, *arg;
654 int len;
655 int raid_usage = 0;
656
657 len = strlen(opts);
658 buf = malloc(len+1);
659 if (!buf) {
660 fprintf(stderr, "Couldn't allocate memory to parse "
661 "raid options!\n");
662 exit(1);
663 }
664 strcpy(buf, opts);
665 for (token = buf; token && *token; token = next) {
666 p = strchr(token, ',');
667 next = 0;
668 if (p) {
669 *p = 0;
670 next = p+1;
671 }
672 arg = strchr(token, '=');
673 if (arg) {
674 *arg = 0;
675 arg++;
676 }
677 if (strcmp(token, "stride") == 0) {
678 if (!arg) {
679 raid_usage++;
680 continue;
681 }
682 fs_stride = strtoul(arg, &p, 0);
683 if (*p || (fs_stride == 0)) {
684 fprintf(stderr, "Invalid stride parameter.\n");
685 raid_usage++;
686 continue;
687 }
688 } else
689 raid_usage++;
690 }
691 if (raid_usage) {
692 fprintf(stderr, "\nBad raid options specified.\n\n"
693 "Raid options are separated by commas, "
694 "and may take an argument which\n"
695 "\tis set off by an equals ('=') sign.\n\n"
696 "Valid raid options are:\n"
697 "\tstride=<stride length in blocks>\n\n");
698 exit(1);
699 }
700}
701
702
703
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704static void PRS(int argc, char *argv[])
705{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000706 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707 int size;
708 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000709 blk_t max = 8192;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000710 int blocksize = 0;
711 int inode_ratio = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 int reserved_ratio = 5;
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000713 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000714 errcode_t retval;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000715 int sparse_option = 1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000716 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000717 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000718 char *raid_opts = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000719 char *fs_type = 0;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000720 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000721#ifdef linux
722 struct utsname ut;
723
724 if (uname(&ut)) {
725 perror("uname");
726 exit(1);
727 }
728 if (ut.release[0] == '2' && ut.release[1] == '.' &&
729 ut.release[2] < '2' && ut.release[3] == '.')
730 sparse_option = 0;
731#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000733 if (oldpath) {
734 char *newpath;
735
736 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
737 strcpy (newpath, PATH_SET);
738 strcat (newpath, ":");
739 strcat (newpath, oldpath);
740 putenv (newpath);
741 } else
742 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743
744 setbuf(stdout, NULL);
745 setbuf(stderr, NULL);
746 initialize_ext2_error_table();
747 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000748 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749
750 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
751 E2FSPROGS_VERSION, E2FSPROGS_DATE,
752 EXT2FS_VERSION, EXT2FS_DATE);
753 if (argc && *argv)
754 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000755 while ((c = getopt (argc, argv,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000756 "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757 switch (c) {
758 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000759 blocksize = strtoul(optarg, &tmp, 0);
760 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761 com_err(program_name, 0, "bad block size - %s",
762 optarg);
763 exit(1);
764 }
765 param.s_log_block_size =
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000766 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
767 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768 break;
769 case 'c':
770 case 't': /* Check for bad blocks */
771 cflag = 1;
772 break;
773 case 'f':
774 size = strtoul(optarg, &tmp, 0);
775 if (size < 1024 || size > 4096 || *tmp) {
776 com_err(program_name, 0, "bad fragment size - %s",
777 optarg);
778 exit(1);
779 }
780 param.s_log_frag_size =
781 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
782 printf("Warning: fragments not supported. "
783 "Ignoring -f option\n");
784 break;
785 case 'g':
786 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000787 if (*tmp) {
788 com_err(program_name, 0,
789 "Illegal number for blocks per group");
790 exit(1);
791 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000792 if ((param.s_blocks_per_group % 8) != 0) {
793 com_err(program_name, 0,
794 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 exit(1);
796 }
797 break;
798 case 'i':
799 inode_ratio = strtoul(optarg, &tmp, 0);
800 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
801 *tmp) {
802 com_err(program_name, 0, "bad inode ratio - %s",
803 optarg);
804 exit(1);
805 }
806 break;
807 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000808 bad_blocks_filename = malloc(strlen(optarg)+1);
809 if (!bad_blocks_filename) {
810 com_err(program_name, ENOMEM,
811 "in malloc for bad_blocks_filename");
812 exit(1);
813 }
814 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000815 break;
816 case 'm':
817 reserved_ratio = strtoul(optarg, &tmp, 0);
818 if (reserved_ratio > 50 || *tmp) {
819 com_err(program_name, 0,
820 "bad reserved blocks percent - %s",
821 optarg);
822 exit(1);
823 }
824 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000825 case 'n':
826 noaction++;
827 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000828 case 'o':
829 creator_os = optarg;
830 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000831 case 'r':
832 param.s_rev_level = atoi(optarg);
833 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000834 case 's':
835 sparse_option = atoi(optarg);
836 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000837#ifdef EXT2_DYNAMIC_REV
838 case 'I':
839 param.s_inode_size = atoi(optarg);
840 break;
841#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000842 case 'N':
843 num_inodes = atoi(optarg);
844 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000845 case 'v':
846 verbose = 1;
847 break;
848 case 'q':
849 quiet = 1;
850 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000851 case 'F':
852 force = 1;
853 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000854 case 'L':
855 volume_label = optarg;
856 break;
857 case 'M':
858 mount_dir = optarg;
859 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000860 case 'R':
861 raid_opts = optarg;
862 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000863 case 'S':
864 super_only = 1;
865 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000866 case 'T':
867 fs_type = optarg;
868 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000869 case 'V':
870 /* Print version number and exit */
871 fprintf(stderr, "\tUsing %s\n",
872 error_message(EXT2_ET_BASE));
873 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000874 default:
875 usage();
876 }
877 if (optind == argc)
878 usage();
879 device_name = argv[optind];
880 optind++;
881 if (optind < argc) {
882 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
883 if (*tmp) {
884 com_err(program_name, 0, "bad blocks count - %s",
885 argv[optind - 1]);
886 exit(1);
887 }
888 }
889 if (optind < argc)
890 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000891
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000892 if (raid_opts)
893 parse_raid_opts(raid_opts);
894
Theodore Ts'o74becf31997-04-26 14:37:06 +0000895 if (!force)
896 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000897 check_mount();
898
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 param.s_log_frag_size = param.s_log_block_size;
900
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000901 if (noaction && param.s_blocks_count) {
902 dev_size = param.s_blocks_count;
903 retval = 0;
904 } else
905 retval = ext2fs_get_device_size(device_name,
906 EXT2_BLOCK_SIZE(&param),
907 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000908 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
909 com_err(program_name, retval,
910 "while trying to determine filesystem size");
911 exit(1);
912 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000913 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000914 if (retval == EXT2_ET_UNIMPLEMENTED) {
915 com_err(program_name, 0,
916 "Couldn't determine device size; you "
917 "must specify\nthe size of the "
918 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000919 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000920 } else
921 param.s_blocks_count = dev_size;
922 } else if (!force && (param.s_blocks_count > dev_size)) {
923 com_err(program_name, 0,
924 "Filesystem larger than apparent filesystem size.");
925 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000926 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000928 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
929
Theodore Ts'o521e3681997-04-29 17:48:10 +0000930 if (param.s_blocks_per_group) {
931 if (param.s_blocks_per_group < 256 ||
932 param.s_blocks_per_group > max || *tmp) {
933 com_err(program_name, 0,
934 "blocks per group count out of range");
935 exit(1);
936 }
937 }
938
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 /*
940 * Calculate number of inodes based on the inode ratio
941 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000942 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'of3db3561997-04-26 13:34:30 +0000943 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
944 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000945
946 /*
947 * Calculate number of blocks to reserve
948 */
949 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000950
Theodore Ts'o521e3681997-04-29 17:48:10 +0000951#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000952 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000953 param_ext2->s_feature_ro_compat |=
954 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
955#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956}
957
958int main (int argc, char *argv[])
959{
960 errcode_t retval = 0;
961 ext2_filsys fs;
962 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000963 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000964
965 PRS(argc, argv);
966
Theodore Ts'o3839e651997-04-26 13:21:57 +0000967 /*
968 * Initialize the superblock....
969 */
970 retval = ext2fs_initialize(device_name, 0, &param,
971 unix_io_manager, &fs);
972 if (retval) {
973 com_err(device_name, retval, "while setting up superblock");
974 exit(1);
975 }
976
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000977 /*
978 * Generate a UUID for it...
979 */
980 s = (struct ext2fs_sb *) fs->super;
981 uuid_generate(s->s_uuid);
982
983 /*
984 * Override the creator OS, if applicable
985 */
986 if (creator_os && !set_os(fs->super, creator_os)) {
987 com_err (program_name, 0, "unknown os - %s", creator_os);
988 exit(1);
989 }
990
991 /*
992 * Set the volume label...
993 */
994 if (volume_label) {
995 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
996 strncpy(s->s_volume_name, volume_label,
997 sizeof(s->s_volume_name));
998 }
999
1000 /*
1001 * Set the last mount directory
1002 */
1003 if (mount_dir) {
1004 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1005 strncpy(s->s_last_mounted, mount_dir,
1006 sizeof(s->s_last_mounted));
1007 }
1008
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001009 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001010 show_stats(fs);
1011
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001012 if (noaction)
1013 exit(0);
1014
Theodore Ts'o3839e651997-04-26 13:21:57 +00001015 if (bad_blocks_filename)
1016 read_bb_file(fs, &bb_list, bad_blocks_filename);
1017 if (cflag)
1018 test_disk(fs, &bb_list);
1019
1020 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001021 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001022 retval = ext2fs_allocate_tables(fs);
1023 if (retval) {
1024 com_err(program_name, retval,
1025 "while trying to allocate filesystem tables");
1026 exit(1);
1027 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001028 if (super_only) {
1029 fs->super->s_state |= EXT2_ERROR_FS;
1030 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1031 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001032 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001033 create_root_dir(fs);
1034 create_lost_and_found(fs);
1035 reserve_inodes(fs);
1036 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001037#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001038 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001039#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001040 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041
1042 if (!quiet)
1043 printf("Writing superblocks and "
1044 "filesystem accounting information: ");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001045 retval = ext2fs_flush(fs);
1046 if (retval) {
1047 printf("\nWarning, had trouble writing out superblocks.");
1048 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001049 if (!quiet)
1050 printf("done\n");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001051 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 return 0;
1053}