blob: 820b9d26e9c35e407e1779c0a8b2a800a9463978 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000023#ifdef linux
24#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000028#endif
29#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_ERRNO_H
36#include <errno.h>
37#endif
38#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000040#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000041#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000042#include <sys/types.h>
43
Theodore Ts'of3db3561997-04-26 13:34:30 +000044#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000045#ifdef HAVE_LINUX_MAJOR_H
46#include <linux/major.h>
Theodore Ts'oe6597041999-10-26 02:30:16 +000047#include <sys/stat.h> /* Only need sys/stat.h for major nr test */
Theodore Ts'o74becf31997-04-26 14:37:06 +000048#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
50#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000051#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000052#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "ext2fs/ext2fs.h"
54#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000055#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000057/* Everything is STDC, these days */
58#define NOARGS void
59
Theodore Ts'o3839e651997-04-26 13:21:57 +000060#define STRIDE_LENGTH 8
61
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000062#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000063#define ZAP_BOOTBLOCK
64#endif
65
Theodore Ts'o3839e651997-04-26 13:21:57 +000066extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000067extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
69const char * program_name = "mke2fs";
70const char * device_name = NULL;
71
72/* Command line options */
73int cflag = 0;
74int verbose = 0;
75int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000076int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000077int force = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +000078int noaction = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000080__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
82struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000083char *creator_os = NULL;
84char *volume_label = NULL;
85char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000087static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
88
Theodore Ts'o3839e651997-04-26 13:21:57 +000089static void usage(NOARGS)
90{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000091 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000092 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000093 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
94 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000095 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
96 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000097 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 program_name);
99 exit(1);
100}
101
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000102static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103{
104 int l = 0;
105
106 arg >>= 1;
107 while (arg) {
108 l++;
109 arg >>= 1;
110 }
111 return l;
112}
113
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000114static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000115{
116 int l;
117
118 for (l=0; arg ; l++)
119 arg = arg / 10;
120 return l;
121}
122
Theodore Ts'oa789d841998-03-30 01:20:55 +0000123static void proceed_question(NOARGS)
124{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000125 int c;
126 char *short_yes = _("yY");
127
Theodore Ts'oa789d841998-03-30 01:20:55 +0000128 fflush(stdout);
129 fflush(stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000130 printf(_("Proceed anyway? (y,n) "));
131 c = getchar();
132 if (strchr(short_yes, (char) c))
Theodore Ts'oa789d841998-03-30 01:20:55 +0000133 exit(1);
134}
135
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000136#ifndef SCSI_BLK_MAJOR
137#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
138#endif
139
Theodore Ts'o74becf31997-04-26 14:37:06 +0000140static void check_plausibility(NOARGS)
141{
142#ifdef HAVE_LINUX_MAJOR_H
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000143#ifndef MAJOR
144#define MAJOR(dev) ((dev)>>8)
145#define MINOR(dev) ((dev) & 0xff)
146#endif
147
Theodore Ts'o74becf31997-04-26 14:37:06 +0000148 int val;
149 struct stat s;
150
151 val = stat(device_name, &s);
152
153 if(val == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000154 fprintf(stderr, _("Could not stat %s --- %s\n"),
Theodore Ts'oa789d841998-03-30 01:20:55 +0000155 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000156 if (errno == ENOENT)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000157 fprintf(stderr, _("\nThe device apparently does "
158 "not exist; did you specify it correctly?\n"));
Theodore Ts'o74becf31997-04-26 14:37:06 +0000159 exit(1);
160 }
161 if(!S_ISBLK(s.st_mode)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000162 printf(_("%s is not a block special device.\n"), device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000163 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000164 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000165 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
166 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000167 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
168 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000169 printf(_("%s is entire device, not just one partition!\n"),
Theodore Ts'o74becf31997-04-26 14:37:06 +0000170 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000171 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000172 }
173#endif
174}
175
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176static void check_mount(NOARGS)
177{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000178 errcode_t retval;
179 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000181 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
182 if (retval) {
183 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000184 _("while determining whether %s is mounted."),
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000185 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000187 }
188 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000190
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000191 fprintf(stderr, _("%s is mounted; "), device_name);
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000192 if (force) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000193 fprintf(stderr, _("mke2fs forced anyway. "
194 "Hope /etc/mtab is incorrect.\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000195 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000196 fprintf(stderr, _("will not make a filesystem here!\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000197 exit(1);
198 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199}
200
201/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000202 * This function sets the default parameters for a filesystem
203 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000204 * The type is specified by the user. The size is the maximum size
205 * (in megabytes) for which a set of parameters applies, with a size
206 * of zero meaning that it is the default parameter for the type.
207 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000208 */
209static char default_str[] = "default";
210struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000211 const char *type;
212 int size;
213 int blocksize;
214 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000215} settings[] = {
216 { default_str, 0, 4096, 8192 },
217 { default_str, 512, 1024, 4096 },
218 { default_str, 3, 1024, 8192 },
219 { "news", 0, 4096, 4096 },
220 { 0, 0, 0, 0},
221};
222
Theodore Ts'o9094f281999-10-26 16:42:50 +0000223static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000224 int blocksize, int *inode_ratio)
225{
226 int megs;
227 int ratio = 0;
228 struct mke2fs_defaults *p;
229
Theodore Ts'o9094f281999-10-26 16:42:50 +0000230 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000231 1024);
232 if (inode_ratio)
233 ratio = *inode_ratio;
234 if (!fs_type)
235 fs_type = default_str;
236 for (p = settings; p->type; p++) {
237 if ((strcmp(p->type, fs_type) != 0) &&
238 (strcmp(p->type, default_str) != 0))
239 continue;
240 if ((p->size != 0) &&
241 (megs > p->size))
242 continue;
243 if (ratio == 0)
244 *inode_ratio = p->inode_ratio;
245 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000246 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000247 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000248 }
249 }
250 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000251 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000252}
253
254/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255 * Helper function for read_bb_file and test_disk
256 */
257static void invalid_block(ext2_filsys fs, blk_t blk)
258{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000259 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000260 return;
261}
262
263/*
264 * Reads the bad blocks list from a file
265 */
266static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
267 const char *bad_blocks_file)
268{
269 FILE *f;
270 errcode_t retval;
271
272 f = fopen(bad_blocks_file, "r");
273 if (!f) {
274 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000275 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 exit(1);
277 }
278 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
279 fclose (f);
280 if (retval) {
281 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000282 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283 exit(1);
284 }
285}
286
287/*
288 * Runs the badblocks program to test the disk
289 */
290static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
291{
292 FILE *f;
293 errcode_t retval;
294 char buf[1024];
295
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000296 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
297 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298 fs->super->s_blocks_count);
299 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000300 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 f = popen(buf, "r");
302 if (!f) {
303 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000304 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 exit(1);
306 }
307 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000308 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 if (retval) {
310 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000311 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 exit(1);
313 }
314}
315
316static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
317{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000318 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 int must_be_good;
320 blk_t blk;
321 badblocks_iterate bb_iter;
322 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000323 blk_t group_block;
324 int group;
325 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326
327 if (!bb_list)
328 return;
329
330 /*
331 * The primary superblock and group descriptors *must* be
332 * good; if not, abort.
333 */
334 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
335 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
336 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000337 fprintf(stderr, _("Block %d in primary "
338 "superblock/group descriptor area bad.\n"), i);
339 fprintf(stderr, _("Blocks %d through %d must be good "
340 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000342 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000343 exit(1);
344 }
345 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000346
347 /*
348 * See if any of the bad blocks are showing up in the backup
349 * superblocks and/or group descriptors. If so, issue a
350 * warning and adjust the block counts appropriately.
351 */
352 group_block = fs->super->s_first_data_block +
353 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000354
355 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000356 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000357 for (j=0; j < fs->desc_blocks+1; j++) {
358 if (badblocks_list_test(bb_list, group_block +
359 j)) {
360 if (!group_bad)
361 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000362_("Warning: the backup superblock/group descriptors at block %d contain\n"
363" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000364 group_block);
365 group_bad++;
366 group = ext2fs_group_of_blk(fs, group_block+j);
367 fs->group_desc[group].bg_free_blocks_count++;
368 fs->super->s_free_blocks_count++;
369 }
370 }
371 group_block += fs->super->s_blocks_per_group;
372 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000373
374 /*
375 * Mark all the bad blocks as used...
376 */
377 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
378 if (retval) {
379 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000380 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381 exit(1);
382 }
383 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000384 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 badblocks_list_iterate_end(bb_iter);
386}
387
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000388static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389{
390 errcode_t retval;
391 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000392 int i, j, num, count;
393 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000394 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000395 int sync_kludge = 0;
396 char *mke2fs_sync;
397
398 mke2fs_sync = getenv("MKE2FS_SYNC");
399 if (mke2fs_sync)
400 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401
402 buf = malloc(fs->blocksize * STRIDE_LENGTH);
403 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000404 com_err("malloc", ENOMEM,
405 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 exit(1);
407 }
408 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000409
410 /*
411 * Figure out how many digits we need
412 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000413 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000414 sprintf(format, "%%%dd/%%%dld", i, i);
415 memset(backup, '\b', sizeof(backup)-1);
416 backup[sizeof(backup)-1] = 0;
417 if ((2*i)+1 < sizeof(backup))
418 backup[(2*i)+1] = 0;
419
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000421 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 for (i = 0; i < fs->group_desc_count; i++) {
423 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000424 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000426 blk = fs->group_desc[i].bg_inode_table;
427 num = fs->inode_blocks_per_group;
428
429 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
430 if (num-j > STRIDE_LENGTH)
431 count = STRIDE_LENGTH;
432 else
433 count = num - j;
434 retval = io_channel_write_blk(fs->io, blk, count, buf);
435 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000436 printf(_("Warning: could not write %d blocks "
437 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000438 count, blk, error_message(retval));
439 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000441 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000442 if (sync_kludge) {
443 if (sync_kludge == 1)
444 sync();
445 else if ((i % sync_kludge) == 0)
446 sync();
447 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000449 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000451 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452}
453
454static void create_root_dir(ext2_filsys fs)
455{
456 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000457 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458
459 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
460 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000461 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 exit(1);
463 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000464 if (geteuid()) {
465 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
466 if (retval) {
467 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000468 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 exit(1);
470 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000471 inode.i_uid = getuid();
472 if (inode.i_uid)
473 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000474 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
475 if (retval) {
476 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000477 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000478 exit(1);
479 }
480 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481}
482
483static void create_lost_and_found(ext2_filsys fs)
484{
485 errcode_t retval;
486 ino_t ino;
487 const char *name = "lost+found";
488 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000489 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490
491 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
492 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000493 com_err("ext2fs_mkdir", retval,
494 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 exit(1);
496 }
497
498 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
499 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000500 com_err("ext2_lookup", retval,
501 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 exit(1);
503 }
504
505 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000506 if ((lpf_size += fs->blocksize) >= 16*1024)
507 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 retval = ext2fs_expand_dir(fs, ino);
509 if (retval) {
510 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000511 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 exit(1);
513 }
514 }
515}
516
517static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
518{
519 errcode_t retval;
520
Theodore Ts'of3db3561997-04-26 13:34:30 +0000521 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 fs->group_desc[0].bg_free_inodes_count--;
523 fs->super->s_free_inodes_count--;
524 retval = ext2fs_update_bb_inode(fs, bb_list);
525 if (retval) {
526 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000527 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 exit(1);
529 }
530
531}
532
533static void reserve_inodes(ext2_filsys fs)
534{
535 ino_t i;
536 int group;
537
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000538 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000539 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 group = ext2fs_group_of_ino(fs, i);
541 fs->group_desc[group].bg_free_inodes_count--;
542 fs->super->s_free_inodes_count--;
543 }
544 ext2fs_mark_ib_dirty(fs);
545}
546
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000547#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000548static void zap_bootblock(ext2_filsys fs)
549{
550 char buf[512];
551 int retval;
552
553 memset(buf, 0, 512);
554
555 retval = io_channel_write_blk(fs->io, 0, -512, buf);
556 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000557 printf(_("Warning: could not erase block 0: %s\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000558 error_message(retval));
559}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000560#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000561
562
Theodore Ts'o3839e651997-04-26 13:21:57 +0000563static void show_stats(ext2_filsys fs)
564{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000565 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
566 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000568 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569
570 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000571 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000573
574 memset(buf, 0, sizeof(buf));
575 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000576 printf(_("Filesystem label=%s\n"), buf);
577 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000578 switch (fs->super->s_creator_os) {
579 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000580 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000581 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000582 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000583 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000584 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000585 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000586 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000587 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000588 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000589 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000591 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 s->s_r_blocks_count,
593 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000594 printf(_("First data block=%u\n"), s->s_first_data_block);
595 if (fs->group_desc_count > 1)
596 printf(_("%lu block groups\n"), fs->group_desc_count);
597 else
598 printf(_("%lu block group\n"), fs->group_desc_count);
599 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000601 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602
603 if (fs->group_desc_count == 1) {
604 printf("\n");
605 return;
606 }
607
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000608 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 group_block = s->s_first_data_block;
610 col_left = 0;
611 for (i = 1; i < fs->group_desc_count; i++) {
612 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000613 if (!ext2fs_bg_has_super(fs, i))
614 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000615 if (i != 1)
616 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000617 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000618 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000620 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000622 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000623 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 }
625 printf("\n\n");
626}
627
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000628#ifndef HAVE_STRCASECMP
629static int strcasecmp (char *s1, char *s2)
630{
631 while (*s1 && *s2) {
632 int ch1 = *s1++, ch2 = *s2++;
633 if (isupper (ch1))
634 ch1 = tolower (ch1);
635 if (isupper (ch2))
636 ch2 = tolower (ch2);
637 if (ch1 != ch2)
638 return ch1 - ch2;
639 }
640 return *s1 ? 1 : *s2 ? -1 : 0;
641}
642#endif
643
644/*
645 * Set the S_CREATOR_OS field. Return true if OS is known,
646 * otherwise, 0.
647 */
648static int set_os(struct ext2_super_block *sb, char *os)
649{
650 if (isdigit (*os))
651 sb->s_creator_os = atoi (os);
652 else if (strcasecmp(os, "linux") == 0)
653 sb->s_creator_os = EXT2_OS_LINUX;
654 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
655 sb->s_creator_os = EXT2_OS_HURD;
656 else if (strcasecmp(os, "masix") == 0)
657 sb->s_creator_os = EXT2_OS_MASIX;
658 else
659 return 0;
660 return 1;
661}
662
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000663#define PATH_SET "PATH=/sbin"
664
Theodore Ts'od163b091997-10-03 17:42:28 +0000665static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000666{
667 char *buf, *token, *next, *p, *arg;
668 int len;
669 int raid_usage = 0;
670
671 len = strlen(opts);
672 buf = malloc(len+1);
673 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000674 fprintf(stderr, _("Couldn't allocate memory to parse "
675 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000676 exit(1);
677 }
678 strcpy(buf, opts);
679 for (token = buf; token && *token; token = next) {
680 p = strchr(token, ',');
681 next = 0;
682 if (p) {
683 *p = 0;
684 next = p+1;
685 }
686 arg = strchr(token, '=');
687 if (arg) {
688 *arg = 0;
689 arg++;
690 }
691 if (strcmp(token, "stride") == 0) {
692 if (!arg) {
693 raid_usage++;
694 continue;
695 }
696 fs_stride = strtoul(arg, &p, 0);
697 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000698 fprintf(stderr,
699 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000700 raid_usage++;
701 continue;
702 }
703 } else
704 raid_usage++;
705 }
706 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000707 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000708 "Raid options are separated by commas, "
709 "and may take an argument which\n"
710 "\tis set off by an equals ('=') sign.\n\n"
711 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000712 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000713 exit(1);
714 }
715}
716
Theodore Ts'o896938d1999-10-23 01:04:50 +0000717static __u32 ok_features[3] = {
718 0, /* Compat */
719 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
720 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
721};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000722
723
Theodore Ts'o3839e651997-04-26 13:21:57 +0000724static void PRS(int argc, char *argv[])
725{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000726 int c;
727 int size;
728 char * tmp;
729 blk_t max = 8192;
730 int blocksize = 0;
731 int inode_ratio = 0;
732 int reserved_ratio = 5;
733 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000734 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000735 int sparse_option = 0;
736 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000737 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000738 char * raid_opts = 0;
739 char * fs_type = 0;
740 const char * feature_set = "filetype,sparse_super";
741 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000742#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000743 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000744
745 if (uname(&ut)) {
746 perror("uname");
747 exit(1);
748 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000749 if ((ut.release[0] == '1') ||
750 (ut.release[0] == '2' && ut.release[1] == '.' &&
751 ut.release[2] < '2' && ut.release[3] == '.'))
752 feature_set = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000753#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000755 if (oldpath) {
756 char *newpath;
757
758 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
759 strcpy (newpath, PATH_SET);
760 strcat (newpath, ":");
761 strcat (newpath, oldpath);
762 putenv (newpath);
763 } else
764 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000765
766 setbuf(stdout, NULL);
767 setbuf(stderr, NULL);
768 initialize_ext2_error_table();
769 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000770 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000771
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000772 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000773 E2FSPROGS_VERSION, E2FSPROGS_DATE,
774 EXT2FS_VERSION, EXT2FS_DATE);
775 if (argc && *argv)
776 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000777 while ((c = getopt (argc, argv,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000778 "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000779 switch (c) {
780 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000781 blocksize = strtoul(optarg, &tmp, 0);
782 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000783 com_err(program_name, 0,
784 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 exit(1);
786 }
787 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000788 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000789 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000790 break;
791 case 'c':
792 case 't': /* Check for bad blocks */
793 cflag = 1;
794 break;
795 case 'f':
796 size = strtoul(optarg, &tmp, 0);
797 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000798 com_err(program_name, 0,
799 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000800 optarg);
801 exit(1);
802 }
803 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000804 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000805 printf(_("Warning: fragments not supported. "
806 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000807 break;
808 case 'g':
809 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000810 if (*tmp) {
811 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000812 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000813 exit(1);
814 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000815 if ((param.s_blocks_per_group % 8) != 0) {
816 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000817 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000818 exit(1);
819 }
820 break;
821 case 'i':
822 inode_ratio = strtoul(optarg, &tmp, 0);
823 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
824 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000825 com_err(program_name, 0,
826 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827 exit(1);
828 }
829 break;
830 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000831 bad_blocks_filename = malloc(strlen(optarg)+1);
832 if (!bad_blocks_filename) {
833 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000834 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000835 exit(1);
836 }
837 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 break;
839 case 'm':
840 reserved_ratio = strtoul(optarg, &tmp, 0);
841 if (reserved_ratio > 50 || *tmp) {
842 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000843 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844 optarg);
845 exit(1);
846 }
847 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000848 case 'n':
849 noaction++;
850 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000851 case 'o':
852 creator_os = optarg;
853 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000854 case 'r':
855 param.s_rev_level = atoi(optarg);
856 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000857 case 's':
858 sparse_option = atoi(optarg);
859 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000860#ifdef EXT2_DYNAMIC_REV
861 case 'I':
862 param.s_inode_size = atoi(optarg);
863 break;
864#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000865 case 'N':
866 num_inodes = atoi(optarg);
867 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868 case 'v':
869 verbose = 1;
870 break;
871 case 'q':
872 quiet = 1;
873 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000874 case 'F':
875 force = 1;
876 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000877 case 'L':
878 volume_label = optarg;
879 break;
880 case 'M':
881 mount_dir = optarg;
882 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000883 case 'O':
884 feature_set = optarg;
885 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000886 case 'R':
887 raid_opts = optarg;
888 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000889 case 'S':
890 super_only = 1;
891 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000892 case 'T':
893 fs_type = optarg;
894 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000895 case 'V':
896 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000897 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000898 error_message(EXT2_ET_BASE));
899 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 default:
901 usage();
902 }
903 if (optind == argc)
904 usage();
905 device_name = argv[optind];
906 optind++;
907 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000908 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
909
910 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000911 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000912 argv[optind - 1]);
913 exit(1);
914 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000915 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 }
917 if (optind < argc)
918 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000919
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000920 if (raid_opts)
921 parse_raid_opts(raid_opts);
922
Theodore Ts'o74becf31997-04-26 14:37:06 +0000923 if (!force)
924 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000925 check_mount();
926
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 param.s_log_frag_size = param.s_log_block_size;
928
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000929 if (noaction && param.s_blocks_count) {
930 dev_size = param.s_blocks_count;
931 retval = 0;
932 } else
933 retval = ext2fs_get_device_size(device_name,
934 EXT2_BLOCK_SIZE(&param),
935 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000936 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
937 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000938 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000939 exit(1);
940 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000941 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000942 if (retval == EXT2_ET_UNIMPLEMENTED) {
943 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000944 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +0000945 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000946 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000947 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000948 } else
949 param.s_blocks_count = dev_size;
950 } else if (!force && (param.s_blocks_count > dev_size)) {
951 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000952 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000953 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000954 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000955
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000956 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
957
Theodore Ts'o521e3681997-04-29 17:48:10 +0000958 if (param.s_blocks_per_group) {
959 if (param.s_blocks_per_group < 256 ||
960 param.s_blocks_per_group > max || *tmp) {
961 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000962 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +0000963 exit(1);
964 }
965 }
966
Theodore Ts'o3839e651997-04-26 13:21:57 +0000967 /*
968 * Calculate number of inodes based on the inode ratio
969 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000970 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +0000971 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000972 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973
974 /*
975 * Calculate number of blocks to reserve
976 */
977 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000978
Theodore Ts'o521e3681997-04-29 17:48:10 +0000979#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000980 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000981 param_ext2->s_feature_ro_compat |=
982 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
983#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +0000984 if (feature_set && !strncasecmp(feature_set, "none", 4))
985 feature_set = 0;
986 if (feature_set && e2p_edit_feature(feature_set,
987 &param_ext2->s_feature_compat,
988 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000989 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +0000990 feature_set);
991 exit(1);
992 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993}
994
995int main (int argc, char *argv[])
996{
997 errcode_t retval = 0;
998 ext2_filsys fs;
999 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001000 struct ext2fs_sb *s;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001001
1002#ifdef ENABLE_NLS
1003 setlocale(LC_MESSAGES, "");
1004 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1005 textdomain(NLS_CAT_NAME);
1006#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007 PRS(argc, argv);
1008
Theodore Ts'o3839e651997-04-26 13:21:57 +00001009 /*
1010 * Initialize the superblock....
1011 */
1012 retval = ext2fs_initialize(device_name, 0, &param,
1013 unix_io_manager, &fs);
1014 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001015 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001016 exit(1);
1017 }
1018
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001019 /*
1020 * Generate a UUID for it...
1021 */
1022 s = (struct ext2fs_sb *) fs->super;
1023 uuid_generate(s->s_uuid);
1024
1025 /*
1026 * Override the creator OS, if applicable
1027 */
1028 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001029 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001030 exit(1);
1031 }
1032
1033 /*
1034 * Set the volume label...
1035 */
1036 if (volume_label) {
1037 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1038 strncpy(s->s_volume_name, volume_label,
1039 sizeof(s->s_volume_name));
1040 }
1041
1042 /*
1043 * Set the last mount directory
1044 */
1045 if (mount_dir) {
1046 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1047 strncpy(s->s_last_mounted, mount_dir,
1048 sizeof(s->s_last_mounted));
1049 }
1050
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001051 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 show_stats(fs);
1053
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001054 if (noaction)
1055 exit(0);
1056
Theodore Ts'o3839e651997-04-26 13:21:57 +00001057 if (bad_blocks_filename)
1058 read_bb_file(fs, &bb_list, bad_blocks_filename);
1059 if (cflag)
1060 test_disk(fs, &bb_list);
1061
1062 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001063 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001064 retval = ext2fs_allocate_tables(fs);
1065 if (retval) {
1066 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001067 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001068 exit(1);
1069 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001070 if (super_only) {
1071 fs->super->s_state |= EXT2_ERROR_FS;
1072 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1073 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001074 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001075 create_root_dir(fs);
1076 create_lost_and_found(fs);
1077 reserve_inodes(fs);
1078 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001079#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001080 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001081#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001082 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001083
1084 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001085 printf(_("Writing superblocks and "
1086 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001087 retval = ext2fs_flush(fs);
1088 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001089 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001090 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001091 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001092 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001093 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001094 return 0;
1095}