blob: 2659fd79e58e02b977ebf0082eba2d19683f9646 [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'o85ef4ae2000-10-24 19:33:45 +000082int journal_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000084__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085
86struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000087char *creator_os = NULL;
88char *volume_label = NULL;
89char *mount_dir = NULL;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +000090char *journal_device = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000091
Theodore Ts'o8ddaa662000-11-17 04:55:24 +000092static void usage(NOARGS);
93static void check_plausibility(const char *device);
94static void check_mount(const char *device);
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000095
Theodore Ts'o3839e651997-04-26 13:21:57 +000096static void usage(NOARGS)
97{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000098 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000099 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000100 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
101 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +0000102 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
103 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000104 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 program_name);
106 exit(1);
107}
108
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000109static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110{
111 int l = 0;
112
113 arg >>= 1;
114 while (arg) {
115 l++;
116 arg >>= 1;
117 }
118 return l;
119}
120
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000121static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000122{
123 int l;
124
125 for (l=0; arg ; l++)
126 arg = arg / 10;
127 return l;
128}
129
Theodore Ts'oa789d841998-03-30 01:20:55 +0000130static void proceed_question(NOARGS)
131{
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000132 char buf[256];
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000133 char *short_yes = _("yY");
134
Theodore Ts'oa789d841998-03-30 01:20:55 +0000135 fflush(stdout);
136 fflush(stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000137 printf(_("Proceed anyway? (y,n) "));
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000138 buf[0] = 0;
139 fgets(buf, sizeof(buf), stdin);
140 if (strchr(short_yes, buf[0]) == 0)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000141 exit(1);
142}
143
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000144#ifndef SCSI_BLK_MAJOR
145#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
146#endif
147
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000148static void check_plausibility(const char *device)
Theodore Ts'o74becf31997-04-26 14:37:06 +0000149{
150#ifdef HAVE_LINUX_MAJOR_H
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000151#ifndef MAJOR
152#define MAJOR(dev) ((dev)>>8)
153#define MINOR(dev) ((dev) & 0xff)
154#endif
155
Theodore Ts'o74becf31997-04-26 14:37:06 +0000156 int val;
157 struct stat s;
158
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000159 val = stat(device, &s);
Theodore Ts'o74becf31997-04-26 14:37:06 +0000160
161 if(val == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000162 fprintf(stderr, _("Could not stat %s --- %s\n"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000163 device, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000164 if (errno == ENOENT)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000165 fprintf(stderr, _("\nThe device apparently does "
166 "not exist; did you specify it correctly?\n"));
Theodore Ts'o74becf31997-04-26 14:37:06 +0000167 exit(1);
168 }
169 if(!S_ISBLK(s.st_mode)) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000170 printf(_("%s is not a block special device.\n"), device);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000171 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000172 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000173 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
174 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000175 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
176 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000177 printf(_("%s is entire device, not just one partition!\n"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000178 device);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000179 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000180 }
181#endif
182}
183
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000184static void check_mount(const char *device)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000186 errcode_t retval;
187 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000189 retval = ext2fs_check_if_mounted(device, &mount_flags);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000190 if (retval) {
191 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000192 _("while determining whether %s is mounted."),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000193 device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000195 }
196 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000198
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000199 fprintf(stderr, _("%s is mounted; "), device);
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000200 if (force) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000201 fprintf(stderr, _("mke2fs forced anyway. "
202 "Hope /etc/mtab is incorrect.\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000203 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000204 fprintf(stderr, _("will not make a filesystem here!\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000205 exit(1);
206 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207}
208
209/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000210 * This function sets the default parameters for a filesystem
211 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000212 * The type is specified by the user. The size is the maximum size
213 * (in megabytes) for which a set of parameters applies, with a size
214 * of zero meaning that it is the default parameter for the type.
215 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000216 */
217static char default_str[] = "default";
218struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000219 const char *type;
220 int size;
221 int blocksize;
222 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000223} settings[] = {
224 { default_str, 0, 4096, 8192 },
225 { default_str, 512, 1024, 4096 },
226 { default_str, 3, 1024, 8192 },
227 { "news", 0, 4096, 4096 },
228 { 0, 0, 0, 0},
229};
230
Theodore Ts'o9094f281999-10-26 16:42:50 +0000231static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000232 int blocksize, int *inode_ratio)
233{
234 int megs;
235 int ratio = 0;
236 struct mke2fs_defaults *p;
237
Theodore Ts'o9094f281999-10-26 16:42:50 +0000238 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000239 1024);
240 if (inode_ratio)
241 ratio = *inode_ratio;
242 if (!fs_type)
243 fs_type = default_str;
244 for (p = settings; p->type; p++) {
245 if ((strcmp(p->type, fs_type) != 0) &&
246 (strcmp(p->type, default_str) != 0))
247 continue;
248 if ((p->size != 0) &&
249 (megs > p->size))
250 continue;
251 if (ratio == 0)
252 *inode_ratio = p->inode_ratio;
253 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000254 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000255 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000256 }
257 }
258 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000259 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000260}
261
262/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 * Helper function for read_bb_file and test_disk
264 */
265static void invalid_block(ext2_filsys fs, blk_t blk)
266{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000267 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268 return;
269}
270
271/*
272 * Reads the bad blocks list from a file
273 */
274static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
275 const char *bad_blocks_file)
276{
277 FILE *f;
278 errcode_t retval;
279
280 f = fopen(bad_blocks_file, "r");
281 if (!f) {
282 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000283 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 exit(1);
285 }
286 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
287 fclose (f);
288 if (retval) {
289 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000290 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 exit(1);
292 }
293}
294
295/*
296 * Runs the badblocks program to test the disk
297 */
298static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
299{
300 FILE *f;
301 errcode_t retval;
302 char buf[1024];
303
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000304 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
305 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 fs->super->s_blocks_count);
307 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000308 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 f = popen(buf, "r");
310 if (!f) {
311 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000312 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 exit(1);
314 }
315 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000316 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 if (retval) {
318 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000319 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 exit(1);
321 }
322}
323
324static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
325{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000326 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327 int must_be_good;
328 blk_t blk;
329 badblocks_iterate bb_iter;
330 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000331 blk_t group_block;
332 int group;
333 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334
335 if (!bb_list)
336 return;
337
338 /*
339 * The primary superblock and group descriptors *must* be
340 * good; if not, abort.
341 */
342 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
343 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
344 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000345 fprintf(stderr, _("Block %d in primary "
346 "superblock/group descriptor area bad.\n"), i);
347 fprintf(stderr, _("Blocks %d through %d must be good "
348 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000350 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351 exit(1);
352 }
353 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000354
355 /*
356 * See if any of the bad blocks are showing up in the backup
357 * superblocks and/or group descriptors. If so, issue a
358 * warning and adjust the block counts appropriately.
359 */
360 group_block = fs->super->s_first_data_block +
361 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000362
363 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000364 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000365 for (j=0; j < fs->desc_blocks+1; j++) {
366 if (badblocks_list_test(bb_list, group_block +
367 j)) {
368 if (!group_bad)
369 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000370_("Warning: the backup superblock/group descriptors at block %d contain\n"
371" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000372 group_block);
373 group_bad++;
374 group = ext2fs_group_of_blk(fs, group_block+j);
375 fs->group_desc[group].bg_free_blocks_count++;
376 fs->super->s_free_blocks_count++;
377 }
378 }
379 group_block += fs->super->s_blocks_per_group;
380 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381
382 /*
383 * Mark all the bad blocks as used...
384 */
385 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
386 if (retval) {
387 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000388 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389 exit(1);
390 }
391 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000392 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393 badblocks_list_iterate_end(bb_iter);
394}
395
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000396static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397{
398 errcode_t retval;
399 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000400 int i, j, num, count;
401 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000402 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000403 int sync_kludge = 0;
404 char *mke2fs_sync;
405
406 mke2fs_sync = getenv("MKE2FS_SYNC");
407 if (mke2fs_sync)
408 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409
410 buf = malloc(fs->blocksize * STRIDE_LENGTH);
411 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000412 com_err("malloc", ENOMEM,
413 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 exit(1);
415 }
416 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000417
418 /*
419 * Figure out how many digits we need
420 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000421 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000422 sprintf(format, "%%%dd/%%%dld", i, i);
423 memset(backup, '\b', sizeof(backup)-1);
424 backup[sizeof(backup)-1] = 0;
425 if ((2*i)+1 < sizeof(backup))
426 backup[(2*i)+1] = 0;
427
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000429 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 for (i = 0; i < fs->group_desc_count; i++) {
431 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000432 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000434 blk = fs->group_desc[i].bg_inode_table;
435 num = fs->inode_blocks_per_group;
436
437 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
438 if (num-j > STRIDE_LENGTH)
439 count = STRIDE_LENGTH;
440 else
441 count = num - j;
442 retval = io_channel_write_blk(fs->io, blk, count, buf);
443 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000444 printf(_("Warning: could not write %d blocks "
445 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000446 count, blk, error_message(retval));
447 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000449 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000450 if (sync_kludge) {
451 if (sync_kludge == 1)
452 sync();
453 else if ((i % sync_kludge) == 0)
454 sync();
455 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000457 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000459 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460}
461
462static void create_root_dir(ext2_filsys fs)
463{
464 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000465 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466
467 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
468 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000469 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 exit(1);
471 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000472 if (geteuid()) {
473 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
474 if (retval) {
475 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000476 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 exit(1);
478 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000479 inode.i_uid = getuid();
480 if (inode.i_uid)
481 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
483 if (retval) {
484 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000485 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000486 exit(1);
487 }
488 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489}
490
491static void create_lost_and_found(ext2_filsys fs)
492{
493 errcode_t retval;
494 ino_t ino;
495 const char *name = "lost+found";
496 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000497 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498
499 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
500 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000501 com_err("ext2fs_mkdir", retval,
502 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 exit(1);
504 }
505
506 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
507 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000508 com_err("ext2_lookup", retval,
509 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 exit(1);
511 }
512
513 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000514 if ((lpf_size += fs->blocksize) >= 16*1024)
515 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 retval = ext2fs_expand_dir(fs, ino);
517 if (retval) {
518 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000519 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 exit(1);
521 }
522 }
523}
524
525static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
526{
527 errcode_t retval;
528
Theodore Ts'of3db3561997-04-26 13:34:30 +0000529 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 fs->group_desc[0].bg_free_inodes_count--;
531 fs->super->s_free_inodes_count--;
532 retval = ext2fs_update_bb_inode(fs, bb_list);
533 if (retval) {
534 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000535 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 exit(1);
537 }
538
539}
540
541static void reserve_inodes(ext2_filsys fs)
542{
543 ino_t i;
544 int group;
545
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000546 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000547 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 group = ext2fs_group_of_ino(fs, i);
549 fs->group_desc[group].bg_free_inodes_count--;
550 fs->super->s_free_inodes_count--;
551 }
552 ext2fs_mark_ib_dirty(fs);
553}
554
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000555static void zap_sector(ext2_filsys fs, int sect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000556{
557 char buf[512];
558 int retval;
559
560 memset(buf, 0, 512);
561
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000562 io_channel_set_blksize(fs->io, 512);
563 retval = io_channel_write_blk(fs->io, sect, -512, buf);
564 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000566 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000567 error_message(retval));
568}
569
570
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571static void show_stats(ext2_filsys fs)
572{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000573 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
574 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000576 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577
578 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000579 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000581
582 memset(buf, 0, sizeof(buf));
583 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000584 printf(_("Filesystem label=%s\n"), buf);
585 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000586 switch (fs->super->s_creator_os) {
587 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000588 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000589 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000590 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000591 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000592 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000593 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000594 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000595 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000596 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000597 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000599 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 s->s_r_blocks_count,
601 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000602 printf(_("First data block=%u\n"), s->s_first_data_block);
603 if (fs->group_desc_count > 1)
604 printf(_("%lu block groups\n"), fs->group_desc_count);
605 else
606 printf(_("%lu block group\n"), fs->group_desc_count);
607 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000609 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610
611 if (fs->group_desc_count == 1) {
612 printf("\n");
613 return;
614 }
615
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000616 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 group_block = s->s_first_data_block;
618 col_left = 0;
619 for (i = 1; i < fs->group_desc_count; i++) {
620 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000621 if (!ext2fs_bg_has_super(fs, i))
622 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000623 if (i != 1)
624 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000625 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000626 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000628 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000630 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000631 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 }
633 printf("\n\n");
634}
635
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000636#ifndef HAVE_STRCASECMP
637static int strcasecmp (char *s1, char *s2)
638{
639 while (*s1 && *s2) {
640 int ch1 = *s1++, ch2 = *s2++;
641 if (isupper (ch1))
642 ch1 = tolower (ch1);
643 if (isupper (ch2))
644 ch2 = tolower (ch2);
645 if (ch1 != ch2)
646 return ch1 - ch2;
647 }
648 return *s1 ? 1 : *s2 ? -1 : 0;
649}
650#endif
651
652/*
653 * Set the S_CREATOR_OS field. Return true if OS is known,
654 * otherwise, 0.
655 */
656static int set_os(struct ext2_super_block *sb, char *os)
657{
658 if (isdigit (*os))
659 sb->s_creator_os = atoi (os);
660 else if (strcasecmp(os, "linux") == 0)
661 sb->s_creator_os = EXT2_OS_LINUX;
662 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
663 sb->s_creator_os = EXT2_OS_HURD;
664 else if (strcasecmp(os, "masix") == 0)
665 sb->s_creator_os = EXT2_OS_MASIX;
666 else
667 return 0;
668 return 1;
669}
670
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000671#define PATH_SET "PATH=/sbin"
672
Theodore Ts'od163b091997-10-03 17:42:28 +0000673static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000674{
675 char *buf, *token, *next, *p, *arg;
676 int len;
677 int raid_usage = 0;
678
679 len = strlen(opts);
680 buf = malloc(len+1);
681 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000682 fprintf(stderr, _("Couldn't allocate memory to parse "
683 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000684 exit(1);
685 }
686 strcpy(buf, opts);
687 for (token = buf; token && *token; token = next) {
688 p = strchr(token, ',');
689 next = 0;
690 if (p) {
691 *p = 0;
692 next = p+1;
693 }
694 arg = strchr(token, '=');
695 if (arg) {
696 *arg = 0;
697 arg++;
698 }
699 if (strcmp(token, "stride") == 0) {
700 if (!arg) {
701 raid_usage++;
702 continue;
703 }
704 fs_stride = strtoul(arg, &p, 0);
705 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000706 fprintf(stderr,
707 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000708 raid_usage++;
709 continue;
710 }
711 } else
712 raid_usage++;
713 }
714 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000715 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000716 "Raid options are separated by commas, "
717 "and may take an argument which\n"
718 "\tis set off by an equals ('=') sign.\n\n"
719 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000720 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000721 exit(1);
722 }
723}
724
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000725static void parse_journal_opts(const char *opts)
726{
727 char *buf, *token, *next, *p, *arg;
728 int len;
729 int journal_usage = 0;
730
731 len = strlen(opts);
732 buf = malloc(len+1);
733 if (!buf) {
734 fprintf(stderr, _("Couldn't allocate memory to parse "
735 "journal options!\n"));
736 exit(1);
737 }
738 strcpy(buf, opts);
739 for (token = buf; token && *token; token = next) {
740 p = strchr(token, ',');
741 next = 0;
742 if (p) {
743 *p = 0;
744 next = p+1;
745 }
746 arg = strchr(token, '=');
747 if (arg) {
748 *arg = 0;
749 arg++;
750 }
751 printf("Journal option=%s, argument=%s\n", token,
752 arg ? arg : "NONE");
753 if (strcmp(token, "device") == 0) {
754 if (!arg) {
755 journal_usage++;
756 continue;
757 }
758 journal_device = arg;
759 } else if (strcmp(token, "size") == 0) {
760 if (!arg) {
761 journal_usage++;
762 continue;
763 }
764 journal_size = strtoul(arg, &p, 0);
765 journal_size_check:
766 if (*p || (journal_size < 4 || journal_size > 100)) {
767 fprintf(stderr,
768 _("Invalid journal size parameter - %s.\n"),
769 arg);
770 journal_usage++;
771 continue;
772 }
773 } else {
774 journal_size = strtoul(token, &p, 0);
775 if (*p)
776 journal_usage++;
777 else
778 goto journal_size_check;
779 }
780 }
781 if (journal_usage) {
782 fprintf(stderr, _("\nBad journal options specified.\n\n"
783 "Journal options are separated by commas, "
784 "and may take an argument which\n"
785 "\tis set off by an equals ('=') sign.\n\n"
786 "Valid raid options are:\n"
787 "\tsize=<journal size in megabytes>\n"
788 "\tdevice=<journal device>\n\n"
789 "Journal size must be between "
790 "4 and 100 megabytes.\n\n" ));
791 exit(1);
792 }
793}
794
Theodore Ts'o896938d1999-10-23 01:04:50 +0000795static __u32 ok_features[3] = {
796 0, /* Compat */
797 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
798 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
799};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000800
801
Theodore Ts'o3839e651997-04-26 13:21:57 +0000802static void PRS(int argc, char *argv[])
803{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000804 int c;
805 int size;
806 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000807 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000808 int blocksize = 0;
809 int inode_ratio = 0;
810 int reserved_ratio = 5;
811 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000812 errcode_t retval;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000813 int sparse_option = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000814 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000815 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000816 char * raid_opts = 0;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000817 char * journal_opts = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000818 char * fs_type = 0;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000819 const char * feature_set = "filetype";
Theodore Ts'o4a600561999-10-26 14:35:51 +0000820 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000821#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000822 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000823
824 if (uname(&ut)) {
825 perror("uname");
826 exit(1);
827 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000828 if ((ut.release[0] == '1') ||
829 (ut.release[0] == '2' && ut.release[1] == '.' &&
830 ut.release[2] < '2' && ut.release[3] == '.'))
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000831 feature_set = NULL;
Theodore Ts'o27401561999-09-14 20:11:19 +0000832#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000834 if (oldpath) {
835 char *newpath;
836
837 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
838 strcpy (newpath, PATH_SET);
839 strcat (newpath, ":");
840 strcat (newpath, oldpath);
841 putenv (newpath);
842 } else
843 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844
845 setbuf(stdout, NULL);
846 setbuf(stderr, NULL);
847 initialize_ext2_error_table();
848 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000849 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000850
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000851 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000852 E2FSPROGS_VERSION, E2FSPROGS_DATE,
853 EXT2FS_VERSION, EXT2FS_DATE);
854 if (argc && *argv)
855 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000856 while ((c = getopt (argc, argv,
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000857 "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000858 switch (c) {
859 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000860 blocksize = strtoul(optarg, &tmp, 0);
861 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000862 com_err(program_name, 0,
863 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000864 exit(1);
865 }
866 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000867 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000868 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000869 break;
870 case 'c':
871 case 't': /* Check for bad blocks */
872 cflag = 1;
873 break;
874 case 'f':
875 size = strtoul(optarg, &tmp, 0);
876 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000877 com_err(program_name, 0,
878 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879 optarg);
880 exit(1);
881 }
882 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000883 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000884 printf(_("Warning: fragments not supported. "
885 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 break;
887 case 'g':
888 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000889 if (*tmp) {
890 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000891 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000892 exit(1);
893 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000894 if ((param.s_blocks_per_group % 8) != 0) {
895 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000896 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897 exit(1);
898 }
899 break;
900 case 'i':
901 inode_ratio = strtoul(optarg, &tmp, 0);
902 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
903 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000904 com_err(program_name, 0,
905 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 exit(1);
907 }
908 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000909 case 'j':
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000910 journal_opts = optarg;
911 break;
912#if 0
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000913 journal_size = strtoul(optarg, &tmp, 0);
914 if (journal_size < 4 || journal_size > 100 || *tmp) {
915 com_err(program_name, 0,
916 _("bad journal size - %s"), optarg);
917 exit(1);
918 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000919#endif
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000920 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000921 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000922 bad_blocks_filename = malloc(strlen(optarg)+1);
923 if (!bad_blocks_filename) {
924 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000925 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000926 exit(1);
927 }
928 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929 break;
930 case 'm':
931 reserved_ratio = strtoul(optarg, &tmp, 0);
932 if (reserved_ratio > 50 || *tmp) {
933 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000934 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000935 optarg);
936 exit(1);
937 }
938 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000939 case 'n':
940 noaction++;
941 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000942 case 'o':
943 creator_os = optarg;
944 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000945 case 'r':
946 param.s_rev_level = atoi(optarg);
947 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000948 case 's':
949 sparse_option = atoi(optarg);
950 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000951#ifdef EXT2_DYNAMIC_REV
952 case 'I':
953 param.s_inode_size = atoi(optarg);
954 break;
955#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000956 case 'N':
957 num_inodes = atoi(optarg);
958 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000959 case 'v':
960 verbose = 1;
961 break;
962 case 'q':
963 quiet = 1;
964 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000965 case 'F':
966 force = 1;
967 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000968 case 'L':
969 volume_label = optarg;
970 break;
971 case 'M':
972 mount_dir = optarg;
973 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000974 case 'O':
975 feature_set = optarg;
976 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000977 case 'R':
978 raid_opts = optarg;
979 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000980 case 'S':
981 super_only = 1;
982 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000983 case 'T':
984 fs_type = optarg;
985 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000986 case 'V':
987 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000988 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000989 error_message(EXT2_ET_BASE));
990 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000991 default:
992 usage();
993 }
994 if (optind == argc)
995 usage();
996 device_name = argv[optind];
997 optind++;
998 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000999 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
1000
1001 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001002 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001003 argv[optind - 1]);
1004 exit(1);
1005 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001006 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007 }
1008 if (optind < argc)
1009 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001010
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001011 if (raid_opts)
1012 parse_raid_opts(raid_opts);
1013
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001014 if (journal_opts)
1015 parse_journal_opts(journal_opts);
1016
Theodore Ts'o74becf31997-04-26 14:37:06 +00001017 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001018 check_plausibility(device_name);
1019 check_mount(device_name);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001020
Theodore Ts'o3839e651997-04-26 13:21:57 +00001021 param.s_log_frag_size = param.s_log_block_size;
1022
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001023 if (noaction && param.s_blocks_count) {
1024 dev_size = param.s_blocks_count;
1025 retval = 0;
1026 } else
1027 retval = ext2fs_get_device_size(device_name,
1028 EXT2_BLOCK_SIZE(&param),
1029 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001030 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1031 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001032 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001033 exit(1);
1034 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001035 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001036 if (retval == EXT2_ET_UNIMPLEMENTED) {
1037 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001038 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001039 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001040 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001041 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001042 } else {
1043 if (dev_size == 0) {
1044 com_err(program_name, 0,
1045 _("Device size reported to be zero. "
1046 "Invalid partition specified, or\n\t"
1047 "partition table wasn't reread "
1048 "after running fdisk, due to\n\t"
1049 "a modified partition being busy "
1050 "and in use. You may need to reboot\n\t"
1051 "to re-read your partition table.\n"
1052 ));
1053 exit(1);
1054 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001055 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001056 }
1057
Theodore Ts'oa789d841998-03-30 01:20:55 +00001058 } else if (!force && (param.s_blocks_count > dev_size)) {
1059 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001060 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001061 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001062 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001063
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001064 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
1065
Theodore Ts'o521e3681997-04-29 17:48:10 +00001066 if (param.s_blocks_per_group) {
1067 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001068 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001069 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001070 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001071 exit(1);
1072 }
1073 }
1074
Theodore Ts'o3839e651997-04-26 13:21:57 +00001075 /*
1076 * Calculate number of inodes based on the inode ratio
1077 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001078 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001079 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001080 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001081
1082 /*
1083 * Calculate number of blocks to reserve
1084 */
1085 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001086
Theodore Ts'of6f65832000-10-25 03:01:37 +00001087 /* Turn off features not supported by the earlier filesystem version */
1088 if (param.s_rev_level == 0) {
1089 sparse_option = 0;
1090 feature_set = NULL;
1091 }
Theodore Ts'o27401561999-09-14 20:11:19 +00001092 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +00001093 param_ext2->s_feature_ro_compat |=
1094 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'of6f65832000-10-25 03:01:37 +00001095
Theodore Ts'o896938d1999-10-23 01:04:50 +00001096 if (feature_set && !strncasecmp(feature_set, "none", 4))
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001097 feature_set = NULL;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001098 if (feature_set && e2p_edit_feature(feature_set,
1099 &param_ext2->s_feature_compat,
1100 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001101 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +00001102 feature_set);
1103 exit(1);
1104 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001105}
1106
1107int main (int argc, char *argv[])
1108{
1109 errcode_t retval = 0;
1110 ext2_filsys fs;
1111 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001112 int journal_blocks;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001113 struct ext2fs_sb *s;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001114
1115#ifdef ENABLE_NLS
1116 setlocale(LC_MESSAGES, "");
1117 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1118 textdomain(NLS_CAT_NAME);
1119#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001120 PRS(argc, argv);
1121
Theodore Ts'o3839e651997-04-26 13:21:57 +00001122 /*
1123 * Initialize the superblock....
1124 */
1125 retval = ext2fs_initialize(device_name, 0, &param,
1126 unix_io_manager, &fs);
1127 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001128 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001129 exit(1);
1130 }
1131
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001132 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001133 * Wipe out the old on-disk superblock
1134 */
1135 zap_sector(fs, 2);
1136
1137 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001138 * Generate a UUID for it...
1139 */
1140 s = (struct ext2fs_sb *) fs->super;
1141 uuid_generate(s->s_uuid);
1142
1143 /*
1144 * Override the creator OS, if applicable
1145 */
1146 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001147 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001148 exit(1);
1149 }
1150
1151 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001152 * For the Hurd, we will turn off filetype since it doesn't
1153 * support it.
1154 */
1155 if (fs->super->s_creator_os == EXT2_OS_HURD)
1156 fs->super->s_feature_incompat &=
1157 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1158
1159 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001160 * Set the volume label...
1161 */
1162 if (volume_label) {
1163 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1164 strncpy(s->s_volume_name, volume_label,
1165 sizeof(s->s_volume_name));
1166 }
1167
1168 /*
1169 * Set the last mount directory
1170 */
1171 if (mount_dir) {
1172 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1173 strncpy(s->s_last_mounted, mount_dir,
1174 sizeof(s->s_last_mounted));
1175 }
1176
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001177 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001178 show_stats(fs);
1179
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001180 if (noaction)
1181 exit(0);
1182
Theodore Ts'o3839e651997-04-26 13:21:57 +00001183 if (bad_blocks_filename)
1184 read_bb_file(fs, &bb_list, bad_blocks_filename);
1185 if (cflag)
1186 test_disk(fs, &bb_list);
1187
1188 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001189 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001190 retval = ext2fs_allocate_tables(fs);
1191 if (retval) {
1192 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001193 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001194 exit(1);
1195 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001196 if (super_only) {
1197 fs->super->s_state |= EXT2_ERROR_FS;
1198 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1199 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001200 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001201 create_root_dir(fs);
1202 create_lost_and_found(fs);
1203 reserve_inodes(fs);
1204 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001205#ifdef ZAP_BOOTBLOCK
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001206 zap_sector(fs, 0);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001207#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001208 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001209
1210 if (journal_device) {
1211 if (!force)
1212 check_plausibility(journal_device);
1213 check_mount(journal_device);
1214
1215 if (!quiet)
1216 printf(_("Creating journal on device %s: "),
1217 journal_device);
1218 retval = ext2fs_add_journal_device(fs, journal_device, 0);
1219 if(retval) {
1220 com_err (program_name, retval,
1221 _("while trying to create journal on device %s"),
1222 journal_device);
1223 exit(1);
1224 }
1225 if (!quiet)
1226 printf(_("done\n"));
1227 } else if (journal_size) {
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001228 if (!quiet)
1229 printf(_("Creating journal: "));
1230 journal_blocks = journal_size * 1024 /
1231 (fs->blocksize / 1024);
1232 retval = ext2fs_add_journal_fs(fs, journal_blocks);
1233 if (retval) {
1234 com_err (program_name, retval,
1235 _("while trying to create journal"));
1236 exit(1);
1237 }
1238 if (!quiet)
1239 printf(_("done\n"));
1240 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001241
1242 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001243 printf(_("Writing superblocks and "
1244 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001245 retval = ext2fs_flush(fs);
1246 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001247 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001248 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001249 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001250 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001251 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001252 return 0;
1253}