blob: 9b9bd319ebbe46763817b4f9c19a90fc45811479 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000023#ifdef linux
24#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Theodore Ts'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"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000058#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000060/* Everything is STDC, these days */
61#define NOARGS void
62
Theodore Ts'o3839e651997-04-26 13:21:57 +000063#define STRIDE_LENGTH 8
64
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000065#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000066#define ZAP_BOOTBLOCK
67#endif
68
Theodore Ts'o3839e651997-04-26 13:21:57 +000069extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000070extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
72const char * program_name = "mke2fs";
73const char * device_name = NULL;
74
75/* Command line options */
76int cflag = 0;
77int verbose = 0;
78int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000079int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000080int force = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +000081int noaction = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000083__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
85struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000086char *creator_os = NULL;
87char *volume_label = NULL;
88char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000089
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000090static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
91
Theodore Ts'o3839e651997-04-26 13:21:57 +000092static void usage(NOARGS)
93{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000094 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000095 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000096 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
97 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000098 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
99 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000100 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 program_name);
102 exit(1);
103}
104
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000105static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106{
107 int l = 0;
108
109 arg >>= 1;
110 while (arg) {
111 l++;
112 arg >>= 1;
113 }
114 return l;
115}
116
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000117static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000118{
119 int l;
120
121 for (l=0; arg ; l++)
122 arg = arg / 10;
123 return l;
124}
125
Theodore Ts'oa789d841998-03-30 01:20:55 +0000126static void proceed_question(NOARGS)
127{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000128 int c;
129 char *short_yes = _("yY");
130
Theodore Ts'oa789d841998-03-30 01:20:55 +0000131 fflush(stdout);
132 fflush(stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000133 printf(_("Proceed anyway? (y,n) "));
134 c = getchar();
135 if (strchr(short_yes, (char) c))
Theodore Ts'oa789d841998-03-30 01:20:55 +0000136 exit(1);
137}
138
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000139#ifndef SCSI_BLK_MAJOR
140#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
141#endif
142
Theodore Ts'o74becf31997-04-26 14:37:06 +0000143static void check_plausibility(NOARGS)
144{
145#ifdef HAVE_LINUX_MAJOR_H
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000146#ifndef MAJOR
147#define MAJOR(dev) ((dev)>>8)
148#define MINOR(dev) ((dev) & 0xff)
149#endif
150
Theodore Ts'o74becf31997-04-26 14:37:06 +0000151 int val;
152 struct stat s;
153
154 val = stat(device_name, &s);
155
156 if(val == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000157 fprintf(stderr, _("Could not stat %s --- %s\n"),
Theodore Ts'oa789d841998-03-30 01:20:55 +0000158 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000159 if (errno == ENOENT)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000160 fprintf(stderr, _("\nThe device apparently does "
161 "not exist; did you specify it correctly?\n"));
Theodore Ts'o74becf31997-04-26 14:37:06 +0000162 exit(1);
163 }
164 if(!S_ISBLK(s.st_mode)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000165 printf(_("%s is not a block special device.\n"), device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000166 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000167 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000168 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
169 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000170 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
171 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000172 printf(_("%s is entire device, not just one partition!\n"),
Theodore Ts'o74becf31997-04-26 14:37:06 +0000173 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000174 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000175 }
176#endif
177}
178
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179static void check_mount(NOARGS)
180{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000181 errcode_t retval;
182 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000184 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
185 if (retval) {
186 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000187 _("while determining whether %s is mounted."),
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000188 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000190 }
191 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000193
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000194 fprintf(stderr, _("%s is mounted; "), device_name);
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000195 if (force) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000196 fprintf(stderr, _("mke2fs forced anyway. "
197 "Hope /etc/mtab is incorrect.\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000198 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000199 fprintf(stderr, _("will not make a filesystem here!\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000200 exit(1);
201 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202}
203
204/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000205 * This function sets the default parameters for a filesystem
206 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000207 * The type is specified by the user. The size is the maximum size
208 * (in megabytes) for which a set of parameters applies, with a size
209 * of zero meaning that it is the default parameter for the type.
210 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000211 */
212static char default_str[] = "default";
213struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000214 const char *type;
215 int size;
216 int blocksize;
217 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000218} settings[] = {
219 { default_str, 0, 4096, 8192 },
220 { default_str, 512, 1024, 4096 },
221 { default_str, 3, 1024, 8192 },
222 { "news", 0, 4096, 4096 },
223 { 0, 0, 0, 0},
224};
225
Theodore Ts'o9094f281999-10-26 16:42:50 +0000226static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000227 int blocksize, int *inode_ratio)
228{
229 int megs;
230 int ratio = 0;
231 struct mke2fs_defaults *p;
232
Theodore Ts'o9094f281999-10-26 16:42:50 +0000233 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000234 1024);
235 if (inode_ratio)
236 ratio = *inode_ratio;
237 if (!fs_type)
238 fs_type = default_str;
239 for (p = settings; p->type; p++) {
240 if ((strcmp(p->type, fs_type) != 0) &&
241 (strcmp(p->type, default_str) != 0))
242 continue;
243 if ((p->size != 0) &&
244 (megs > p->size))
245 continue;
246 if (ratio == 0)
247 *inode_ratio = p->inode_ratio;
248 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000249 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000250 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000251 }
252 }
253 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000254 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000255}
256
257/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258 * Helper function for read_bb_file and test_disk
259 */
260static void invalid_block(ext2_filsys fs, blk_t blk)
261{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000262 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 return;
264}
265
266/*
267 * Reads the bad blocks list from a file
268 */
269static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
270 const char *bad_blocks_file)
271{
272 FILE *f;
273 errcode_t retval;
274
275 f = fopen(bad_blocks_file, "r");
276 if (!f) {
277 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000278 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 exit(1);
280 }
281 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
282 fclose (f);
283 if (retval) {
284 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000285 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 exit(1);
287 }
288}
289
290/*
291 * Runs the badblocks program to test the disk
292 */
293static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
294{
295 FILE *f;
296 errcode_t retval;
297 char buf[1024];
298
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000299 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
300 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 fs->super->s_blocks_count);
302 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000303 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304 f = popen(buf, "r");
305 if (!f) {
306 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000307 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 exit(1);
309 }
310 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000311 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 if (retval) {
313 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000314 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 exit(1);
316 }
317}
318
319static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
320{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000321 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322 int must_be_good;
323 blk_t blk;
324 badblocks_iterate bb_iter;
325 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000326 blk_t group_block;
327 int group;
328 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329
330 if (!bb_list)
331 return;
332
333 /*
334 * The primary superblock and group descriptors *must* be
335 * good; if not, abort.
336 */
337 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
338 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
339 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000340 fprintf(stderr, _("Block %d in primary "
341 "superblock/group descriptor area bad.\n"), i);
342 fprintf(stderr, _("Blocks %d through %d must be good "
343 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000345 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 exit(1);
347 }
348 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000349
350 /*
351 * See if any of the bad blocks are showing up in the backup
352 * superblocks and/or group descriptors. If so, issue a
353 * warning and adjust the block counts appropriately.
354 */
355 group_block = fs->super->s_first_data_block +
356 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000357
358 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000359 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000360 for (j=0; j < fs->desc_blocks+1; j++) {
361 if (badblocks_list_test(bb_list, group_block +
362 j)) {
363 if (!group_bad)
364 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000365_("Warning: the backup superblock/group descriptors at block %d contain\n"
366" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000367 group_block);
368 group_bad++;
369 group = ext2fs_group_of_blk(fs, group_block+j);
370 fs->group_desc[group].bg_free_blocks_count++;
371 fs->super->s_free_blocks_count++;
372 }
373 }
374 group_block += fs->super->s_blocks_per_group;
375 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376
377 /*
378 * Mark all the bad blocks as used...
379 */
380 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
381 if (retval) {
382 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000383 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 exit(1);
385 }
386 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000387 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 badblocks_list_iterate_end(bb_iter);
389}
390
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000391static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392{
393 errcode_t retval;
394 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000395 int i, j, num, count;
396 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000397 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000398 int sync_kludge = 0;
399 char *mke2fs_sync;
400
401 mke2fs_sync = getenv("MKE2FS_SYNC");
402 if (mke2fs_sync)
403 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000404
405 buf = malloc(fs->blocksize * STRIDE_LENGTH);
406 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000407 com_err("malloc", ENOMEM,
408 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409 exit(1);
410 }
411 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000412
413 /*
414 * Figure out how many digits we need
415 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000416 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000417 sprintf(format, "%%%dd/%%%dld", i, i);
418 memset(backup, '\b', sizeof(backup)-1);
419 backup[sizeof(backup)-1] = 0;
420 if ((2*i)+1 < sizeof(backup))
421 backup[(2*i)+1] = 0;
422
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000424 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425 for (i = 0; i < fs->group_desc_count; i++) {
426 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000427 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000429 blk = fs->group_desc[i].bg_inode_table;
430 num = fs->inode_blocks_per_group;
431
432 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
433 if (num-j > STRIDE_LENGTH)
434 count = STRIDE_LENGTH;
435 else
436 count = num - j;
437 retval = io_channel_write_blk(fs->io, blk, count, buf);
438 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000439 printf(_("Warning: could not write %d blocks "
440 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000441 count, blk, error_message(retval));
442 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000444 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000445 if (sync_kludge) {
446 if (sync_kludge == 1)
447 sync();
448 else if ((i % sync_kludge) == 0)
449 sync();
450 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000452 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000454 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455}
456
457static void create_root_dir(ext2_filsys fs)
458{
459 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000460 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461
462 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
463 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000464 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 exit(1);
466 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000467 if (geteuid()) {
468 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
469 if (retval) {
470 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000471 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000472 exit(1);
473 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000474 inode.i_uid = getuid();
475 if (inode.i_uid)
476 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
478 if (retval) {
479 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000480 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000481 exit(1);
482 }
483 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484}
485
486static void create_lost_and_found(ext2_filsys fs)
487{
488 errcode_t retval;
489 ino_t ino;
490 const char *name = "lost+found";
491 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000492 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493
494 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
495 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000496 com_err("ext2fs_mkdir", retval,
497 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 exit(1);
499 }
500
501 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
502 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000503 com_err("ext2_lookup", retval,
504 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 exit(1);
506 }
507
508 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000509 if ((lpf_size += fs->blocksize) >= 16*1024)
510 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 retval = ext2fs_expand_dir(fs, ino);
512 if (retval) {
513 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000514 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 exit(1);
516 }
517 }
518}
519
520static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
521{
522 errcode_t retval;
523
Theodore Ts'of3db3561997-04-26 13:34:30 +0000524 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 fs->group_desc[0].bg_free_inodes_count--;
526 fs->super->s_free_inodes_count--;
527 retval = ext2fs_update_bb_inode(fs, bb_list);
528 if (retval) {
529 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000530 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531 exit(1);
532 }
533
534}
535
536static void reserve_inodes(ext2_filsys fs)
537{
538 ino_t i;
539 int group;
540
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000541 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000542 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 group = ext2fs_group_of_ino(fs, i);
544 fs->group_desc[group].bg_free_inodes_count--;
545 fs->super->s_free_inodes_count--;
546 }
547 ext2fs_mark_ib_dirty(fs);
548}
549
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000550#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000551static void zap_bootblock(ext2_filsys fs)
552{
553 char buf[512];
554 int retval;
555
556 memset(buf, 0, 512);
557
558 retval = io_channel_write_blk(fs->io, 0, -512, buf);
559 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000560 printf(_("Warning: could not erase block 0: %s\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000561 error_message(retval));
562}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000563#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000564
565
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566static void show_stats(ext2_filsys fs)
567{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000568 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
569 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000571 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572
573 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000574 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000576
577 memset(buf, 0, sizeof(buf));
578 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000579 printf(_("Filesystem label=%s\n"), buf);
580 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000581 switch (fs->super->s_creator_os) {
582 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000583 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000584 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000585 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000586 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000587 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000588 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000589 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000590 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000591 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000592 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000594 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595 s->s_r_blocks_count,
596 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000597 printf(_("First data block=%u\n"), s->s_first_data_block);
598 if (fs->group_desc_count > 1)
599 printf(_("%lu block groups\n"), fs->group_desc_count);
600 else
601 printf(_("%lu block group\n"), fs->group_desc_count);
602 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000604 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605
606 if (fs->group_desc_count == 1) {
607 printf("\n");
608 return;
609 }
610
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000611 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612 group_block = s->s_first_data_block;
613 col_left = 0;
614 for (i = 1; i < fs->group_desc_count; i++) {
615 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000616 if (!ext2fs_bg_has_super(fs, i))
617 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000618 if (i != 1)
619 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000620 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000621 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000623 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000625 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000626 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 }
628 printf("\n\n");
629}
630
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000631#ifndef HAVE_STRCASECMP
632static int strcasecmp (char *s1, char *s2)
633{
634 while (*s1 && *s2) {
635 int ch1 = *s1++, ch2 = *s2++;
636 if (isupper (ch1))
637 ch1 = tolower (ch1);
638 if (isupper (ch2))
639 ch2 = tolower (ch2);
640 if (ch1 != ch2)
641 return ch1 - ch2;
642 }
643 return *s1 ? 1 : *s2 ? -1 : 0;
644}
645#endif
646
647/*
648 * Set the S_CREATOR_OS field. Return true if OS is known,
649 * otherwise, 0.
650 */
651static int set_os(struct ext2_super_block *sb, char *os)
652{
653 if (isdigit (*os))
654 sb->s_creator_os = atoi (os);
655 else if (strcasecmp(os, "linux") == 0)
656 sb->s_creator_os = EXT2_OS_LINUX;
657 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
658 sb->s_creator_os = EXT2_OS_HURD;
659 else if (strcasecmp(os, "masix") == 0)
660 sb->s_creator_os = EXT2_OS_MASIX;
661 else
662 return 0;
663 return 1;
664}
665
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000666#define PATH_SET "PATH=/sbin"
667
Theodore Ts'od163b091997-10-03 17:42:28 +0000668static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000669{
670 char *buf, *token, *next, *p, *arg;
671 int len;
672 int raid_usage = 0;
673
674 len = strlen(opts);
675 buf = malloc(len+1);
676 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000677 fprintf(stderr, _("Couldn't allocate memory to parse "
678 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000679 exit(1);
680 }
681 strcpy(buf, opts);
682 for (token = buf; token && *token; token = next) {
683 p = strchr(token, ',');
684 next = 0;
685 if (p) {
686 *p = 0;
687 next = p+1;
688 }
689 arg = strchr(token, '=');
690 if (arg) {
691 *arg = 0;
692 arg++;
693 }
694 if (strcmp(token, "stride") == 0) {
695 if (!arg) {
696 raid_usage++;
697 continue;
698 }
699 fs_stride = strtoul(arg, &p, 0);
700 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000701 fprintf(stderr,
702 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000703 raid_usage++;
704 continue;
705 }
706 } else
707 raid_usage++;
708 }
709 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000710 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000711 "Raid options are separated by commas, "
712 "and may take an argument which\n"
713 "\tis set off by an equals ('=') sign.\n\n"
714 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000715 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000716 exit(1);
717 }
718}
719
Theodore Ts'o896938d1999-10-23 01:04:50 +0000720static __u32 ok_features[3] = {
721 0, /* Compat */
722 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
723 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
724};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000725
726
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727static void PRS(int argc, char *argv[])
728{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000729 int c;
730 int size;
731 char * tmp;
732 blk_t max = 8192;
733 int blocksize = 0;
734 int inode_ratio = 0;
735 int reserved_ratio = 5;
736 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000737 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000738 int sparse_option = 0;
739 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000740 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000741 char * raid_opts = 0;
742 char * fs_type = 0;
743 const char * feature_set = "filetype,sparse_super";
744 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000745#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000746 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000747
748 if (uname(&ut)) {
749 perror("uname");
750 exit(1);
751 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000752 if ((ut.release[0] == '1') ||
753 (ut.release[0] == '2' && ut.release[1] == '.' &&
754 ut.release[2] < '2' && ut.release[3] == '.'))
755 feature_set = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000756#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000758 if (oldpath) {
759 char *newpath;
760
761 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
762 strcpy (newpath, PATH_SET);
763 strcat (newpath, ":");
764 strcat (newpath, oldpath);
765 putenv (newpath);
766 } else
767 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768
769 setbuf(stdout, NULL);
770 setbuf(stderr, NULL);
771 initialize_ext2_error_table();
772 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000773 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000774
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000775 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000776 E2FSPROGS_VERSION, E2FSPROGS_DATE,
777 EXT2FS_VERSION, EXT2FS_DATE);
778 if (argc && *argv)
779 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000780 while ((c = getopt (argc, argv,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000781 "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 +0000782 switch (c) {
783 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000784 blocksize = strtoul(optarg, &tmp, 0);
785 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000786 com_err(program_name, 0,
787 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788 exit(1);
789 }
790 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000791 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000792 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793 break;
794 case 'c':
795 case 't': /* Check for bad blocks */
796 cflag = 1;
797 break;
798 case 'f':
799 size = strtoul(optarg, &tmp, 0);
800 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000801 com_err(program_name, 0,
802 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000803 optarg);
804 exit(1);
805 }
806 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000807 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000808 printf(_("Warning: fragments not supported. "
809 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810 break;
811 case 'g':
812 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000813 if (*tmp) {
814 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000815 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000816 exit(1);
817 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000818 if ((param.s_blocks_per_group % 8) != 0) {
819 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000820 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821 exit(1);
822 }
823 break;
824 case 'i':
825 inode_ratio = strtoul(optarg, &tmp, 0);
826 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
827 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000828 com_err(program_name, 0,
829 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000830 exit(1);
831 }
832 break;
833 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000834 bad_blocks_filename = malloc(strlen(optarg)+1);
835 if (!bad_blocks_filename) {
836 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000837 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000838 exit(1);
839 }
840 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000841 break;
842 case 'm':
843 reserved_ratio = strtoul(optarg, &tmp, 0);
844 if (reserved_ratio > 50 || *tmp) {
845 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000846 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847 optarg);
848 exit(1);
849 }
850 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000851 case 'n':
852 noaction++;
853 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000854 case 'o':
855 creator_os = optarg;
856 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000857 case 'r':
858 param.s_rev_level = atoi(optarg);
859 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000860 case 's':
861 sparse_option = atoi(optarg);
862 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000863#ifdef EXT2_DYNAMIC_REV
864 case 'I':
865 param.s_inode_size = atoi(optarg);
866 break;
867#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000868 case 'N':
869 num_inodes = atoi(optarg);
870 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000871 case 'v':
872 verbose = 1;
873 break;
874 case 'q':
875 quiet = 1;
876 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000877 case 'F':
878 force = 1;
879 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000880 case 'L':
881 volume_label = optarg;
882 break;
883 case 'M':
884 mount_dir = optarg;
885 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000886 case 'O':
887 feature_set = optarg;
888 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000889 case 'R':
890 raid_opts = optarg;
891 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000892 case 'S':
893 super_only = 1;
894 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000895 case 'T':
896 fs_type = optarg;
897 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000898 case 'V':
899 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000900 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000901 error_message(EXT2_ET_BASE));
902 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000903 default:
904 usage();
905 }
906 if (optind == argc)
907 usage();
908 device_name = argv[optind];
909 optind++;
910 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000911 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
912
913 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000914 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915 argv[optind - 1]);
916 exit(1);
917 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000918 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000919 }
920 if (optind < argc)
921 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000922
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000923 if (raid_opts)
924 parse_raid_opts(raid_opts);
925
Theodore Ts'o74becf31997-04-26 14:37:06 +0000926 if (!force)
927 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000928 check_mount();
929
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 param.s_log_frag_size = param.s_log_block_size;
931
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000932 if (noaction && param.s_blocks_count) {
933 dev_size = param.s_blocks_count;
934 retval = 0;
935 } else
936 retval = ext2fs_get_device_size(device_name,
937 EXT2_BLOCK_SIZE(&param),
938 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000939 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
940 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000941 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000942 exit(1);
943 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000944 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000945 if (retval == EXT2_ET_UNIMPLEMENTED) {
946 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000947 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +0000948 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000949 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000950 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000951 } else
952 param.s_blocks_count = dev_size;
953 } else if (!force && (param.s_blocks_count > dev_size)) {
954 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000955 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000956 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000957 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000958
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000959 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
960
Theodore Ts'o521e3681997-04-29 17:48:10 +0000961 if (param.s_blocks_per_group) {
962 if (param.s_blocks_per_group < 256 ||
963 param.s_blocks_per_group > max || *tmp) {
964 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000965 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +0000966 exit(1);
967 }
968 }
969
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970 /*
971 * Calculate number of inodes based on the inode ratio
972 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000973 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +0000974 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000975 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976
977 /*
978 * Calculate number of blocks to reserve
979 */
980 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000981
Theodore Ts'o521e3681997-04-29 17:48:10 +0000982#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000983 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000984 param_ext2->s_feature_ro_compat |=
985 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
986#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +0000987 if (feature_set && !strncasecmp(feature_set, "none", 4))
988 feature_set = 0;
989 if (feature_set && e2p_edit_feature(feature_set,
990 &param_ext2->s_feature_compat,
991 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000992 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +0000993 feature_set);
994 exit(1);
995 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000996}
997
998int main (int argc, char *argv[])
999{
1000 errcode_t retval = 0;
1001 ext2_filsys fs;
1002 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001003 struct ext2fs_sb *s;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001004
1005#ifdef ENABLE_NLS
1006 setlocale(LC_MESSAGES, "");
1007 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1008 textdomain(NLS_CAT_NAME);
1009#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001010 PRS(argc, argv);
1011
Theodore Ts'o3839e651997-04-26 13:21:57 +00001012 /*
1013 * Initialize the superblock....
1014 */
1015 retval = ext2fs_initialize(device_name, 0, &param,
1016 unix_io_manager, &fs);
1017 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001018 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 exit(1);
1020 }
1021
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001022 /*
1023 * Generate a UUID for it...
1024 */
1025 s = (struct ext2fs_sb *) fs->super;
1026 uuid_generate(s->s_uuid);
1027
1028 /*
1029 * Override the creator OS, if applicable
1030 */
1031 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001032 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001033 exit(1);
1034 }
1035
1036 /*
1037 * Set the volume label...
1038 */
1039 if (volume_label) {
1040 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1041 strncpy(s->s_volume_name, volume_label,
1042 sizeof(s->s_volume_name));
1043 }
1044
1045 /*
1046 * Set the last mount directory
1047 */
1048 if (mount_dir) {
1049 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1050 strncpy(s->s_last_mounted, mount_dir,
1051 sizeof(s->s_last_mounted));
1052 }
1053
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001054 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001055 show_stats(fs);
1056
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001057 if (noaction)
1058 exit(0);
1059
Theodore Ts'o3839e651997-04-26 13:21:57 +00001060 if (bad_blocks_filename)
1061 read_bb_file(fs, &bb_list, bad_blocks_filename);
1062 if (cflag)
1063 test_disk(fs, &bb_list);
1064
1065 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001066 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001067 retval = ext2fs_allocate_tables(fs);
1068 if (retval) {
1069 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001070 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001071 exit(1);
1072 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001073 if (super_only) {
1074 fs->super->s_state |= EXT2_ERROR_FS;
1075 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1076 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001077 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001078 create_root_dir(fs);
1079 create_lost_and_found(fs);
1080 reserve_inodes(fs);
1081 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001082#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001083 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001084#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001085 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001086
1087 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001088 printf(_("Writing superblocks and "
1089 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001090 retval = ext2fs_flush(fs);
1091 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001092 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001093 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001094 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001095 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001096 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001097 return 0;
1098}