blob: c8ac45eebbde6bdcadf3f99e69ddaaf180937774 [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>
22#include <termios.h>
23#include <time.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000024#ifdef linux
25#include <sys/utsname.h>
26#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000027#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000029#endif
30#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000031#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#endif
33#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000035#endif
36#ifdef HAVE_ERRNO_H
37#include <errno.h>
38#endif
39#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000040#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000041#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000043#include <sys/types.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000044#include <sys/stat.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000046#ifdef HAVE_LINUX_FS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000047#include <linux/fs.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000048#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000049#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000050#ifdef HAVE_LINUX_MAJOR_H
51#include <linux/major.h>
52#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000053
54#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000055#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "ext2fs/ext2fs.h"
57#include "../version.h"
58
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000059/* Everything is STDC, these days */
60#define NOARGS void
61
Theodore Ts'o3839e651997-04-26 13:21:57 +000062#define STRIDE_LENGTH 8
63
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000064#ifndef sparc
65#define ZAP_BOOTBLOCK
66#endif
67
Theodore Ts'o3839e651997-04-26 13:21:57 +000068extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000069extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000070
71const char * program_name = "mke2fs";
72const char * device_name = NULL;
73
74/* Command line options */
75int cflag = 0;
76int verbose = 0;
77int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000078int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000079int force = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +000080int noaction = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000082__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
84struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000085char *creator_os = NULL;
86char *volume_label = NULL;
87char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000088
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000089static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
90
Theodore Ts'o3839e651997-04-26 13:21:57 +000091static void usage(NOARGS)
92{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000093 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
94 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000095 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
96 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
97 "[-M last-mounted-directory] [-r fs-revision]\n\t[-R raid_opts]"
98 "[-s sparse-super-flag] [-qvSV] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +000099 program_name);
100 exit(1);
101}
102
103static int log2(int arg)
104{
105 int l = 0;
106
107 arg >>= 1;
108 while (arg) {
109 l++;
110 arg >>= 1;
111 }
112 return l;
113}
114
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000115static int log10(unsigned int arg)
116{
117 int l;
118
119 for (l=0; arg ; l++)
120 arg = arg / 10;
121 return l;
122}
123
Theodore Ts'oa789d841998-03-30 01:20:55 +0000124static void proceed_question(NOARGS)
125{
126 fflush(stdout);
127 fflush(stderr);
128 printf("Proceed anyway? (y,n) ");
129 if (getchar() != 'y')
130 exit(1);
131}
132
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000133#ifndef SCSI_BLK_MAJOR
134#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
135#endif
136
Theodore Ts'o74becf31997-04-26 14:37:06 +0000137static void check_plausibility(NOARGS)
138{
139#ifdef HAVE_LINUX_MAJOR_H
140 int val;
141 struct stat s;
142
143 val = stat(device_name, &s);
144
145 if(val == -1) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000146 fprintf(stderr, "Could not stat %s --- %s\n",
147 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000148 if (errno == ENOENT)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000149 fprintf(stderr, "\nThe device apparently does "
150 "not exist; did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000151 exit(1);
152 }
153 if(!S_ISBLK(s.st_mode)) {
154 printf("%s is not a block special device.\n", device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000155 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000156 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000157 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
158 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000159 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
160 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000161 printf("%s is entire device, not just one partition!\n",
162 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000163 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000164 }
165#endif
166}
167
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168static void check_mount(NOARGS)
169{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000170 errcode_t retval;
171 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000173 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
174 if (retval) {
175 com_err("ext2fs_check_if_mount", retval,
176 "while determining whether %s is mounted.",
177 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000179 }
180 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000182
183 fprintf(stderr, "%s is mounted; ", device_name);
184 if (force) {
185 fprintf(stderr, "mke2fs forced anyway. "
186 "Hope /etc/mtab is incorrect.\n");
187 } else {
188 fprintf(stderr, "will not make a filesystem here!\n");
189 exit(1);
190 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191}
192
193/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000194 * This function sets the default parameters for a filesystem
195 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000196 * The type is specified by the user. The size is the maximum size
197 * (in megabytes) for which a set of parameters applies, with a size
198 * of zero meaning that it is the default parameter for the type.
199 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000200 */
201static char default_str[] = "default";
202struct mke2fs_defaults {
203 char *type;
204 int size;
205 int blocksize;
206 int inode_ratio;
207} settings[] = {
208 { default_str, 0, 4096, 8192 },
209 { default_str, 512, 1024, 4096 },
210 { default_str, 3, 1024, 8192 },
211 { "news", 0, 4096, 4096 },
212 { 0, 0, 0, 0},
213};
214
215static void set_fs_defaults(char *fs_type, struct ext2fs_sb *param,
216 int blocksize, int *inode_ratio)
217{
218 int megs;
219 int ratio = 0;
220 struct mke2fs_defaults *p;
221
222 megs = (param->s_blocks_count * (EXT2_BLOCK_SIZE(param) / 1024) /
223 1024);
224 if (inode_ratio)
225 ratio = *inode_ratio;
226 if (!fs_type)
227 fs_type = default_str;
228 for (p = settings; p->type; p++) {
229 if ((strcmp(p->type, fs_type) != 0) &&
230 (strcmp(p->type, default_str) != 0))
231 continue;
232 if ((p->size != 0) &&
233 (megs > p->size))
234 continue;
235 if (ratio == 0)
236 *inode_ratio = p->inode_ratio;
237 if (blocksize == 0) {
238 param->s_log_frag_size = param->s_log_block_size =
239 log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
240 }
241 }
242 if (blocksize == 0)
243 param->s_blocks_count /= EXT2_BLOCK_SIZE(param) / 1024;
244}
245
246/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 * Helper function for read_bb_file and test_disk
248 */
249static void invalid_block(ext2_filsys fs, blk_t blk)
250{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000251 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252 return;
253}
254
255/*
256 * Reads the bad blocks list from a file
257 */
258static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
259 const char *bad_blocks_file)
260{
261 FILE *f;
262 errcode_t retval;
263
264 f = fopen(bad_blocks_file, "r");
265 if (!f) {
266 com_err("read_bad_blocks_file", errno,
267 "while trying to open %s", bad_blocks_file);
268 exit(1);
269 }
270 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
271 fclose (f);
272 if (retval) {
273 com_err("ext2fs_read_bb_FILE", retval,
274 "while reading in list of bad blocks from file");
275 exit(1);
276 }
277}
278
279/*
280 * Runs the badblocks program to test the disk
281 */
282static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
283{
284 FILE *f;
285 errcode_t retval;
286 char buf[1024];
287
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000288 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
289 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290 fs->super->s_blocks_count);
291 if (verbose)
292 printf("Running command: %s\n", buf);
293 f = popen(buf, "r");
294 if (!f) {
295 com_err("popen", errno,
296 "while trying run '%s'", buf);
297 exit(1);
298 }
299 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 if (retval) {
302 com_err("ext2fs_read_bb_FILE", retval,
303 "while processing list of bad blocks from program");
304 exit(1);
305 }
306}
307
308static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
309{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000310 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000311 int must_be_good;
312 blk_t blk;
313 badblocks_iterate bb_iter;
314 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000315 blk_t group_block;
316 int group;
317 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318
319 if (!bb_list)
320 return;
321
322 /*
323 * The primary superblock and group descriptors *must* be
324 * good; if not, abort.
325 */
326 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
327 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
328 if (badblocks_list_test(bb_list, i)) {
329 fprintf(stderr, "Block %d in primary superblock/group "
330 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000331 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332 "in order to build a filesystem.\n",
333 fs->super->s_first_data_block, must_be_good);
334 fprintf(stderr, "Aborting....\n");
335 exit(1);
336 }
337 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000338
339 /*
340 * See if any of the bad blocks are showing up in the backup
341 * superblocks and/or group descriptors. If so, issue a
342 * warning and adjust the block counts appropriately.
343 */
344 group_block = fs->super->s_first_data_block +
345 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000346
347 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000348 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000349 for (j=0; j < fs->desc_blocks+1; j++) {
350 if (badblocks_list_test(bb_list, group_block +
351 j)) {
352 if (!group_bad)
353 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000354"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000355" bad blocks.\n\n",
356 group_block);
357 group_bad++;
358 group = ext2fs_group_of_blk(fs, group_block+j);
359 fs->group_desc[group].bg_free_blocks_count++;
360 fs->super->s_free_blocks_count++;
361 }
362 }
363 group_block += fs->super->s_blocks_per_group;
364 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365
366 /*
367 * Mark all the bad blocks as used...
368 */
369 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
370 if (retval) {
371 com_err("badblocks_list_iterate_begin", retval,
372 "while marking bad blocks as used");
373 exit(1);
374 }
375 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000376 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377 badblocks_list_iterate_end(bb_iter);
378}
379
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000380static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381{
382 errcode_t retval;
383 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000384 int i, j, num, count;
385 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000386 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000387 int sync_kludge = 0;
388 char *mke2fs_sync;
389
390 mke2fs_sync = getenv("MKE2FS_SYNC");
391 if (mke2fs_sync)
392 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393
394 buf = malloc(fs->blocksize * STRIDE_LENGTH);
395 if (!buf) {
396 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
397 exit(1);
398 }
399 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000400
401 /*
402 * Figure out how many digits we need
403 */
404 i = log10(fs->group_desc_count);
405 sprintf(format, "%%%dd/%%%dld", i, i);
406 memset(backup, '\b', sizeof(backup)-1);
407 backup[sizeof(backup)-1] = 0;
408 if ((2*i)+1 < sizeof(backup))
409 backup[(2*i)+1] = 0;
410
Theodore Ts'o3839e651997-04-26 13:21:57 +0000411 if (!quiet)
412 printf("Writing inode tables: ");
413 for (i = 0; i < fs->group_desc_count; i++) {
414 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000415 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000417 blk = fs->group_desc[i].bg_inode_table;
418 num = fs->inode_blocks_per_group;
419
420 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
421 if (num-j > STRIDE_LENGTH)
422 count = STRIDE_LENGTH;
423 else
424 count = num - j;
425 retval = io_channel_write_blk(fs->io, blk, count, buf);
426 if (retval)
427 printf("Warning: could not write %d blocks "
428 "in inode table starting at %d: %s\n",
429 count, blk, error_message(retval));
430 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000432 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000433 if (sync_kludge) {
434 if (sync_kludge == 1)
435 sync();
436 else if ((i % sync_kludge) == 0)
437 sync();
438 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000440 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000442 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443}
444
445static void create_root_dir(ext2_filsys fs)
446{
447 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000448 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449
450 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
451 if (retval) {
452 com_err("ext2fs_mkdir", retval, "while creating root dir");
453 exit(1);
454 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000455 if (geteuid()) {
456 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
457 if (retval) {
458 com_err("ext2fs_read_inode", retval,
459 "while reading root inode");
460 exit(1);
461 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000462 inode.i_uid = getuid();
463 if (inode.i_uid)
464 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000465 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
466 if (retval) {
467 com_err("ext2fs_write_inode", retval,
468 "while setting root inode ownership");
469 exit(1);
470 }
471 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472}
473
474static void create_lost_and_found(ext2_filsys fs)
475{
476 errcode_t retval;
477 ino_t ino;
478 const char *name = "lost+found";
479 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000480 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481
482 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
483 if (retval) {
484 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
485 exit(1);
486 }
487
488 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
489 if (retval) {
490 com_err("ext2_lookup", retval, "while looking up /lost+found");
491 exit(1);
492 }
493
494 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000495 if ((lpf_size += fs->blocksize) >= 16*1024)
496 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 retval = ext2fs_expand_dir(fs, ino);
498 if (retval) {
499 com_err("ext2fs_expand_dir", retval,
500 "while expanding /lost+found");
501 exit(1);
502 }
503 }
504}
505
506static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
507{
508 errcode_t retval;
509
Theodore Ts'of3db3561997-04-26 13:34:30 +0000510 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 fs->group_desc[0].bg_free_inodes_count--;
512 fs->super->s_free_inodes_count--;
513 retval = ext2fs_update_bb_inode(fs, bb_list);
514 if (retval) {
515 com_err("ext2fs_update_bb_inode", retval,
516 "while setting bad block inode");
517 exit(1);
518 }
519
520}
521
522static void reserve_inodes(ext2_filsys fs)
523{
524 ino_t i;
525 int group;
526
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000527 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000528 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 group = ext2fs_group_of_ino(fs, i);
530 fs->group_desc[group].bg_free_inodes_count--;
531 fs->super->s_free_inodes_count--;
532 }
533 ext2fs_mark_ib_dirty(fs);
534}
535
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000536#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000537static void zap_bootblock(ext2_filsys fs)
538{
539 char buf[512];
540 int retval;
541
542 memset(buf, 0, 512);
543
544 retval = io_channel_write_blk(fs->io, 0, -512, buf);
545 if (retval)
546 printf("Warning: could not erase block 0: %s\n",
547 error_message(retval));
548}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000549#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000550
551
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552static void show_stats(ext2_filsys fs)
553{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000554 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
555 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000557 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558
559 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000560 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000562
563 memset(buf, 0, sizeof(buf));
564 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
565 printf("Filesystem label=%s\n", buf);
566 printf("OS type: ");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000567 switch (fs->super->s_creator_os) {
568 case EXT2_OS_LINUX: printf ("Linux"); break;
569 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
570 case EXT2_OS_MASIX: printf ("Masix"); break;
571 default: printf ("(unknown os)");
572 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000573 printf("\n");
574 printf("Block size=%u (log=%u)\n", fs->blocksize,
575 s->s_log_block_size);
576 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
577 s->s_log_frag_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000578 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000580 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581 s->s_r_blocks_count,
582 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000583 printf("First data block=%u\n", s->s_first_data_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 printf("%lu block group%s\n", fs->group_desc_count,
585 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000586 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000588 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589
590 if (fs->group_desc_count == 1) {
591 printf("\n");
592 return;
593 }
594
595 printf("Superblock backups stored on blocks: ");
596 group_block = s->s_first_data_block;
597 col_left = 0;
598 for (i = 1; i < fs->group_desc_count; i++) {
599 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000600 if (!ext2fs_bg_has_super(fs, i))
601 continue;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000602 need = log10(group_block) + 2;
603 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000605 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000607 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000608 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 if (i != fs->group_desc_count - 1)
610 printf(", ");
611 }
612 printf("\n\n");
613}
614
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000615#ifndef HAVE_STRCASECMP
616static int strcasecmp (char *s1, char *s2)
617{
618 while (*s1 && *s2) {
619 int ch1 = *s1++, ch2 = *s2++;
620 if (isupper (ch1))
621 ch1 = tolower (ch1);
622 if (isupper (ch2))
623 ch2 = tolower (ch2);
624 if (ch1 != ch2)
625 return ch1 - ch2;
626 }
627 return *s1 ? 1 : *s2 ? -1 : 0;
628}
629#endif
630
631/*
632 * Set the S_CREATOR_OS field. Return true if OS is known,
633 * otherwise, 0.
634 */
635static int set_os(struct ext2_super_block *sb, char *os)
636{
637 if (isdigit (*os))
638 sb->s_creator_os = atoi (os);
639 else if (strcasecmp(os, "linux") == 0)
640 sb->s_creator_os = EXT2_OS_LINUX;
641 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
642 sb->s_creator_os = EXT2_OS_HURD;
643 else if (strcasecmp(os, "masix") == 0)
644 sb->s_creator_os = EXT2_OS_MASIX;
645 else
646 return 0;
647 return 1;
648}
649
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000650#define PATH_SET "PATH=/sbin"
651
Theodore Ts'od163b091997-10-03 17:42:28 +0000652static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000653{
654 char *buf, *token, *next, *p, *arg;
655 int len;
656 int raid_usage = 0;
657
658 len = strlen(opts);
659 buf = malloc(len+1);
660 if (!buf) {
661 fprintf(stderr, "Couldn't allocate memory to parse "
662 "raid options!\n");
663 exit(1);
664 }
665 strcpy(buf, opts);
666 for (token = buf; token && *token; token = next) {
667 p = strchr(token, ',');
668 next = 0;
669 if (p) {
670 *p = 0;
671 next = p+1;
672 }
673 arg = strchr(token, '=');
674 if (arg) {
675 *arg = 0;
676 arg++;
677 }
678 if (strcmp(token, "stride") == 0) {
679 if (!arg) {
680 raid_usage++;
681 continue;
682 }
683 fs_stride = strtoul(arg, &p, 0);
684 if (*p || (fs_stride == 0)) {
685 fprintf(stderr, "Invalid stride parameter.\n");
686 raid_usage++;
687 continue;
688 }
689 } else
690 raid_usage++;
691 }
692 if (raid_usage) {
693 fprintf(stderr, "\nBad raid options specified.\n\n"
694 "Raid options are separated by commas, "
695 "and may take an argument which\n"
696 "\tis set off by an equals ('=') sign.\n\n"
697 "Valid raid options are:\n"
698 "\tstride=<stride length in blocks>\n\n");
699 exit(1);
700 }
701}
702
703
704
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705static void PRS(int argc, char *argv[])
706{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000707 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000708 int size;
709 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000710 blk_t max = 8192;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000711 int blocksize = 0;
712 int inode_ratio = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000713 int reserved_ratio = 5;
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000714 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000715 errcode_t retval;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000716 int sparse_option = 1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000717 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000718 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000719 char *raid_opts = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000720 char *fs_type = 0;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000721 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000722#ifdef linux
723 struct utsname ut;
724
725 if (uname(&ut)) {
726 perror("uname");
727 exit(1);
728 }
729 if (ut.release[0] == '2' && ut.release[1] == '.' &&
730 ut.release[2] < '2' && ut.release[3] == '.')
731 sparse_option = 0;
732#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000733 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000734 if (oldpath) {
735 char *newpath;
736
737 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
738 strcpy (newpath, PATH_SET);
739 strcat (newpath, ":");
740 strcat (newpath, oldpath);
741 putenv (newpath);
742 } else
743 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744
745 setbuf(stdout, NULL);
746 setbuf(stderr, NULL);
747 initialize_ext2_error_table();
748 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000749 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000750
751 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
752 E2FSPROGS_VERSION, E2FSPROGS_DATE,
753 EXT2FS_VERSION, EXT2FS_DATE);
754 if (argc && *argv)
755 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000756 while ((c = getopt (argc, argv,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000757 "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 +0000758 switch (c) {
759 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000760 blocksize = strtoul(optarg, &tmp, 0);
761 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000762 com_err(program_name, 0, "bad block size - %s",
763 optarg);
764 exit(1);
765 }
766 param.s_log_block_size =
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000767 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
768 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769 break;
770 case 'c':
771 case 't': /* Check for bad blocks */
772 cflag = 1;
773 break;
774 case 'f':
775 size = strtoul(optarg, &tmp, 0);
776 if (size < 1024 || size > 4096 || *tmp) {
777 com_err(program_name, 0, "bad fragment size - %s",
778 optarg);
779 exit(1);
780 }
781 param.s_log_frag_size =
782 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
783 printf("Warning: fragments not supported. "
784 "Ignoring -f option\n");
785 break;
786 case 'g':
787 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000788 if (*tmp) {
789 com_err(program_name, 0,
790 "Illegal number for blocks per group");
791 exit(1);
792 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000793 if ((param.s_blocks_per_group % 8) != 0) {
794 com_err(program_name, 0,
795 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796 exit(1);
797 }
798 break;
799 case 'i':
800 inode_ratio = strtoul(optarg, &tmp, 0);
801 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
802 *tmp) {
803 com_err(program_name, 0, "bad inode ratio - %s",
804 optarg);
805 exit(1);
806 }
807 break;
808 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000809 bad_blocks_filename = malloc(strlen(optarg)+1);
810 if (!bad_blocks_filename) {
811 com_err(program_name, ENOMEM,
812 "in malloc for bad_blocks_filename");
813 exit(1);
814 }
815 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000816 break;
817 case 'm':
818 reserved_ratio = strtoul(optarg, &tmp, 0);
819 if (reserved_ratio > 50 || *tmp) {
820 com_err(program_name, 0,
821 "bad reserved blocks percent - %s",
822 optarg);
823 exit(1);
824 }
825 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000826 case 'n':
827 noaction++;
828 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000829 case 'o':
830 creator_os = optarg;
831 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000832 case 'r':
833 param.s_rev_level = atoi(optarg);
834 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000835 case 's':
836 sparse_option = atoi(optarg);
837 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000838#ifdef EXT2_DYNAMIC_REV
839 case 'I':
840 param.s_inode_size = atoi(optarg);
841 break;
842#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000843 case 'N':
844 num_inodes = atoi(optarg);
845 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000846 case 'v':
847 verbose = 1;
848 break;
849 case 'q':
850 quiet = 1;
851 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000852 case 'F':
853 force = 1;
854 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000855 case 'L':
856 volume_label = optarg;
857 break;
858 case 'M':
859 mount_dir = optarg;
860 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000861 case 'R':
862 raid_opts = optarg;
863 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000864 case 'S':
865 super_only = 1;
866 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000867 case 'T':
868 fs_type = optarg;
869 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000870 case 'V':
871 /* Print version number and exit */
872 fprintf(stderr, "\tUsing %s\n",
873 error_message(EXT2_ET_BASE));
874 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000875 default:
876 usage();
877 }
878 if (optind == argc)
879 usage();
880 device_name = argv[optind];
881 optind++;
882 if (optind < argc) {
883 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
884 if (*tmp) {
885 com_err(program_name, 0, "bad blocks count - %s",
886 argv[optind - 1]);
887 exit(1);
888 }
889 }
890 if (optind < argc)
891 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000892
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000893 if (raid_opts)
894 parse_raid_opts(raid_opts);
895
Theodore Ts'o74becf31997-04-26 14:37:06 +0000896 if (!force)
897 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000898 check_mount();
899
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 param.s_log_frag_size = param.s_log_block_size;
901
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000902 if (noaction && param.s_blocks_count) {
903 dev_size = param.s_blocks_count;
904 retval = 0;
905 } else
906 retval = ext2fs_get_device_size(device_name,
907 EXT2_BLOCK_SIZE(&param),
908 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000909 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
910 com_err(program_name, retval,
911 "while trying to determine filesystem size");
912 exit(1);
913 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000914 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000915 if (retval == EXT2_ET_UNIMPLEMENTED) {
916 com_err(program_name, 0,
917 "Couldn't determine device size; you "
918 "must specify\nthe size of the "
919 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000920 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000921 } else
922 param.s_blocks_count = dev_size;
923 } else if (!force && (param.s_blocks_count > dev_size)) {
924 com_err(program_name, 0,
925 "Filesystem larger than apparent filesystem size.");
926 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000927 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000928
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000929 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
930
Theodore Ts'o521e3681997-04-29 17:48:10 +0000931 if (param.s_blocks_per_group) {
932 if (param.s_blocks_per_group < 256 ||
933 param.s_blocks_per_group > max || *tmp) {
934 com_err(program_name, 0,
935 "blocks per group count out of range");
936 exit(1);
937 }
938 }
939
Theodore Ts'o3839e651997-04-26 13:21:57 +0000940 /*
941 * Calculate number of inodes based on the inode ratio
942 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000943 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'of3db3561997-04-26 13:34:30 +0000944 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
945 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946
947 /*
948 * Calculate number of blocks to reserve
949 */
950 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000951
Theodore Ts'o521e3681997-04-29 17:48:10 +0000952#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000953 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000954 param_ext2->s_feature_ro_compat |=
955 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
956#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000957}
958
959int main (int argc, char *argv[])
960{
961 errcode_t retval = 0;
962 ext2_filsys fs;
963 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000964 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965
966 PRS(argc, argv);
967
Theodore Ts'o3839e651997-04-26 13:21:57 +0000968 /*
969 * Initialize the superblock....
970 */
971 retval = ext2fs_initialize(device_name, 0, &param,
972 unix_io_manager, &fs);
973 if (retval) {
974 com_err(device_name, retval, "while setting up superblock");
975 exit(1);
976 }
977
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000978 /*
979 * Generate a UUID for it...
980 */
981 s = (struct ext2fs_sb *) fs->super;
982 uuid_generate(s->s_uuid);
983
984 /*
985 * Override the creator OS, if applicable
986 */
987 if (creator_os && !set_os(fs->super, creator_os)) {
988 com_err (program_name, 0, "unknown os - %s", creator_os);
989 exit(1);
990 }
991
992 /*
993 * Set the volume label...
994 */
995 if (volume_label) {
996 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
997 strncpy(s->s_volume_name, volume_label,
998 sizeof(s->s_volume_name));
999 }
1000
1001 /*
1002 * Set the last mount directory
1003 */
1004 if (mount_dir) {
1005 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1006 strncpy(s->s_last_mounted, mount_dir,
1007 sizeof(s->s_last_mounted));
1008 }
1009
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001010 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011 show_stats(fs);
1012
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001013 if (noaction)
1014 exit(0);
1015
Theodore Ts'o3839e651997-04-26 13:21:57 +00001016 if (bad_blocks_filename)
1017 read_bb_file(fs, &bb_list, bad_blocks_filename);
1018 if (cflag)
1019 test_disk(fs, &bb_list);
1020
1021 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001022 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001023 retval = ext2fs_allocate_tables(fs);
1024 if (retval) {
1025 com_err(program_name, retval,
1026 "while trying to allocate filesystem tables");
1027 exit(1);
1028 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001029 if (super_only) {
1030 fs->super->s_state |= EXT2_ERROR_FS;
1031 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1032 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001033 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001034 create_root_dir(fs);
1035 create_lost_and_found(fs);
1036 reserve_inodes(fs);
1037 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001038#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001039 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001040#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001041 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001042
1043 if (!quiet)
1044 printf("Writing superblocks and "
1045 "filesystem accounting information: ");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001046 retval = ext2fs_flush(fs);
1047 if (retval) {
1048 printf("\nWarning, had trouble writing out superblocks.");
1049 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050 if (!quiet)
1051 printf("done\n");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001052 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001053 return 0;
1054}