blob: df0b878bfbf617a80c68f709065e03710620592c [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>
43
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000044#ifdef HAVE_LINUX_FS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <linux/fs.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000046#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000047#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000048#ifdef HAVE_LINUX_MAJOR_H
49#include <linux/major.h>
Theodore Ts'oe6597041999-10-26 02:30:16 +000050#include <sys/stat.h> /* Only need sys/stat.h for major nr test */
Theodore Ts'o74becf31997-04-26 14:37:06 +000051#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'o896938d1999-10-23 01:04:50 +000055#include "e2p/e2p.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'o6733c2f1999-11-23 02:23:30 +000064#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000065#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] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000097 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
98 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
99 "[-qvSV] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100 program_name);
101 exit(1);
102}
103
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000104static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105{
106 int l = 0;
107
108 arg >>= 1;
109 while (arg) {
110 l++;
111 arg >>= 1;
112 }
113 return l;
114}
115
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000116static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000117{
118 int l;
119
120 for (l=0; arg ; l++)
121 arg = arg / 10;
122 return l;
123}
124
Theodore Ts'oa789d841998-03-30 01:20:55 +0000125static void proceed_question(NOARGS)
126{
127 fflush(stdout);
128 fflush(stderr);
129 printf("Proceed anyway? (y,n) ");
130 if (getchar() != 'y')
131 exit(1);
132}
133
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000134#ifndef SCSI_BLK_MAJOR
135#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
136#endif
137
Theodore Ts'o74becf31997-04-26 14:37:06 +0000138static void check_plausibility(NOARGS)
139{
140#ifdef HAVE_LINUX_MAJOR_H
141 int val;
142 struct stat s;
143
144 val = stat(device_name, &s);
145
146 if(val == -1) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000147 fprintf(stderr, "Could not stat %s --- %s\n",
148 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000149 if (errno == ENOENT)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000150 fprintf(stderr, "\nThe device apparently does "
151 "not exist; did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000152 exit(1);
153 }
154 if(!S_ISBLK(s.st_mode)) {
155 printf("%s is not a block special device.\n", device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000156 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000157 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000158 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
159 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000160 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
161 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000162 printf("%s is entire device, not just one partition!\n",
163 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000164 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000165 }
166#endif
167}
168
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169static void check_mount(NOARGS)
170{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000171 errcode_t retval;
172 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000174 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
175 if (retval) {
176 com_err("ext2fs_check_if_mount", retval,
177 "while determining whether %s is mounted.",
178 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000180 }
181 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000183
184 fprintf(stderr, "%s is mounted; ", device_name);
185 if (force) {
186 fprintf(stderr, "mke2fs forced anyway. "
187 "Hope /etc/mtab is incorrect.\n");
188 } else {
189 fprintf(stderr, "will not make a filesystem here!\n");
190 exit(1);
191 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192}
193
194/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000195 * This function sets the default parameters for a filesystem
196 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000197 * The type is specified by the user. The size is the maximum size
198 * (in megabytes) for which a set of parameters applies, with a size
199 * of zero meaning that it is the default parameter for the type.
200 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000201 */
202static char default_str[] = "default";
203struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000204 const char *type;
205 int size;
206 int blocksize;
207 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000208} settings[] = {
209 { default_str, 0, 4096, 8192 },
210 { default_str, 512, 1024, 4096 },
211 { default_str, 3, 1024, 8192 },
212 { "news", 0, 4096, 4096 },
213 { 0, 0, 0, 0},
214};
215
Theodore Ts'o9094f281999-10-26 16:42:50 +0000216static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000217 int blocksize, int *inode_ratio)
218{
219 int megs;
220 int ratio = 0;
221 struct mke2fs_defaults *p;
222
Theodore Ts'o9094f281999-10-26 16:42:50 +0000223 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000224 1024);
225 if (inode_ratio)
226 ratio = *inode_ratio;
227 if (!fs_type)
228 fs_type = default_str;
229 for (p = settings; p->type; p++) {
230 if ((strcmp(p->type, fs_type) != 0) &&
231 (strcmp(p->type, default_str) != 0))
232 continue;
233 if ((p->size != 0) &&
234 (megs > p->size))
235 continue;
236 if (ratio == 0)
237 *inode_ratio = p->inode_ratio;
238 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000239 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000240 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000241 }
242 }
243 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000244 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000245}
246
247/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 * Helper function for read_bb_file and test_disk
249 */
250static void invalid_block(ext2_filsys fs, blk_t blk)
251{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000252 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253 return;
254}
255
256/*
257 * Reads the bad blocks list from a file
258 */
259static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
260 const char *bad_blocks_file)
261{
262 FILE *f;
263 errcode_t retval;
264
265 f = fopen(bad_blocks_file, "r");
266 if (!f) {
267 com_err("read_bad_blocks_file", errno,
268 "while trying to open %s", bad_blocks_file);
269 exit(1);
270 }
271 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
272 fclose (f);
273 if (retval) {
274 com_err("ext2fs_read_bb_FILE", retval,
275 "while reading in list of bad blocks from file");
276 exit(1);
277 }
278}
279
280/*
281 * Runs the badblocks program to test the disk
282 */
283static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
284{
285 FILE *f;
286 errcode_t retval;
287 char buf[1024];
288
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000289 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
290 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 fs->super->s_blocks_count);
292 if (verbose)
293 printf("Running command: %s\n", buf);
294 f = popen(buf, "r");
295 if (!f) {
296 com_err("popen", errno,
297 "while trying run '%s'", buf);
298 exit(1);
299 }
300 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000301 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302 if (retval) {
303 com_err("ext2fs_read_bb_FILE", retval,
304 "while processing list of bad blocks from program");
305 exit(1);
306 }
307}
308
309static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
310{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000311 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 int must_be_good;
313 blk_t blk;
314 badblocks_iterate bb_iter;
315 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000316 blk_t group_block;
317 int group;
318 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319
320 if (!bb_list)
321 return;
322
323 /*
324 * The primary superblock and group descriptors *must* be
325 * good; if not, abort.
326 */
327 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
328 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
329 if (badblocks_list_test(bb_list, i)) {
330 fprintf(stderr, "Block %d in primary superblock/group "
331 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000332 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000333 "in order to build a filesystem.\n",
334 fs->super->s_first_data_block, must_be_good);
335 fprintf(stderr, "Aborting....\n");
336 exit(1);
337 }
338 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000339
340 /*
341 * See if any of the bad blocks are showing up in the backup
342 * superblocks and/or group descriptors. If so, issue a
343 * warning and adjust the block counts appropriately.
344 */
345 group_block = fs->super->s_first_data_block +
346 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000347
348 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000349 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000350 for (j=0; j < fs->desc_blocks+1; j++) {
351 if (badblocks_list_test(bb_list, group_block +
352 j)) {
353 if (!group_bad)
354 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000355"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000356" bad blocks.\n\n",
357 group_block);
358 group_bad++;
359 group = ext2fs_group_of_blk(fs, group_block+j);
360 fs->group_desc[group].bg_free_blocks_count++;
361 fs->super->s_free_blocks_count++;
362 }
363 }
364 group_block += fs->super->s_blocks_per_group;
365 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366
367 /*
368 * Mark all the bad blocks as used...
369 */
370 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
371 if (retval) {
372 com_err("badblocks_list_iterate_begin", retval,
373 "while marking bad blocks as used");
374 exit(1);
375 }
376 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000377 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 badblocks_list_iterate_end(bb_iter);
379}
380
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000381static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382{
383 errcode_t retval;
384 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000385 int i, j, num, count;
386 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000387 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000388 int sync_kludge = 0;
389 char *mke2fs_sync;
390
391 mke2fs_sync = getenv("MKE2FS_SYNC");
392 if (mke2fs_sync)
393 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394
395 buf = malloc(fs->blocksize * STRIDE_LENGTH);
396 if (!buf) {
397 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
398 exit(1);
399 }
400 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000401
402 /*
403 * Figure out how many digits we need
404 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000405 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000406 sprintf(format, "%%%dd/%%%dld", i, i);
407 memset(backup, '\b', sizeof(backup)-1);
408 backup[sizeof(backup)-1] = 0;
409 if ((2*i)+1 < sizeof(backup))
410 backup[(2*i)+1] = 0;
411
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 if (!quiet)
413 printf("Writing inode tables: ");
414 for (i = 0; i < fs->group_desc_count; i++) {
415 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000416 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000417
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000418 blk = fs->group_desc[i].bg_inode_table;
419 num = fs->inode_blocks_per_group;
420
421 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
422 if (num-j > STRIDE_LENGTH)
423 count = STRIDE_LENGTH;
424 else
425 count = num - j;
426 retval = io_channel_write_blk(fs->io, blk, count, buf);
427 if (retval)
428 printf("Warning: could not write %d blocks "
429 "in inode table starting at %d: %s\n",
430 count, blk, error_message(retval));
431 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000433 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000434 if (sync_kludge) {
435 if (sync_kludge == 1)
436 sync();
437 else if ((i % sync_kludge) == 0)
438 sync();
439 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000441 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000443 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444}
445
446static void create_root_dir(ext2_filsys fs)
447{
448 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000449 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450
451 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
452 if (retval) {
453 com_err("ext2fs_mkdir", retval, "while creating root dir");
454 exit(1);
455 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000456 if (geteuid()) {
457 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
458 if (retval) {
459 com_err("ext2fs_read_inode", retval,
460 "while reading root inode");
461 exit(1);
462 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000463 inode.i_uid = getuid();
464 if (inode.i_uid)
465 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000466 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
467 if (retval) {
468 com_err("ext2fs_write_inode", retval,
469 "while setting root inode ownership");
470 exit(1);
471 }
472 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473}
474
475static void create_lost_and_found(ext2_filsys fs)
476{
477 errcode_t retval;
478 ino_t ino;
479 const char *name = "lost+found";
480 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000481 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482
483 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
484 if (retval) {
485 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
486 exit(1);
487 }
488
489 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
490 if (retval) {
491 com_err("ext2_lookup", retval, "while looking up /lost+found");
492 exit(1);
493 }
494
495 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000496 if ((lpf_size += fs->blocksize) >= 16*1024)
497 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 retval = ext2fs_expand_dir(fs, ino);
499 if (retval) {
500 com_err("ext2fs_expand_dir", retval,
501 "while expanding /lost+found");
502 exit(1);
503 }
504 }
505}
506
507static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
508{
509 errcode_t retval;
510
Theodore Ts'of3db3561997-04-26 13:34:30 +0000511 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 fs->group_desc[0].bg_free_inodes_count--;
513 fs->super->s_free_inodes_count--;
514 retval = ext2fs_update_bb_inode(fs, bb_list);
515 if (retval) {
516 com_err("ext2fs_update_bb_inode", retval,
517 "while setting bad block inode");
518 exit(1);
519 }
520
521}
522
523static void reserve_inodes(ext2_filsys fs)
524{
525 ino_t i;
526 int group;
527
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000528 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000529 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 group = ext2fs_group_of_ino(fs, i);
531 fs->group_desc[group].bg_free_inodes_count--;
532 fs->super->s_free_inodes_count--;
533 }
534 ext2fs_mark_ib_dirty(fs);
535}
536
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000537#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000538static void zap_bootblock(ext2_filsys fs)
539{
540 char buf[512];
541 int retval;
542
543 memset(buf, 0, 512);
544
545 retval = io_channel_write_blk(fs->io, 0, -512, buf);
546 if (retval)
547 printf("Warning: could not erase block 0: %s\n",
548 error_message(retval));
549}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000550#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000551
552
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553static void show_stats(ext2_filsys fs)
554{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000555 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
556 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000558 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559
560 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000561 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000563
564 memset(buf, 0, sizeof(buf));
565 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
566 printf("Filesystem label=%s\n", buf);
567 printf("OS type: ");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000568 switch (fs->super->s_creator_os) {
569 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000570 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000571 case EXT2_OS_MASIX: printf ("Masix"); break;
572 default: printf ("(unknown os)");
573 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000574 printf("\n");
575 printf("Block size=%u (log=%u)\n", fs->blocksize,
576 s->s_log_block_size);
577 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
578 s->s_log_frag_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000579 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000581 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582 s->s_r_blocks_count,
583 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000584 printf("First data block=%u\n", s->s_first_data_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 printf("%lu block group%s\n", fs->group_desc_count,
586 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000587 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000589 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590
591 if (fs->group_desc_count == 1) {
592 printf("\n");
593 return;
594 }
595
596 printf("Superblock backups stored on blocks: ");
597 group_block = s->s_first_data_block;
598 col_left = 0;
599 for (i = 1; i < fs->group_desc_count; i++) {
600 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000601 if (!ext2fs_bg_has_super(fs, i))
602 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000603 if (i != 1)
604 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000605 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000606 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000608 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000610 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000611 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612 }
613 printf("\n\n");
614}
615
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000616#ifndef HAVE_STRCASECMP
617static int strcasecmp (char *s1, char *s2)
618{
619 while (*s1 && *s2) {
620 int ch1 = *s1++, ch2 = *s2++;
621 if (isupper (ch1))
622 ch1 = tolower (ch1);
623 if (isupper (ch2))
624 ch2 = tolower (ch2);
625 if (ch1 != ch2)
626 return ch1 - ch2;
627 }
628 return *s1 ? 1 : *s2 ? -1 : 0;
629}
630#endif
631
632/*
633 * Set the S_CREATOR_OS field. Return true if OS is known,
634 * otherwise, 0.
635 */
636static int set_os(struct ext2_super_block *sb, char *os)
637{
638 if (isdigit (*os))
639 sb->s_creator_os = atoi (os);
640 else if (strcasecmp(os, "linux") == 0)
641 sb->s_creator_os = EXT2_OS_LINUX;
642 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
643 sb->s_creator_os = EXT2_OS_HURD;
644 else if (strcasecmp(os, "masix") == 0)
645 sb->s_creator_os = EXT2_OS_MASIX;
646 else
647 return 0;
648 return 1;
649}
650
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000651#define PATH_SET "PATH=/sbin"
652
Theodore Ts'od163b091997-10-03 17:42:28 +0000653static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000654{
655 char *buf, *token, *next, *p, *arg;
656 int len;
657 int raid_usage = 0;
658
659 len = strlen(opts);
660 buf = malloc(len+1);
661 if (!buf) {
662 fprintf(stderr, "Couldn't allocate memory to parse "
663 "raid options!\n");
664 exit(1);
665 }
666 strcpy(buf, opts);
667 for (token = buf; token && *token; token = next) {
668 p = strchr(token, ',');
669 next = 0;
670 if (p) {
671 *p = 0;
672 next = p+1;
673 }
674 arg = strchr(token, '=');
675 if (arg) {
676 *arg = 0;
677 arg++;
678 }
679 if (strcmp(token, "stride") == 0) {
680 if (!arg) {
681 raid_usage++;
682 continue;
683 }
684 fs_stride = strtoul(arg, &p, 0);
685 if (*p || (fs_stride == 0)) {
686 fprintf(stderr, "Invalid stride parameter.\n");
687 raid_usage++;
688 continue;
689 }
690 } else
691 raid_usage++;
692 }
693 if (raid_usage) {
694 fprintf(stderr, "\nBad raid options specified.\n\n"
695 "Raid options are separated by commas, "
696 "and may take an argument which\n"
697 "\tis set off by an equals ('=') sign.\n\n"
698 "Valid raid options are:\n"
699 "\tstride=<stride length in blocks>\n\n");
700 exit(1);
701 }
702}
703
Theodore Ts'o896938d1999-10-23 01:04:50 +0000704static __u32 ok_features[3] = {
705 0, /* Compat */
706 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
707 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
708};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000709
710
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711static void PRS(int argc, char *argv[])
712{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000713 int c;
714 int size;
715 char * tmp;
716 blk_t max = 8192;
717 int blocksize = 0;
718 int inode_ratio = 0;
719 int reserved_ratio = 5;
720 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000721 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000722 int sparse_option = 0;
723 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000724 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000725 char * raid_opts = 0;
726 char * fs_type = 0;
727 const char * feature_set = "filetype,sparse_super";
728 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000729#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000730 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000731
732 if (uname(&ut)) {
733 perror("uname");
734 exit(1);
735 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000736 if ((ut.release[0] == '1') ||
737 (ut.release[0] == '2' && ut.release[1] == '.' &&
738 ut.release[2] < '2' && ut.release[3] == '.'))
739 feature_set = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000740#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000741 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000742 if (oldpath) {
743 char *newpath;
744
745 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
746 strcpy (newpath, PATH_SET);
747 strcat (newpath, ":");
748 strcat (newpath, oldpath);
749 putenv (newpath);
750 } else
751 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752
753 setbuf(stdout, NULL);
754 setbuf(stderr, NULL);
755 initialize_ext2_error_table();
756 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000757 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758
759 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
760 E2FSPROGS_VERSION, E2FSPROGS_DATE,
761 EXT2FS_VERSION, EXT2FS_DATE);
762 if (argc && *argv)
763 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000764 while ((c = getopt (argc, argv,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000765 "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766 switch (c) {
767 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000768 blocksize = strtoul(optarg, &tmp, 0);
769 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000770 com_err(program_name, 0, "bad block size - %s",
771 optarg);
772 exit(1);
773 }
774 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000775 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000776 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000777 break;
778 case 'c':
779 case 't': /* Check for bad blocks */
780 cflag = 1;
781 break;
782 case 'f':
783 size = strtoul(optarg, &tmp, 0);
784 if (size < 1024 || size > 4096 || *tmp) {
785 com_err(program_name, 0, "bad fragment size - %s",
786 optarg);
787 exit(1);
788 }
789 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000790 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000791 printf("Warning: fragments not supported. "
792 "Ignoring -f option\n");
793 break;
794 case 'g':
795 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000796 if (*tmp) {
797 com_err(program_name, 0,
798 "Illegal number for blocks per group");
799 exit(1);
800 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000801 if ((param.s_blocks_per_group % 8) != 0) {
802 com_err(program_name, 0,
803 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000804 exit(1);
805 }
806 break;
807 case 'i':
808 inode_ratio = strtoul(optarg, &tmp, 0);
809 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
810 *tmp) {
811 com_err(program_name, 0, "bad inode ratio - %s",
812 optarg);
813 exit(1);
814 }
815 break;
816 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000817 bad_blocks_filename = malloc(strlen(optarg)+1);
818 if (!bad_blocks_filename) {
819 com_err(program_name, ENOMEM,
820 "in malloc for bad_blocks_filename");
821 exit(1);
822 }
823 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000824 break;
825 case 'm':
826 reserved_ratio = strtoul(optarg, &tmp, 0);
827 if (reserved_ratio > 50 || *tmp) {
828 com_err(program_name, 0,
829 "bad reserved blocks percent - %s",
830 optarg);
831 exit(1);
832 }
833 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000834 case 'n':
835 noaction++;
836 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000837 case 'o':
838 creator_os = optarg;
839 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000840 case 'r':
841 param.s_rev_level = atoi(optarg);
842 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000843 case 's':
844 sparse_option = atoi(optarg);
845 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000846#ifdef EXT2_DYNAMIC_REV
847 case 'I':
848 param.s_inode_size = atoi(optarg);
849 break;
850#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000851 case 'N':
852 num_inodes = atoi(optarg);
853 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000854 case 'v':
855 verbose = 1;
856 break;
857 case 'q':
858 quiet = 1;
859 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000860 case 'F':
861 force = 1;
862 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000863 case 'L':
864 volume_label = optarg;
865 break;
866 case 'M':
867 mount_dir = optarg;
868 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000869 case 'O':
870 feature_set = optarg;
871 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000872 case 'R':
873 raid_opts = optarg;
874 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000875 case 'S':
876 super_only = 1;
877 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000878 case 'T':
879 fs_type = optarg;
880 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000881 case 'V':
882 /* Print version number and exit */
883 fprintf(stderr, "\tUsing %s\n",
884 error_message(EXT2_ET_BASE));
885 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 default:
887 usage();
888 }
889 if (optind == argc)
890 usage();
891 device_name = argv[optind];
892 optind++;
893 if (optind < argc) {
894 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
895 if (*tmp) {
896 com_err(program_name, 0, "bad blocks count - %s",
897 argv[optind - 1]);
898 exit(1);
899 }
900 }
901 if (optind < argc)
902 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000903
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000904 if (raid_opts)
905 parse_raid_opts(raid_opts);
906
Theodore Ts'o74becf31997-04-26 14:37:06 +0000907 if (!force)
908 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000909 check_mount();
910
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 param.s_log_frag_size = param.s_log_block_size;
912
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000913 if (noaction && param.s_blocks_count) {
914 dev_size = param.s_blocks_count;
915 retval = 0;
916 } else
917 retval = ext2fs_get_device_size(device_name,
918 EXT2_BLOCK_SIZE(&param),
919 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000920 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
921 com_err(program_name, retval,
922 "while trying to determine filesystem size");
923 exit(1);
924 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000925 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000926 if (retval == EXT2_ET_UNIMPLEMENTED) {
927 com_err(program_name, 0,
928 "Couldn't determine device size; you "
929 "must specify\nthe size of the "
930 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000931 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000932 } else
933 param.s_blocks_count = dev_size;
934 } else if (!force && (param.s_blocks_count > dev_size)) {
935 com_err(program_name, 0,
936 "Filesystem larger than apparent filesystem size.");
937 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000938 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000940 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
941
Theodore Ts'o521e3681997-04-29 17:48:10 +0000942 if (param.s_blocks_per_group) {
943 if (param.s_blocks_per_group < 256 ||
944 param.s_blocks_per_group > max || *tmp) {
945 com_err(program_name, 0,
946 "blocks per group count out of range");
947 exit(1);
948 }
949 }
950
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951 /*
952 * Calculate number of inodes based on the inode ratio
953 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000954 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +0000955 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000956 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000957
958 /*
959 * Calculate number of blocks to reserve
960 */
961 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000962
Theodore Ts'o521e3681997-04-29 17:48:10 +0000963#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000964 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000965 param_ext2->s_feature_ro_compat |=
966 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
967#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +0000968 if (feature_set && !strncasecmp(feature_set, "none", 4))
969 feature_set = 0;
970 if (feature_set && e2p_edit_feature(feature_set,
971 &param_ext2->s_feature_compat,
972 ok_features)) {
973 fprintf(stderr, "Invalid filesystem option set: %s\n",
974 feature_set);
975 exit(1);
976 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000977}
978
979int main (int argc, char *argv[])
980{
981 errcode_t retval = 0;
982 ext2_filsys fs;
983 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000984 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985
986 PRS(argc, argv);
987
Theodore Ts'o3839e651997-04-26 13:21:57 +0000988 /*
989 * Initialize the superblock....
990 */
991 retval = ext2fs_initialize(device_name, 0, &param,
992 unix_io_manager, &fs);
993 if (retval) {
994 com_err(device_name, retval, "while setting up superblock");
995 exit(1);
996 }
997
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000998 /*
999 * Generate a UUID for it...
1000 */
1001 s = (struct ext2fs_sb *) fs->super;
1002 uuid_generate(s->s_uuid);
1003
1004 /*
1005 * Override the creator OS, if applicable
1006 */
1007 if (creator_os && !set_os(fs->super, creator_os)) {
1008 com_err (program_name, 0, "unknown os - %s", creator_os);
1009 exit(1);
1010 }
1011
1012 /*
1013 * Set the volume label...
1014 */
1015 if (volume_label) {
1016 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1017 strncpy(s->s_volume_name, volume_label,
1018 sizeof(s->s_volume_name));
1019 }
1020
1021 /*
1022 * Set the last mount directory
1023 */
1024 if (mount_dir) {
1025 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1026 strncpy(s->s_last_mounted, mount_dir,
1027 sizeof(s->s_last_mounted));
1028 }
1029
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001030 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001031 show_stats(fs);
1032
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001033 if (noaction)
1034 exit(0);
1035
Theodore Ts'o3839e651997-04-26 13:21:57 +00001036 if (bad_blocks_filename)
1037 read_bb_file(fs, &bb_list, bad_blocks_filename);
1038 if (cflag)
1039 test_disk(fs, &bb_list);
1040
1041 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001042 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001043 retval = ext2fs_allocate_tables(fs);
1044 if (retval) {
1045 com_err(program_name, retval,
1046 "while trying to allocate filesystem tables");
1047 exit(1);
1048 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001049 if (super_only) {
1050 fs->super->s_state |= EXT2_ERROR_FS;
1051 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1052 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001053 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001054 create_root_dir(fs);
1055 create_lost_and_found(fs);
1056 reserve_inodes(fs);
1057 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001058#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001059 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001060#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001061 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001062
1063 if (!quiet)
1064 printf("Writing superblocks and "
1065 "filesystem accounting information: ");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001066 retval = ext2fs_flush(fs);
1067 if (retval) {
1068 printf("\nWarning, had trouble writing out superblocks.");
1069 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001070 if (!quiet)
1071 printf("done\n");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001072 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001073 return 0;
1074}