blob: bf604e4aab0f7e31dca84933acdec19b7ac8fe0f [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'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'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'o76714331999-10-20 18:06:29 +0000602 if (i != 1)
603 printf(", ");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000604 need = log10(group_block) + 2;
605 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000607 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000609 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000610 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611 }
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
Theodore Ts'o896938d1999-10-23 01:04:50 +0000703static __u32 ok_features[3] = {
704 0, /* Compat */
705 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
706 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
707};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000708
709
Theodore Ts'o3839e651997-04-26 13:21:57 +0000710static void PRS(int argc, char *argv[])
711{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000712 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000713 int size;
714 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000715 blk_t max = 8192;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000716 int blocksize = 0;
717 int inode_ratio = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000718 int reserved_ratio = 5;
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000719 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000720 errcode_t retval;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000721 int sparse_option = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000722 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000723 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000724 char *raid_opts = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000725 char *fs_type = 0;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000726 char *feature_set = "filetype,sparse_super";
Theodore Ts'oa789d841998-03-30 01:20:55 +0000727 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000728#ifdef linux
729 struct utsname ut;
730
731 if (uname(&ut)) {
732 perror("uname");
733 exit(1);
734 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000735 if ((ut.release[0] == '1') ||
736 (ut.release[0] == '2' && ut.release[1] == '.' &&
737 ut.release[2] < '2' && ut.release[3] == '.'))
738 feature_set = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000739#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000740 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000741 if (oldpath) {
742 char *newpath;
743
744 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
745 strcpy (newpath, PATH_SET);
746 strcat (newpath, ":");
747 strcat (newpath, oldpath);
748 putenv (newpath);
749 } else
750 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000751
752 setbuf(stdout, NULL);
753 setbuf(stderr, NULL);
754 initialize_ext2_error_table();
755 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000756 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757
758 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
759 E2FSPROGS_VERSION, E2FSPROGS_DATE,
760 EXT2FS_VERSION, EXT2FS_DATE);
761 if (argc && *argv)
762 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000763 while ((c = getopt (argc, argv,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000764 "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 +0000765 switch (c) {
766 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000767 blocksize = strtoul(optarg, &tmp, 0);
768 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769 com_err(program_name, 0, "bad block size - %s",
770 optarg);
771 exit(1);
772 }
773 param.s_log_block_size =
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000774 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
775 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000776 break;
777 case 'c':
778 case 't': /* Check for bad blocks */
779 cflag = 1;
780 break;
781 case 'f':
782 size = strtoul(optarg, &tmp, 0);
783 if (size < 1024 || size > 4096 || *tmp) {
784 com_err(program_name, 0, "bad fragment size - %s",
785 optarg);
786 exit(1);
787 }
788 param.s_log_frag_size =
789 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
790 printf("Warning: fragments not supported. "
791 "Ignoring -f option\n");
792 break;
793 case 'g':
794 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000795 if (*tmp) {
796 com_err(program_name, 0,
797 "Illegal number for blocks per group");
798 exit(1);
799 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000800 if ((param.s_blocks_per_group % 8) != 0) {
801 com_err(program_name, 0,
802 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000803 exit(1);
804 }
805 break;
806 case 'i':
807 inode_ratio = strtoul(optarg, &tmp, 0);
808 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
809 *tmp) {
810 com_err(program_name, 0, "bad inode ratio - %s",
811 optarg);
812 exit(1);
813 }
814 break;
815 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000816 bad_blocks_filename = malloc(strlen(optarg)+1);
817 if (!bad_blocks_filename) {
818 com_err(program_name, ENOMEM,
819 "in malloc for bad_blocks_filename");
820 exit(1);
821 }
822 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 break;
824 case 'm':
825 reserved_ratio = strtoul(optarg, &tmp, 0);
826 if (reserved_ratio > 50 || *tmp) {
827 com_err(program_name, 0,
828 "bad reserved blocks percent - %s",
829 optarg);
830 exit(1);
831 }
832 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000833 case 'n':
834 noaction++;
835 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000836 case 'o':
837 creator_os = optarg;
838 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000839 case 'r':
840 param.s_rev_level = atoi(optarg);
841 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000842 case 's':
843 sparse_option = atoi(optarg);
844 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000845#ifdef EXT2_DYNAMIC_REV
846 case 'I':
847 param.s_inode_size = atoi(optarg);
848 break;
849#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000850 case 'N':
851 num_inodes = atoi(optarg);
852 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000853 case 'v':
854 verbose = 1;
855 break;
856 case 'q':
857 quiet = 1;
858 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000859 case 'F':
860 force = 1;
861 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000862 case 'L':
863 volume_label = optarg;
864 break;
865 case 'M':
866 mount_dir = optarg;
867 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000868 case 'O':
869 feature_set = optarg;
870 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000871 case 'R':
872 raid_opts = optarg;
873 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000874 case 'S':
875 super_only = 1;
876 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000877 case 'T':
878 fs_type = optarg;
879 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000880 case 'V':
881 /* Print version number and exit */
882 fprintf(stderr, "\tUsing %s\n",
883 error_message(EXT2_ET_BASE));
884 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000885 default:
886 usage();
887 }
888 if (optind == argc)
889 usage();
890 device_name = argv[optind];
891 optind++;
892 if (optind < argc) {
893 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
894 if (*tmp) {
895 com_err(program_name, 0, "bad blocks count - %s",
896 argv[optind - 1]);
897 exit(1);
898 }
899 }
900 if (optind < argc)
901 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000902
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000903 if (raid_opts)
904 parse_raid_opts(raid_opts);
905
Theodore Ts'o74becf31997-04-26 14:37:06 +0000906 if (!force)
907 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000908 check_mount();
909
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910 param.s_log_frag_size = param.s_log_block_size;
911
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000912 if (noaction && param.s_blocks_count) {
913 dev_size = param.s_blocks_count;
914 retval = 0;
915 } else
916 retval = ext2fs_get_device_size(device_name,
917 EXT2_BLOCK_SIZE(&param),
918 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000919 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
920 com_err(program_name, retval,
921 "while trying to determine filesystem size");
922 exit(1);
923 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000924 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000925 if (retval == EXT2_ET_UNIMPLEMENTED) {
926 com_err(program_name, 0,
927 "Couldn't determine device size; you "
928 "must specify\nthe size of the "
929 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000930 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000931 } else
932 param.s_blocks_count = dev_size;
933 } else if (!force && (param.s_blocks_count > dev_size)) {
934 com_err(program_name, 0,
935 "Filesystem larger than apparent filesystem size.");
936 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000937 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000938
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000939 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
940
Theodore Ts'o521e3681997-04-29 17:48:10 +0000941 if (param.s_blocks_per_group) {
942 if (param.s_blocks_per_group < 256 ||
943 param.s_blocks_per_group > max || *tmp) {
944 com_err(program_name, 0,
945 "blocks per group count out of range");
946 exit(1);
947 }
948 }
949
Theodore Ts'o3839e651997-04-26 13:21:57 +0000950 /*
951 * Calculate number of inodes based on the inode ratio
952 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000953 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'of3db3561997-04-26 13:34:30 +0000954 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
955 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956
957 /*
958 * Calculate number of blocks to reserve
959 */
960 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000961
Theodore Ts'o521e3681997-04-29 17:48:10 +0000962#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000963 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000964 param_ext2->s_feature_ro_compat |=
965 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
966#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +0000967 if (feature_set && !strncasecmp(feature_set, "none", 4))
968 feature_set = 0;
969 if (feature_set && e2p_edit_feature(feature_set,
970 &param_ext2->s_feature_compat,
971 ok_features)) {
972 fprintf(stderr, "Invalid filesystem option set: %s\n",
973 feature_set);
974 exit(1);
975 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976}
977
978int main (int argc, char *argv[])
979{
980 errcode_t retval = 0;
981 ext2_filsys fs;
982 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000983 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000984
985 PRS(argc, argv);
986
Theodore Ts'o3839e651997-04-26 13:21:57 +0000987 /*
988 * Initialize the superblock....
989 */
990 retval = ext2fs_initialize(device_name, 0, &param,
991 unix_io_manager, &fs);
992 if (retval) {
993 com_err(device_name, retval, "while setting up superblock");
994 exit(1);
995 }
996
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000997 /*
998 * Generate a UUID for it...
999 */
1000 s = (struct ext2fs_sb *) fs->super;
1001 uuid_generate(s->s_uuid);
1002
1003 /*
1004 * Override the creator OS, if applicable
1005 */
1006 if (creator_os && !set_os(fs->super, creator_os)) {
1007 com_err (program_name, 0, "unknown os - %s", creator_os);
1008 exit(1);
1009 }
1010
1011 /*
1012 * Set the volume label...
1013 */
1014 if (volume_label) {
1015 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1016 strncpy(s->s_volume_name, volume_label,
1017 sizeof(s->s_volume_name));
1018 }
1019
1020 /*
1021 * Set the last mount directory
1022 */
1023 if (mount_dir) {
1024 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1025 strncpy(s->s_last_mounted, mount_dir,
1026 sizeof(s->s_last_mounted));
1027 }
1028
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001029 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001030 show_stats(fs);
1031
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001032 if (noaction)
1033 exit(0);
1034
Theodore Ts'o3839e651997-04-26 13:21:57 +00001035 if (bad_blocks_filename)
1036 read_bb_file(fs, &bb_list, bad_blocks_filename);
1037 if (cflag)
1038 test_disk(fs, &bb_list);
1039
1040 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001041 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001042 retval = ext2fs_allocate_tables(fs);
1043 if (retval) {
1044 com_err(program_name, retval,
1045 "while trying to allocate filesystem tables");
1046 exit(1);
1047 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001048 if (super_only) {
1049 fs->super->s_state |= EXT2_ERROR_FS;
1050 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1051 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001052 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001053 create_root_dir(fs);
1054 create_lost_and_found(fs);
1055 reserve_inodes(fs);
1056 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001057#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001058 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001059#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001060 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001061
1062 if (!quiet)
1063 printf("Writing superblocks and "
1064 "filesystem accounting information: ");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001065 retval = ext2fs_flush(fs);
1066 if (retval) {
1067 printf("\nWarning, had trouble writing out superblocks.");
1068 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001069 if (!quiet)
1070 printf("done\n");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001071 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001072 return 0;
1073}