blob: 5d012c1ee812ab10bccbde418dbdaad191550f03 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000023#ifdef linux
24#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Theodore Ts'of3db3561997-04-26 13:34:30 +000047#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000048#ifdef HAVE_LINUX_MAJOR_H
49#include <linux/major.h>
Theodore Ts'oe6597041999-10-26 02:30:16 +000050#include <sys/stat.h> /* Only need sys/stat.h for major nr test */
Theodore Ts'o74becf31997-04-26 14:37:06 +000051#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
53#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000054#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000055#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "ext2fs/ext2fs.h"
57#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000058#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000060/* Everything is STDC, these days */
61#define NOARGS void
62
Theodore Ts'o3839e651997-04-26 13:21:57 +000063#define STRIDE_LENGTH 8
64
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000065#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000066#define ZAP_BOOTBLOCK
67#endif
68
Theodore Ts'o3839e651997-04-26 13:21:57 +000069extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000070extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
72const char * program_name = "mke2fs";
73const char * device_name = NULL;
74
75/* Command line options */
76int cflag = 0;
77int verbose = 0;
78int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000079int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000080int force = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +000081int noaction = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000083__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
85struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000086char *creator_os = NULL;
87char *volume_label = NULL;
88char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000089
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000090static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
91
Theodore Ts'o3839e651997-04-26 13:21:57 +000092static void usage(NOARGS)
93{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000094 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000095 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000096 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
97 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000098 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
99 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000100 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 program_name);
102 exit(1);
103}
104
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000105static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106{
107 int l = 0;
108
109 arg >>= 1;
110 while (arg) {
111 l++;
112 arg >>= 1;
113 }
114 return l;
115}
116
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000117static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000118{
119 int l;
120
121 for (l=0; arg ; l++)
122 arg = arg / 10;
123 return l;
124}
125
Theodore Ts'oa789d841998-03-30 01:20:55 +0000126static void proceed_question(NOARGS)
127{
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000128 char buf[256];
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000129 char *short_yes = _("yY");
130
Theodore Ts'oa789d841998-03-30 01:20:55 +0000131 fflush(stdout);
132 fflush(stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000133 printf(_("Proceed anyway? (y,n) "));
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000134 buf[0] = 0;
135 fgets(buf, sizeof(buf), stdin);
136 if (strchr(short_yes, buf[0]) == 0)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000137 exit(1);
138}
139
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000140#ifndef SCSI_BLK_MAJOR
141#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
142#endif
143
Theodore Ts'o74becf31997-04-26 14:37:06 +0000144static void check_plausibility(NOARGS)
145{
146#ifdef HAVE_LINUX_MAJOR_H
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000147#ifndef MAJOR
148#define MAJOR(dev) ((dev)>>8)
149#define MINOR(dev) ((dev) & 0xff)
150#endif
151
Theodore Ts'o74becf31997-04-26 14:37:06 +0000152 int val;
153 struct stat s;
154
155 val = stat(device_name, &s);
156
157 if(val == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000158 fprintf(stderr, _("Could not stat %s --- %s\n"),
Theodore Ts'oa789d841998-03-30 01:20:55 +0000159 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000160 if (errno == ENOENT)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000161 fprintf(stderr, _("\nThe device apparently does "
162 "not exist; did you specify it correctly?\n"));
Theodore Ts'o74becf31997-04-26 14:37:06 +0000163 exit(1);
164 }
165 if(!S_ISBLK(s.st_mode)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000166 printf(_("%s is not a block special device.\n"), device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000167 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000168 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000169 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
170 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000171 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
172 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000173 printf(_("%s is entire device, not just one partition!\n"),
Theodore Ts'o74becf31997-04-26 14:37:06 +0000174 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000175 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000176 }
177#endif
178}
179
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180static void check_mount(NOARGS)
181{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000182 errcode_t retval;
183 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000185 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
186 if (retval) {
187 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000188 _("while determining whether %s is mounted."),
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000189 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000191 }
192 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000194
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000195 fprintf(stderr, _("%s is mounted; "), device_name);
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000196 if (force) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000197 fprintf(stderr, _("mke2fs forced anyway. "
198 "Hope /etc/mtab is incorrect.\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000199 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000200 fprintf(stderr, _("will not make a filesystem here!\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000201 exit(1);
202 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203}
204
205/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000206 * This function sets the default parameters for a filesystem
207 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000208 * The type is specified by the user. The size is the maximum size
209 * (in megabytes) for which a set of parameters applies, with a size
210 * of zero meaning that it is the default parameter for the type.
211 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000212 */
213static char default_str[] = "default";
214struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000215 const char *type;
216 int size;
217 int blocksize;
218 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000219} settings[] = {
220 { default_str, 0, 4096, 8192 },
221 { default_str, 512, 1024, 4096 },
222 { default_str, 3, 1024, 8192 },
223 { "news", 0, 4096, 4096 },
224 { 0, 0, 0, 0},
225};
226
Theodore Ts'o9094f281999-10-26 16:42:50 +0000227static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000228 int blocksize, int *inode_ratio)
229{
230 int megs;
231 int ratio = 0;
232 struct mke2fs_defaults *p;
233
Theodore Ts'o9094f281999-10-26 16:42:50 +0000234 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000235 1024);
236 if (inode_ratio)
237 ratio = *inode_ratio;
238 if (!fs_type)
239 fs_type = default_str;
240 for (p = settings; p->type; p++) {
241 if ((strcmp(p->type, fs_type) != 0) &&
242 (strcmp(p->type, default_str) != 0))
243 continue;
244 if ((p->size != 0) &&
245 (megs > p->size))
246 continue;
247 if (ratio == 0)
248 *inode_ratio = p->inode_ratio;
249 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000250 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000251 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000252 }
253 }
254 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000255 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000256}
257
258/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 * Helper function for read_bb_file and test_disk
260 */
261static void invalid_block(ext2_filsys fs, blk_t blk)
262{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000263 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264 return;
265}
266
267/*
268 * Reads the bad blocks list from a file
269 */
270static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
271 const char *bad_blocks_file)
272{
273 FILE *f;
274 errcode_t retval;
275
276 f = fopen(bad_blocks_file, "r");
277 if (!f) {
278 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000279 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000280 exit(1);
281 }
282 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
283 fclose (f);
284 if (retval) {
285 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000286 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287 exit(1);
288 }
289}
290
291/*
292 * Runs the badblocks program to test the disk
293 */
294static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
295{
296 FILE *f;
297 errcode_t retval;
298 char buf[1024];
299
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000300 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
301 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302 fs->super->s_blocks_count);
303 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000304 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 f = popen(buf, "r");
306 if (!f) {
307 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000308 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 exit(1);
310 }
311 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000312 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 if (retval) {
314 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000315 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316 exit(1);
317 }
318}
319
320static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
321{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000322 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323 int must_be_good;
324 blk_t blk;
325 badblocks_iterate bb_iter;
326 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000327 blk_t group_block;
328 int group;
329 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330
331 if (!bb_list)
332 return;
333
334 /*
335 * The primary superblock and group descriptors *must* be
336 * good; if not, abort.
337 */
338 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
339 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
340 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000341 fprintf(stderr, _("Block %d in primary "
342 "superblock/group descriptor area bad.\n"), i);
343 fprintf(stderr, _("Blocks %d through %d must be good "
344 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000346 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347 exit(1);
348 }
349 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000350
351 /*
352 * See if any of the bad blocks are showing up in the backup
353 * superblocks and/or group descriptors. If so, issue a
354 * warning and adjust the block counts appropriately.
355 */
356 group_block = fs->super->s_first_data_block +
357 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000358
359 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000360 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000361 for (j=0; j < fs->desc_blocks+1; j++) {
362 if (badblocks_list_test(bb_list, group_block +
363 j)) {
364 if (!group_bad)
365 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000366_("Warning: the backup superblock/group descriptors at block %d contain\n"
367" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000368 group_block);
369 group_bad++;
370 group = ext2fs_group_of_blk(fs, group_block+j);
371 fs->group_desc[group].bg_free_blocks_count++;
372 fs->super->s_free_blocks_count++;
373 }
374 }
375 group_block += fs->super->s_blocks_per_group;
376 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377
378 /*
379 * Mark all the bad blocks as used...
380 */
381 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
382 if (retval) {
383 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000384 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 exit(1);
386 }
387 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000388 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389 badblocks_list_iterate_end(bb_iter);
390}
391
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000392static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393{
394 errcode_t retval;
395 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000396 int i, j, num, count;
397 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000398 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000399 int sync_kludge = 0;
400 char *mke2fs_sync;
401
402 mke2fs_sync = getenv("MKE2FS_SYNC");
403 if (mke2fs_sync)
404 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405
406 buf = malloc(fs->blocksize * STRIDE_LENGTH);
407 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000408 com_err("malloc", ENOMEM,
409 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 exit(1);
411 }
412 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000413
414 /*
415 * Figure out how many digits we need
416 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000417 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000418 sprintf(format, "%%%dd/%%%dld", i, i);
419 memset(backup, '\b', sizeof(backup)-1);
420 backup[sizeof(backup)-1] = 0;
421 if ((2*i)+1 < sizeof(backup))
422 backup[(2*i)+1] = 0;
423
Theodore Ts'o3839e651997-04-26 13:21:57 +0000424 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000425 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426 for (i = 0; i < fs->group_desc_count; i++) {
427 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000428 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000430 blk = fs->group_desc[i].bg_inode_table;
431 num = fs->inode_blocks_per_group;
432
433 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
434 if (num-j > STRIDE_LENGTH)
435 count = STRIDE_LENGTH;
436 else
437 count = num - j;
438 retval = io_channel_write_blk(fs->io, blk, count, buf);
439 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000440 printf(_("Warning: could not write %d blocks "
441 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000442 count, blk, error_message(retval));
443 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000445 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000446 if (sync_kludge) {
447 if (sync_kludge == 1)
448 sync();
449 else if ((i % sync_kludge) == 0)
450 sync();
451 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000453 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000455 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456}
457
458static void create_root_dir(ext2_filsys fs)
459{
460 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000461 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462
463 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
464 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000465 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 exit(1);
467 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000468 if (geteuid()) {
469 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
470 if (retval) {
471 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000472 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 exit(1);
474 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000475 inode.i_uid = getuid();
476 if (inode.i_uid)
477 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000478 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
479 if (retval) {
480 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000481 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 exit(1);
483 }
484 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485}
486
487static void create_lost_and_found(ext2_filsys fs)
488{
489 errcode_t retval;
490 ino_t ino;
491 const char *name = "lost+found";
492 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000493 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494
495 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
496 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000497 com_err("ext2fs_mkdir", retval,
498 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 exit(1);
500 }
501
502 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
503 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000504 com_err("ext2_lookup", retval,
505 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 exit(1);
507 }
508
509 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000510 if ((lpf_size += fs->blocksize) >= 16*1024)
511 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 retval = ext2fs_expand_dir(fs, ino);
513 if (retval) {
514 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000515 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 exit(1);
517 }
518 }
519}
520
521static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
522{
523 errcode_t retval;
524
Theodore Ts'of3db3561997-04-26 13:34:30 +0000525 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 fs->group_desc[0].bg_free_inodes_count--;
527 fs->super->s_free_inodes_count--;
528 retval = ext2fs_update_bb_inode(fs, bb_list);
529 if (retval) {
530 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000531 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 exit(1);
533 }
534
535}
536
537static void reserve_inodes(ext2_filsys fs)
538{
539 ino_t i;
540 int group;
541
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000542 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000543 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 group = ext2fs_group_of_ino(fs, i);
545 fs->group_desc[group].bg_free_inodes_count--;
546 fs->super->s_free_inodes_count--;
547 }
548 ext2fs_mark_ib_dirty(fs);
549}
550
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000551#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000552static void zap_bootblock(ext2_filsys fs)
553{
554 char buf[512];
555 int retval;
556
557 memset(buf, 0, 512);
558
559 retval = io_channel_write_blk(fs->io, 0, -512, buf);
560 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000561 printf(_("Warning: could not erase block 0: %s\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000562 error_message(retval));
563}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000564#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565
566
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567static void show_stats(ext2_filsys fs)
568{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000569 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
570 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000572 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573
574 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000575 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000577
578 memset(buf, 0, sizeof(buf));
579 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000580 printf(_("Filesystem label=%s\n"), buf);
581 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000582 switch (fs->super->s_creator_os) {
583 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000584 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000585 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000586 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000587 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000588 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000589 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000590 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000591 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000592 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000593 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000595 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596 s->s_r_blocks_count,
597 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000598 printf(_("First data block=%u\n"), s->s_first_data_block);
599 if (fs->group_desc_count > 1)
600 printf(_("%lu block groups\n"), fs->group_desc_count);
601 else
602 printf(_("%lu block group\n"), fs->group_desc_count);
603 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000605 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606
607 if (fs->group_desc_count == 1) {
608 printf("\n");
609 return;
610 }
611
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000612 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 group_block = s->s_first_data_block;
614 col_left = 0;
615 for (i = 1; i < fs->group_desc_count; i++) {
616 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000617 if (!ext2fs_bg_has_super(fs, i))
618 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000619 if (i != 1)
620 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000621 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000622 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000624 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000626 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000627 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 }
629 printf("\n\n");
630}
631
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000632#ifndef HAVE_STRCASECMP
633static int strcasecmp (char *s1, char *s2)
634{
635 while (*s1 && *s2) {
636 int ch1 = *s1++, ch2 = *s2++;
637 if (isupper (ch1))
638 ch1 = tolower (ch1);
639 if (isupper (ch2))
640 ch2 = tolower (ch2);
641 if (ch1 != ch2)
642 return ch1 - ch2;
643 }
644 return *s1 ? 1 : *s2 ? -1 : 0;
645}
646#endif
647
648/*
649 * Set the S_CREATOR_OS field. Return true if OS is known,
650 * otherwise, 0.
651 */
652static int set_os(struct ext2_super_block *sb, char *os)
653{
654 if (isdigit (*os))
655 sb->s_creator_os = atoi (os);
656 else if (strcasecmp(os, "linux") == 0)
657 sb->s_creator_os = EXT2_OS_LINUX;
658 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
659 sb->s_creator_os = EXT2_OS_HURD;
660 else if (strcasecmp(os, "masix") == 0)
661 sb->s_creator_os = EXT2_OS_MASIX;
662 else
663 return 0;
664 return 1;
665}
666
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000667#define PATH_SET "PATH=/sbin"
668
Theodore Ts'od163b091997-10-03 17:42:28 +0000669static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000670{
671 char *buf, *token, *next, *p, *arg;
672 int len;
673 int raid_usage = 0;
674
675 len = strlen(opts);
676 buf = malloc(len+1);
677 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000678 fprintf(stderr, _("Couldn't allocate memory to parse "
679 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000680 exit(1);
681 }
682 strcpy(buf, opts);
683 for (token = buf; token && *token; token = next) {
684 p = strchr(token, ',');
685 next = 0;
686 if (p) {
687 *p = 0;
688 next = p+1;
689 }
690 arg = strchr(token, '=');
691 if (arg) {
692 *arg = 0;
693 arg++;
694 }
695 if (strcmp(token, "stride") == 0) {
696 if (!arg) {
697 raid_usage++;
698 continue;
699 }
700 fs_stride = strtoul(arg, &p, 0);
701 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000702 fprintf(stderr,
703 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000704 raid_usage++;
705 continue;
706 }
707 } else
708 raid_usage++;
709 }
710 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000711 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000712 "Raid options are separated by commas, "
713 "and may take an argument which\n"
714 "\tis set off by an equals ('=') sign.\n\n"
715 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000716 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000717 exit(1);
718 }
719}
720
Theodore Ts'o896938d1999-10-23 01:04:50 +0000721static __u32 ok_features[3] = {
722 0, /* Compat */
723 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
724 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
725};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000726
727
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728static void PRS(int argc, char *argv[])
729{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000730 int c;
731 int size;
732 char * tmp;
733 blk_t max = 8192;
734 int blocksize = 0;
735 int inode_ratio = 0;
736 int reserved_ratio = 5;
737 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000738 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000739 int sparse_option = 0;
740 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000741 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000742 char * raid_opts = 0;
743 char * fs_type = 0;
744 const char * feature_set = "filetype,sparse_super";
745 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000746#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000747 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000748
749 if (uname(&ut)) {
750 perror("uname");
751 exit(1);
752 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000753 if ((ut.release[0] == '1') ||
754 (ut.release[0] == '2' && ut.release[1] == '.' &&
755 ut.release[2] < '2' && ut.release[3] == '.'))
756 feature_set = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000757#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000759 if (oldpath) {
760 char *newpath;
761
762 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
763 strcpy (newpath, PATH_SET);
764 strcat (newpath, ":");
765 strcat (newpath, oldpath);
766 putenv (newpath);
767 } else
768 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769
770 setbuf(stdout, NULL);
771 setbuf(stderr, NULL);
772 initialize_ext2_error_table();
773 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000774 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000775
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000776 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000777 E2FSPROGS_VERSION, E2FSPROGS_DATE,
778 EXT2FS_VERSION, EXT2FS_DATE);
779 if (argc && *argv)
780 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000781 while ((c = getopt (argc, argv,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000782 "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 +0000783 switch (c) {
784 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000785 blocksize = strtoul(optarg, &tmp, 0);
786 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000787 com_err(program_name, 0,
788 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000789 exit(1);
790 }
791 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000792 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000793 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000794 break;
795 case 'c':
796 case 't': /* Check for bad blocks */
797 cflag = 1;
798 break;
799 case 'f':
800 size = strtoul(optarg, &tmp, 0);
801 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000802 com_err(program_name, 0,
803 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000804 optarg);
805 exit(1);
806 }
807 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000808 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000809 printf(_("Warning: fragments not supported. "
810 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000811 break;
812 case 'g':
813 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000814 if (*tmp) {
815 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000816 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000817 exit(1);
818 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000819 if ((param.s_blocks_per_group % 8) != 0) {
820 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000821 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000822 exit(1);
823 }
824 break;
825 case 'i':
826 inode_ratio = strtoul(optarg, &tmp, 0);
827 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
828 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000829 com_err(program_name, 0,
830 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000831 exit(1);
832 }
833 break;
834 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000835 bad_blocks_filename = malloc(strlen(optarg)+1);
836 if (!bad_blocks_filename) {
837 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000838 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000839 exit(1);
840 }
841 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000842 break;
843 case 'm':
844 reserved_ratio = strtoul(optarg, &tmp, 0);
845 if (reserved_ratio > 50 || *tmp) {
846 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000847 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000848 optarg);
849 exit(1);
850 }
851 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000852 case 'n':
853 noaction++;
854 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000855 case 'o':
856 creator_os = optarg;
857 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000858 case 'r':
859 param.s_rev_level = atoi(optarg);
860 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000861 case 's':
862 sparse_option = atoi(optarg);
863 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000864#ifdef EXT2_DYNAMIC_REV
865 case 'I':
866 param.s_inode_size = atoi(optarg);
867 break;
868#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000869 case 'N':
870 num_inodes = atoi(optarg);
871 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872 case 'v':
873 verbose = 1;
874 break;
875 case 'q':
876 quiet = 1;
877 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000878 case 'F':
879 force = 1;
880 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000881 case 'L':
882 volume_label = optarg;
883 break;
884 case 'M':
885 mount_dir = optarg;
886 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000887 case 'O':
888 feature_set = optarg;
889 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000890 case 'R':
891 raid_opts = optarg;
892 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000893 case 'S':
894 super_only = 1;
895 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000896 case 'T':
897 fs_type = optarg;
898 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000899 case 'V':
900 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000901 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000902 error_message(EXT2_ET_BASE));
903 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 default:
905 usage();
906 }
907 if (optind == argc)
908 usage();
909 device_name = argv[optind];
910 optind++;
911 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000912 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
913
914 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000915 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 argv[optind - 1]);
917 exit(1);
918 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000919 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920 }
921 if (optind < argc)
922 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000923
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000924 if (raid_opts)
925 parse_raid_opts(raid_opts);
926
Theodore Ts'o74becf31997-04-26 14:37:06 +0000927 if (!force)
928 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000929 check_mount();
930
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931 param.s_log_frag_size = param.s_log_block_size;
932
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000933 if (noaction && param.s_blocks_count) {
934 dev_size = param.s_blocks_count;
935 retval = 0;
936 } else
937 retval = ext2fs_get_device_size(device_name,
938 EXT2_BLOCK_SIZE(&param),
939 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000940 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
941 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000942 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000943 exit(1);
944 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000945 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000946 if (retval == EXT2_ET_UNIMPLEMENTED) {
947 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000948 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +0000949 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000950 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000951 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +0000952 } else {
953 if (dev_size == 0) {
954 com_err(program_name, 0,
955 _("Device size reported to be zero. "
956 "Invalid partition specified, or\n\t"
957 "partition table wasn't reread "
958 "after running fdisk, due to\n\t"
959 "a modified partition being busy "
960 "and in use. You may need to reboot\n\t"
961 "to re-read your partition table.\n"
962 ));
963 exit(1);
964 }
Theodore Ts'oa789d841998-03-30 01:20:55 +0000965 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +0000966 }
967
Theodore Ts'oa789d841998-03-30 01:20:55 +0000968 } else if (!force && (param.s_blocks_count > dev_size)) {
969 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000970 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000971 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000972 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000974 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
975
Theodore Ts'o521e3681997-04-29 17:48:10 +0000976 if (param.s_blocks_per_group) {
977 if (param.s_blocks_per_group < 256 ||
978 param.s_blocks_per_group > max || *tmp) {
979 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000980 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +0000981 exit(1);
982 }
983 }
984
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985 /*
986 * Calculate number of inodes based on the inode ratio
987 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000988 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +0000989 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000990 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000991
992 /*
993 * Calculate number of blocks to reserve
994 */
995 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000996
Theodore Ts'o521e3681997-04-29 17:48:10 +0000997#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o27401561999-09-14 20:11:19 +0000998 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000999 param_ext2->s_feature_ro_compat |=
1000 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1001#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +00001002 if (feature_set && !strncasecmp(feature_set, "none", 4))
1003 feature_set = 0;
1004 if (feature_set && e2p_edit_feature(feature_set,
1005 &param_ext2->s_feature_compat,
1006 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001007 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +00001008 feature_set);
1009 exit(1);
1010 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011}
1012
1013int main (int argc, char *argv[])
1014{
1015 errcode_t retval = 0;
1016 ext2_filsys fs;
1017 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001018 struct ext2fs_sb *s;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001019
1020#ifdef ENABLE_NLS
1021 setlocale(LC_MESSAGES, "");
1022 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1023 textdomain(NLS_CAT_NAME);
1024#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001025 PRS(argc, argv);
1026
Theodore Ts'o3839e651997-04-26 13:21:57 +00001027 /*
1028 * Initialize the superblock....
1029 */
1030 retval = ext2fs_initialize(device_name, 0, &param,
1031 unix_io_manager, &fs);
1032 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001033 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001034 exit(1);
1035 }
1036
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001037 /*
1038 * Generate a UUID for it...
1039 */
1040 s = (struct ext2fs_sb *) fs->super;
1041 uuid_generate(s->s_uuid);
1042
1043 /*
1044 * Override the creator OS, if applicable
1045 */
1046 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001047 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001048 exit(1);
1049 }
1050
1051 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001052 * For the Hurd, we will turn off filetype since it doesn't
1053 * support it.
1054 */
1055 if (fs->super->s_creator_os == EXT2_OS_HURD)
1056 fs->super->s_feature_incompat &=
1057 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1058
1059 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001060 * Set the volume label...
1061 */
1062 if (volume_label) {
1063 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1064 strncpy(s->s_volume_name, volume_label,
1065 sizeof(s->s_volume_name));
1066 }
1067
1068 /*
1069 * Set the last mount directory
1070 */
1071 if (mount_dir) {
1072 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1073 strncpy(s->s_last_mounted, mount_dir,
1074 sizeof(s->s_last_mounted));
1075 }
1076
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001077 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001078 show_stats(fs);
1079
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001080 if (noaction)
1081 exit(0);
1082
Theodore Ts'o3839e651997-04-26 13:21:57 +00001083 if (bad_blocks_filename)
1084 read_bb_file(fs, &bb_list, bad_blocks_filename);
1085 if (cflag)
1086 test_disk(fs, &bb_list);
1087
1088 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001089 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001090 retval = ext2fs_allocate_tables(fs);
1091 if (retval) {
1092 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001093 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001094 exit(1);
1095 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001096 if (super_only) {
1097 fs->super->s_state |= EXT2_ERROR_FS;
1098 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1099 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001100 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001101 create_root_dir(fs);
1102 create_lost_and_found(fs);
1103 reserve_inodes(fs);
1104 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001105#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001106 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001107#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001108 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001109
1110 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001111 printf(_("Writing superblocks and "
1112 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001113 retval = ext2fs_flush(fs);
1114 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001115 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001116 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001117 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001118 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001119 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001120 return 0;
1121}