blob: 4062bb42d84cfa4b5b3cf9255ca2d867d46feb61 [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'o3839e651997-04-26 13:21:57 +000090
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000091static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
92
Theodore Ts'o3839e651997-04-26 13:21:57 +000093static void usage(NOARGS)
94{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000095 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000096 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000097 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
98 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000099 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
100 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000101 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 program_name);
103 exit(1);
104}
105
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000106static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107{
108 int l = 0;
109
110 arg >>= 1;
111 while (arg) {
112 l++;
113 arg >>= 1;
114 }
115 return l;
116}
117
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000118static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000119{
120 int l;
121
122 for (l=0; arg ; l++)
123 arg = arg / 10;
124 return l;
125}
126
Theodore Ts'oa789d841998-03-30 01:20:55 +0000127static void proceed_question(NOARGS)
128{
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000129 char buf[256];
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000130 char *short_yes = _("yY");
131
Theodore Ts'oa789d841998-03-30 01:20:55 +0000132 fflush(stdout);
133 fflush(stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000134 printf(_("Proceed anyway? (y,n) "));
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000135 buf[0] = 0;
136 fgets(buf, sizeof(buf), stdin);
137 if (strchr(short_yes, buf[0]) == 0)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000138 exit(1);
139}
140
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000141#ifndef SCSI_BLK_MAJOR
142#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
143#endif
144
Theodore Ts'o74becf31997-04-26 14:37:06 +0000145static void check_plausibility(NOARGS)
146{
147#ifdef HAVE_LINUX_MAJOR_H
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000148#ifndef MAJOR
149#define MAJOR(dev) ((dev)>>8)
150#define MINOR(dev) ((dev) & 0xff)
151#endif
152
Theodore Ts'o74becf31997-04-26 14:37:06 +0000153 int val;
154 struct stat s;
155
156 val = stat(device_name, &s);
157
158 if(val == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000159 fprintf(stderr, _("Could not stat %s --- %s\n"),
Theodore Ts'oa789d841998-03-30 01:20:55 +0000160 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000161 if (errno == ENOENT)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000162 fprintf(stderr, _("\nThe device apparently does "
163 "not exist; did you specify it correctly?\n"));
Theodore Ts'o74becf31997-04-26 14:37:06 +0000164 exit(1);
165 }
166 if(!S_ISBLK(s.st_mode)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000167 printf(_("%s is not a block special device.\n"), device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000168 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000169 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000170 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
171 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000172 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
173 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000174 printf(_("%s is entire device, not just one partition!\n"),
Theodore Ts'o74becf31997-04-26 14:37:06 +0000175 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000176 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000177 }
178#endif
179}
180
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181static void check_mount(NOARGS)
182{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000183 errcode_t retval;
184 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000186 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
187 if (retval) {
188 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000189 _("while determining whether %s is mounted."),
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000190 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000192 }
193 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000195
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000196 fprintf(stderr, _("%s is mounted; "), device_name);
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000197 if (force) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000198 fprintf(stderr, _("mke2fs forced anyway. "
199 "Hope /etc/mtab is incorrect.\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000200 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000201 fprintf(stderr, _("will not make a filesystem here!\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000202 exit(1);
203 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204}
205
206/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000207 * This function sets the default parameters for a filesystem
208 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000209 * The type is specified by the user. The size is the maximum size
210 * (in megabytes) for which a set of parameters applies, with a size
211 * of zero meaning that it is the default parameter for the type.
212 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000213 */
214static char default_str[] = "default";
215struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000216 const char *type;
217 int size;
218 int blocksize;
219 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000220} settings[] = {
221 { default_str, 0, 4096, 8192 },
222 { default_str, 512, 1024, 4096 },
223 { default_str, 3, 1024, 8192 },
224 { "news", 0, 4096, 4096 },
225 { 0, 0, 0, 0},
226};
227
Theodore Ts'o9094f281999-10-26 16:42:50 +0000228static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000229 int blocksize, int *inode_ratio)
230{
231 int megs;
232 int ratio = 0;
233 struct mke2fs_defaults *p;
234
Theodore Ts'o9094f281999-10-26 16:42:50 +0000235 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000236 1024);
237 if (inode_ratio)
238 ratio = *inode_ratio;
239 if (!fs_type)
240 fs_type = default_str;
241 for (p = settings; p->type; p++) {
242 if ((strcmp(p->type, fs_type) != 0) &&
243 (strcmp(p->type, default_str) != 0))
244 continue;
245 if ((p->size != 0) &&
246 (megs > p->size))
247 continue;
248 if (ratio == 0)
249 *inode_ratio = p->inode_ratio;
250 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000251 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000252 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000253 }
254 }
255 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000256 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000257}
258
259/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000260 * Helper function for read_bb_file and test_disk
261 */
262static void invalid_block(ext2_filsys fs, blk_t blk)
263{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000264 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 return;
266}
267
268/*
269 * Reads the bad blocks list from a file
270 */
271static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
272 const char *bad_blocks_file)
273{
274 FILE *f;
275 errcode_t retval;
276
277 f = fopen(bad_blocks_file, "r");
278 if (!f) {
279 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000280 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281 exit(1);
282 }
283 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
284 fclose (f);
285 if (retval) {
286 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000287 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 exit(1);
289 }
290}
291
292/*
293 * Runs the badblocks program to test the disk
294 */
295static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
296{
297 FILE *f;
298 errcode_t retval;
299 char buf[1024];
300
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000301 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
302 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000303 fs->super->s_blocks_count);
304 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000305 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 f = popen(buf, "r");
307 if (!f) {
308 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000309 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 exit(1);
311 }
312 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000313 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 if (retval) {
315 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000316 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 exit(1);
318 }
319}
320
321static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
322{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000323 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324 int must_be_good;
325 blk_t blk;
326 badblocks_iterate bb_iter;
327 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000328 blk_t group_block;
329 int group;
330 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331
332 if (!bb_list)
333 return;
334
335 /*
336 * The primary superblock and group descriptors *must* be
337 * good; if not, abort.
338 */
339 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
340 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
341 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000342 fprintf(stderr, _("Block %d in primary "
343 "superblock/group descriptor area bad.\n"), i);
344 fprintf(stderr, _("Blocks %d through %d must be good "
345 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000347 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 exit(1);
349 }
350 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000351
352 /*
353 * See if any of the bad blocks are showing up in the backup
354 * superblocks and/or group descriptors. If so, issue a
355 * warning and adjust the block counts appropriately.
356 */
357 group_block = fs->super->s_first_data_block +
358 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000359
360 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000361 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000362 for (j=0; j < fs->desc_blocks+1; j++) {
363 if (badblocks_list_test(bb_list, group_block +
364 j)) {
365 if (!group_bad)
366 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000367_("Warning: the backup superblock/group descriptors at block %d contain\n"
368" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000369 group_block);
370 group_bad++;
371 group = ext2fs_group_of_blk(fs, group_block+j);
372 fs->group_desc[group].bg_free_blocks_count++;
373 fs->super->s_free_blocks_count++;
374 }
375 }
376 group_block += fs->super->s_blocks_per_group;
377 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378
379 /*
380 * Mark all the bad blocks as used...
381 */
382 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
383 if (retval) {
384 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000385 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386 exit(1);
387 }
388 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000389 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 badblocks_list_iterate_end(bb_iter);
391}
392
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000393static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394{
395 errcode_t retval;
396 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000397 int i, j, num, count;
398 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000399 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000400 int sync_kludge = 0;
401 char *mke2fs_sync;
402
403 mke2fs_sync = getenv("MKE2FS_SYNC");
404 if (mke2fs_sync)
405 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406
407 buf = malloc(fs->blocksize * STRIDE_LENGTH);
408 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000409 com_err("malloc", ENOMEM,
410 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000411 exit(1);
412 }
413 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000414
415 /*
416 * Figure out how many digits we need
417 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000418 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000419 sprintf(format, "%%%dd/%%%dld", i, i);
420 memset(backup, '\b', sizeof(backup)-1);
421 backup[sizeof(backup)-1] = 0;
422 if ((2*i)+1 < sizeof(backup))
423 backup[(2*i)+1] = 0;
424
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000426 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 for (i = 0; i < fs->group_desc_count; i++) {
428 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000429 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000431 blk = fs->group_desc[i].bg_inode_table;
432 num = fs->inode_blocks_per_group;
433
434 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
435 if (num-j > STRIDE_LENGTH)
436 count = STRIDE_LENGTH;
437 else
438 count = num - j;
439 retval = io_channel_write_blk(fs->io, blk, count, buf);
440 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000441 printf(_("Warning: could not write %d blocks "
442 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000443 count, blk, error_message(retval));
444 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000446 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000447 if (sync_kludge) {
448 if (sync_kludge == 1)
449 sync();
450 else if ((i % sync_kludge) == 0)
451 sync();
452 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000454 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000456 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457}
458
459static void create_root_dir(ext2_filsys fs)
460{
461 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463
464 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
465 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000466 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 exit(1);
468 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 if (geteuid()) {
470 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
471 if (retval) {
472 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000473 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000474 exit(1);
475 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000476 inode.i_uid = getuid();
477 if (inode.i_uid)
478 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000479 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
480 if (retval) {
481 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000482 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000483 exit(1);
484 }
485 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486}
487
488static void create_lost_and_found(ext2_filsys fs)
489{
490 errcode_t retval;
491 ino_t ino;
492 const char *name = "lost+found";
493 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000494 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495
496 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
497 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000498 com_err("ext2fs_mkdir", retval,
499 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 exit(1);
501 }
502
503 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
504 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000505 com_err("ext2_lookup", retval,
506 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 exit(1);
508 }
509
510 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000511 if ((lpf_size += fs->blocksize) >= 16*1024)
512 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 retval = ext2fs_expand_dir(fs, ino);
514 if (retval) {
515 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000516 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 exit(1);
518 }
519 }
520}
521
522static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
523{
524 errcode_t retval;
525
Theodore Ts'of3db3561997-04-26 13:34:30 +0000526 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 fs->group_desc[0].bg_free_inodes_count--;
528 fs->super->s_free_inodes_count--;
529 retval = ext2fs_update_bb_inode(fs, bb_list);
530 if (retval) {
531 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000532 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 exit(1);
534 }
535
536}
537
538static void reserve_inodes(ext2_filsys fs)
539{
540 ino_t i;
541 int group;
542
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000543 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 group = ext2fs_group_of_ino(fs, i);
546 fs->group_desc[group].bg_free_inodes_count--;
547 fs->super->s_free_inodes_count--;
548 }
549 ext2fs_mark_ib_dirty(fs);
550}
551
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000552static void zap_sector(ext2_filsys fs, int sect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000553{
554 char buf[512];
555 int retval;
556
557 memset(buf, 0, 512);
558
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000559 io_channel_set_blksize(fs->io, 512);
560 retval = io_channel_write_blk(fs->io, sect, -512, buf);
561 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000562 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000563 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000564 error_message(retval));
565}
566
567
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568static void show_stats(ext2_filsys fs)
569{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000570 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
571 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000573 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574
575 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000576 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000578
579 memset(buf, 0, sizeof(buf));
580 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000581 printf(_("Filesystem label=%s\n"), buf);
582 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000583 switch (fs->super->s_creator_os) {
584 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000585 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000586 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000587 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000588 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000589 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000590 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000591 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000592 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000593 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000594 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000596 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 s->s_r_blocks_count,
598 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000599 printf(_("First data block=%u\n"), s->s_first_data_block);
600 if (fs->group_desc_count > 1)
601 printf(_("%lu block groups\n"), fs->group_desc_count);
602 else
603 printf(_("%lu block group\n"), fs->group_desc_count);
604 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000606 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607
608 if (fs->group_desc_count == 1) {
609 printf("\n");
610 return;
611 }
612
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000613 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 group_block = s->s_first_data_block;
615 col_left = 0;
616 for (i = 1; i < fs->group_desc_count; i++) {
617 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000618 if (!ext2fs_bg_has_super(fs, i))
619 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000620 if (i != 1)
621 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000622 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000623 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000625 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000627 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000628 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 }
630 printf("\n\n");
631}
632
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000633#ifndef HAVE_STRCASECMP
634static int strcasecmp (char *s1, char *s2)
635{
636 while (*s1 && *s2) {
637 int ch1 = *s1++, ch2 = *s2++;
638 if (isupper (ch1))
639 ch1 = tolower (ch1);
640 if (isupper (ch2))
641 ch2 = tolower (ch2);
642 if (ch1 != ch2)
643 return ch1 - ch2;
644 }
645 return *s1 ? 1 : *s2 ? -1 : 0;
646}
647#endif
648
649/*
650 * Set the S_CREATOR_OS field. Return true if OS is known,
651 * otherwise, 0.
652 */
653static int set_os(struct ext2_super_block *sb, char *os)
654{
655 if (isdigit (*os))
656 sb->s_creator_os = atoi (os);
657 else if (strcasecmp(os, "linux") == 0)
658 sb->s_creator_os = EXT2_OS_LINUX;
659 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
660 sb->s_creator_os = EXT2_OS_HURD;
661 else if (strcasecmp(os, "masix") == 0)
662 sb->s_creator_os = EXT2_OS_MASIX;
663 else
664 return 0;
665 return 1;
666}
667
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000668#define PATH_SET "PATH=/sbin"
669
Theodore Ts'od163b091997-10-03 17:42:28 +0000670static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000671{
672 char *buf, *token, *next, *p, *arg;
673 int len;
674 int raid_usage = 0;
675
676 len = strlen(opts);
677 buf = malloc(len+1);
678 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000679 fprintf(stderr, _("Couldn't allocate memory to parse "
680 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000681 exit(1);
682 }
683 strcpy(buf, opts);
684 for (token = buf; token && *token; token = next) {
685 p = strchr(token, ',');
686 next = 0;
687 if (p) {
688 *p = 0;
689 next = p+1;
690 }
691 arg = strchr(token, '=');
692 if (arg) {
693 *arg = 0;
694 arg++;
695 }
696 if (strcmp(token, "stride") == 0) {
697 if (!arg) {
698 raid_usage++;
699 continue;
700 }
701 fs_stride = strtoul(arg, &p, 0);
702 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000703 fprintf(stderr,
704 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000705 raid_usage++;
706 continue;
707 }
708 } else
709 raid_usage++;
710 }
711 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000712 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000713 "Raid options are separated by commas, "
714 "and may take an argument which\n"
715 "\tis set off by an equals ('=') sign.\n\n"
716 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000717 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000718 exit(1);
719 }
720}
721
Theodore Ts'o896938d1999-10-23 01:04:50 +0000722static __u32 ok_features[3] = {
723 0, /* Compat */
724 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
725 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
726};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000727
728
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729static void PRS(int argc, char *argv[])
730{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000731 int c;
732 int size;
733 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000734 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000735 int blocksize = 0;
736 int inode_ratio = 0;
737 int reserved_ratio = 5;
738 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000739 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000740 int sparse_option = 0;
741 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000742 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000743 char * raid_opts = 0;
744 char * fs_type = 0;
745 const char * feature_set = "filetype,sparse_super";
746 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000747#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000748 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000749
750 if (uname(&ut)) {
751 perror("uname");
752 exit(1);
753 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000754 if ((ut.release[0] == '1') ||
755 (ut.release[0] == '2' && ut.release[1] == '.' &&
756 ut.release[2] < '2' && ut.release[3] == '.'))
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000757 feature_set = NULL;
Theodore Ts'o27401561999-09-14 20:11:19 +0000758#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000760 if (oldpath) {
761 char *newpath;
762
763 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
764 strcpy (newpath, PATH_SET);
765 strcat (newpath, ":");
766 strcat (newpath, oldpath);
767 putenv (newpath);
768 } else
769 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000770
771 setbuf(stdout, NULL);
772 setbuf(stderr, NULL);
773 initialize_ext2_error_table();
774 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000775 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000776
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000777 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778 E2FSPROGS_VERSION, E2FSPROGS_DATE,
779 EXT2FS_VERSION, EXT2FS_DATE);
780 if (argc && *argv)
781 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000782 while ((c = getopt (argc, argv,
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000783 "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 +0000784 switch (c) {
785 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000786 blocksize = strtoul(optarg, &tmp, 0);
787 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000788 com_err(program_name, 0,
789 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000790 exit(1);
791 }
792 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000793 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000794 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 break;
796 case 'c':
797 case 't': /* Check for bad blocks */
798 cflag = 1;
799 break;
800 case 'f':
801 size = strtoul(optarg, &tmp, 0);
802 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000803 com_err(program_name, 0,
804 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000805 optarg);
806 exit(1);
807 }
808 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000809 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000810 printf(_("Warning: fragments not supported. "
811 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000812 break;
813 case 'g':
814 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000815 if (*tmp) {
816 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000817 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000818 exit(1);
819 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000820 if ((param.s_blocks_per_group % 8) != 0) {
821 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000822 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 exit(1);
824 }
825 break;
826 case 'i':
827 inode_ratio = strtoul(optarg, &tmp, 0);
828 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
829 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000830 com_err(program_name, 0,
831 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000832 exit(1);
833 }
834 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000835 case 'j':
836 journal_size = strtoul(optarg, &tmp, 0);
837 if (journal_size < 4 || journal_size > 100 || *tmp) {
838 com_err(program_name, 0,
839 _("bad journal size - %s"), optarg);
840 exit(1);
841 }
842 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000843 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000844 bad_blocks_filename = malloc(strlen(optarg)+1);
845 if (!bad_blocks_filename) {
846 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000847 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000848 exit(1);
849 }
850 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000851 break;
852 case 'm':
853 reserved_ratio = strtoul(optarg, &tmp, 0);
854 if (reserved_ratio > 50 || *tmp) {
855 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000856 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000857 optarg);
858 exit(1);
859 }
860 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000861 case 'n':
862 noaction++;
863 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000864 case 'o':
865 creator_os = optarg;
866 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000867 case 'r':
868 param.s_rev_level = atoi(optarg);
869 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000870 case 's':
871 sparse_option = atoi(optarg);
872 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000873#ifdef EXT2_DYNAMIC_REV
874 case 'I':
875 param.s_inode_size = atoi(optarg);
876 break;
877#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000878 case 'N':
879 num_inodes = atoi(optarg);
880 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000881 case 'v':
882 verbose = 1;
883 break;
884 case 'q':
885 quiet = 1;
886 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000887 case 'F':
888 force = 1;
889 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000890 case 'L':
891 volume_label = optarg;
892 break;
893 case 'M':
894 mount_dir = optarg;
895 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000896 case 'O':
897 feature_set = optarg;
898 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000899 case 'R':
900 raid_opts = optarg;
901 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000902 case 'S':
903 super_only = 1;
904 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000905 case 'T':
906 fs_type = optarg;
907 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000908 case 'V':
909 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000910 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000911 error_message(EXT2_ET_BASE));
912 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000913 default:
914 usage();
915 }
916 if (optind == argc)
917 usage();
918 device_name = argv[optind];
919 optind++;
920 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000921 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
922
923 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000924 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925 argv[optind - 1]);
926 exit(1);
927 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000928 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929 }
930 if (optind < argc)
931 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000932
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000933 if (raid_opts)
934 parse_raid_opts(raid_opts);
935
Theodore Ts'o74becf31997-04-26 14:37:06 +0000936 if (!force)
937 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000938 check_mount();
939
Theodore Ts'o3839e651997-04-26 13:21:57 +0000940 param.s_log_frag_size = param.s_log_block_size;
941
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000942 if (noaction && param.s_blocks_count) {
943 dev_size = param.s_blocks_count;
944 retval = 0;
945 } else
946 retval = ext2fs_get_device_size(device_name,
947 EXT2_BLOCK_SIZE(&param),
948 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000949 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
950 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000951 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000952 exit(1);
953 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000954 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000955 if (retval == EXT2_ET_UNIMPLEMENTED) {
956 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000957 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +0000958 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000959 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000960 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +0000961 } else {
962 if (dev_size == 0) {
963 com_err(program_name, 0,
964 _("Device size reported to be zero. "
965 "Invalid partition specified, or\n\t"
966 "partition table wasn't reread "
967 "after running fdisk, due to\n\t"
968 "a modified partition being busy "
969 "and in use. You may need to reboot\n\t"
970 "to re-read your partition table.\n"
971 ));
972 exit(1);
973 }
Theodore Ts'oa789d841998-03-30 01:20:55 +0000974 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +0000975 }
976
Theodore Ts'oa789d841998-03-30 01:20:55 +0000977 } else if (!force && (param.s_blocks_count > dev_size)) {
978 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000979 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000980 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000981 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000982
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000983 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
984
Theodore Ts'o521e3681997-04-29 17:48:10 +0000985 if (param.s_blocks_per_group) {
986 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000987 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000988 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000989 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +0000990 exit(1);
991 }
992 }
993
Theodore Ts'o3839e651997-04-26 13:21:57 +0000994 /*
995 * Calculate number of inodes based on the inode ratio
996 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000997 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +0000998 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000999 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000
1001 /*
1002 * Calculate number of blocks to reserve
1003 */
1004 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001005
Theodore Ts'o521e3681997-04-29 17:48:10 +00001006#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +00001007 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +00001008 param_ext2->s_feature_ro_compat |=
1009 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1010#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +00001011 if (feature_set && !strncasecmp(feature_set, "none", 4))
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001012 feature_set = NULL;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001013 if (feature_set && e2p_edit_feature(feature_set,
1014 &param_ext2->s_feature_compat,
1015 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001016 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +00001017 feature_set);
1018 exit(1);
1019 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001020}
1021
1022int main (int argc, char *argv[])
1023{
1024 errcode_t retval = 0;
1025 ext2_filsys fs;
1026 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001027 int journal_blocks;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001028 struct ext2fs_sb *s;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001029
1030#ifdef ENABLE_NLS
1031 setlocale(LC_MESSAGES, "");
1032 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1033 textdomain(NLS_CAT_NAME);
1034#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001035 PRS(argc, argv);
1036
Theodore Ts'o3839e651997-04-26 13:21:57 +00001037 /*
1038 * Initialize the superblock....
1039 */
1040 retval = ext2fs_initialize(device_name, 0, &param,
1041 unix_io_manager, &fs);
1042 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001043 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001044 exit(1);
1045 }
1046
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001047 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001048 * Wipe out the old on-disk superblock
1049 */
1050 zap_sector(fs, 2);
1051
1052 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001053 * Generate a UUID for it...
1054 */
1055 s = (struct ext2fs_sb *) fs->super;
1056 uuid_generate(s->s_uuid);
1057
1058 /*
1059 * Override the creator OS, if applicable
1060 */
1061 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001062 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001063 exit(1);
1064 }
1065
1066 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001067 * For the Hurd, we will turn off filetype since it doesn't
1068 * support it.
1069 */
1070 if (fs->super->s_creator_os == EXT2_OS_HURD)
1071 fs->super->s_feature_incompat &=
1072 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1073
1074 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001075 * Set the volume label...
1076 */
1077 if (volume_label) {
1078 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1079 strncpy(s->s_volume_name, volume_label,
1080 sizeof(s->s_volume_name));
1081 }
1082
1083 /*
1084 * Set the last mount directory
1085 */
1086 if (mount_dir) {
1087 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1088 strncpy(s->s_last_mounted, mount_dir,
1089 sizeof(s->s_last_mounted));
1090 }
1091
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001092 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001093 show_stats(fs);
1094
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001095 if (noaction)
1096 exit(0);
1097
Theodore Ts'o3839e651997-04-26 13:21:57 +00001098 if (bad_blocks_filename)
1099 read_bb_file(fs, &bb_list, bad_blocks_filename);
1100 if (cflag)
1101 test_disk(fs, &bb_list);
1102
1103 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001104 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001105 retval = ext2fs_allocate_tables(fs);
1106 if (retval) {
1107 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001108 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001109 exit(1);
1110 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001111 if (super_only) {
1112 fs->super->s_state |= EXT2_ERROR_FS;
1113 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1114 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001115 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001116 create_root_dir(fs);
1117 create_lost_and_found(fs);
1118 reserve_inodes(fs);
1119 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001120#ifdef ZAP_BOOTBLOCK
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001121 zap_sector(fs, 0);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001122#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001123 }
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001124 if (journal_size) {
1125 if (!quiet)
1126 printf(_("Creating journal: "));
1127 journal_blocks = journal_size * 1024 /
1128 (fs->blocksize / 1024);
1129 retval = ext2fs_add_journal_fs(fs, journal_blocks);
1130 if (retval) {
1131 com_err (program_name, retval,
1132 _("while trying to create journal"));
1133 exit(1);
1134 }
1135 if (!quiet)
1136 printf(_("done\n"));
1137 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001138
1139 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001140 printf(_("Writing superblocks and "
1141 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001142 retval = ext2fs_flush(fs);
1143 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001144 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001145 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001146 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001147 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001148 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001149 return 0;
1150}