blob: 66e2f3baa709735437971dcd380d5e57f09ce715 [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";
Theodore Ts'od48755e2000-12-09 14:36:04 +000073const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000074
75/* Command line options */
Theodore Ts'od48755e2000-12-09 14:36:04 +000076int cflag /* = 0 */ ;
77int verbose /* = 0 */ ;
78int quiet /* = 0 */ ;
79int super_only /* = 0 */ ;
80int force /* = 0 */ ;
81int noaction /* = 0 */ ;
82int journal_size /* = 0 */ ;
83int journal_flags /* = 0 */ ;
84char *bad_blocks_filename /* = 0 */ ;
85__u32 fs_stride /* = 0 */ ;
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
87struct ext2_super_block param;
Theodore Ts'od48755e2000-12-09 14:36:04 +000088char *creator_os /* = NULL */ ;
89char *volume_label /* = NULL */ ;
90char *mount_dir /* = NULL */ ;
91char *journal_device /* = NULL */ ;
Theodore Ts'o3839e651997-04-26 13:21:57 +000092
Theodore Ts'o8ddaa662000-11-17 04:55:24 +000093static void usage(NOARGS);
94static void check_plausibility(const char *device);
95static void check_mount(const char *device);
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000096
Theodore Ts'o3839e651997-04-26 13:21:57 +000097static void usage(NOARGS)
98{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000099 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000100 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000101 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
102 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
Theodore Ts'o18160d21999-10-23 01:22:17 +0000103 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
104 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000105 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106 program_name);
107 exit(1);
108}
109
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000110static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111{
112 int l = 0;
113
114 arg >>= 1;
115 while (arg) {
116 l++;
117 arg >>= 1;
118 }
119 return l;
120}
121
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000122static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000123{
124 int l;
125
126 for (l=0; arg ; l++)
127 arg = arg / 10;
128 return l;
129}
130
Theodore Ts'oa789d841998-03-30 01:20:55 +0000131static void proceed_question(NOARGS)
132{
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000133 char buf[256];
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000134 char *short_yes = _("yY");
135
Theodore Ts'oa789d841998-03-30 01:20:55 +0000136 fflush(stdout);
137 fflush(stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000138 printf(_("Proceed anyway? (y,n) "));
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000139 buf[0] = 0;
140 fgets(buf, sizeof(buf), stdin);
141 if (strchr(short_yes, buf[0]) == 0)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000142 exit(1);
143}
144
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000145#ifndef SCSI_BLK_MAJOR
146#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
147#endif
148
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000149static void check_plausibility(const char *device)
Theodore Ts'o74becf31997-04-26 14:37:06 +0000150{
151#ifdef HAVE_LINUX_MAJOR_H
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000152#ifndef MAJOR
153#define MAJOR(dev) ((dev)>>8)
154#define MINOR(dev) ((dev) & 0xff)
155#endif
156
Theodore Ts'o74becf31997-04-26 14:37:06 +0000157 int val;
158 struct stat s;
159
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000160 val = stat(device, &s);
Theodore Ts'o74becf31997-04-26 14:37:06 +0000161
162 if(val == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000163 fprintf(stderr, _("Could not stat %s --- %s\n"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000164 device, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000165 if (errno == ENOENT)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000166 fprintf(stderr, _("\nThe device apparently does "
167 "not exist; did you specify it correctly?\n"));
Theodore Ts'o74becf31997-04-26 14:37:06 +0000168 exit(1);
169 }
170 if(!S_ISBLK(s.st_mode)) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000171 printf(_("%s is not a block special device.\n"), device);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000172 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000173 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000174 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
175 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000176 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
177 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000178 printf(_("%s is entire device, not just one partition!\n"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000179 device);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000180 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000181 }
182#endif
183}
184
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000185static void check_mount(const char *device)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000187 errcode_t retval;
188 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000190 retval = ext2fs_check_if_mounted(device, &mount_flags);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000191 if (retval) {
192 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000193 _("while determining whether %s is mounted."),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000194 device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000196 }
197 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000199
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000200 fprintf(stderr, _("%s is mounted; "), device);
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000201 if (force) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000202 fprintf(stderr, _("mke2fs forced anyway. "
203 "Hope /etc/mtab is incorrect.\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000204 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000205 fprintf(stderr, _("will not make a filesystem here!\n"));
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000206 exit(1);
207 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208}
209
210/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000211 * This function sets the default parameters for a filesystem
212 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000213 * The type is specified by the user. The size is the maximum size
214 * (in megabytes) for which a set of parameters applies, with a size
215 * of zero meaning that it is the default parameter for the type.
216 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000217 */
218static char default_str[] = "default";
219struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000220 const char *type;
221 int size;
222 int blocksize;
223 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000224} settings[] = {
225 { default_str, 0, 4096, 8192 },
226 { default_str, 512, 1024, 4096 },
227 { default_str, 3, 1024, 8192 },
228 { "news", 0, 4096, 4096 },
229 { 0, 0, 0, 0},
230};
231
Theodore Ts'o9094f281999-10-26 16:42:50 +0000232static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000233 int blocksize, int *inode_ratio)
234{
235 int megs;
236 int ratio = 0;
237 struct mke2fs_defaults *p;
238
Theodore Ts'o9094f281999-10-26 16:42:50 +0000239 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000240 1024);
241 if (inode_ratio)
242 ratio = *inode_ratio;
243 if (!fs_type)
244 fs_type = default_str;
245 for (p = settings; p->type; p++) {
246 if ((strcmp(p->type, fs_type) != 0) &&
247 (strcmp(p->type, default_str) != 0))
248 continue;
249 if ((p->size != 0) &&
250 (megs > p->size))
251 continue;
252 if (ratio == 0)
253 *inode_ratio = p->inode_ratio;
254 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000255 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000256 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000257 }
258 }
259 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000260 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000261}
262
263/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264 * Helper function for read_bb_file and test_disk
265 */
266static void invalid_block(ext2_filsys fs, blk_t blk)
267{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000268 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 return;
270}
271
272/*
273 * Reads the bad blocks list from a file
274 */
275static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
276 const char *bad_blocks_file)
277{
278 FILE *f;
279 errcode_t retval;
280
281 f = fopen(bad_blocks_file, "r");
282 if (!f) {
283 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000284 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 exit(1);
286 }
287 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
288 fclose (f);
289 if (retval) {
290 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000291 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292 exit(1);
293 }
294}
295
296/*
297 * Runs the badblocks program to test the disk
298 */
299static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
300{
301 FILE *f;
302 errcode_t retval;
303 char buf[1024];
304
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000305 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
306 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 fs->super->s_blocks_count);
308 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000309 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 f = popen(buf, "r");
311 if (!f) {
312 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000313 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 exit(1);
315 }
316 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000317 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318 if (retval) {
319 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000320 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321 exit(1);
322 }
323}
324
325static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
326{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000327 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328 int must_be_good;
329 blk_t blk;
330 badblocks_iterate bb_iter;
331 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000332 blk_t group_block;
333 int group;
334 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335
336 if (!bb_list)
337 return;
338
339 /*
340 * The primary superblock and group descriptors *must* be
341 * good; if not, abort.
342 */
343 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
344 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
345 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000346 fprintf(stderr, _("Block %d in primary "
347 "superblock/group descriptor area bad.\n"), i);
348 fprintf(stderr, _("Blocks %d through %d must be good "
349 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000350 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000351 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 exit(1);
353 }
354 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000355
356 /*
357 * See if any of the bad blocks are showing up in the backup
358 * superblocks and/or group descriptors. If so, issue a
359 * warning and adjust the block counts appropriately.
360 */
361 group_block = fs->super->s_first_data_block +
362 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000363
364 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000365 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000366 for (j=0; j < fs->desc_blocks+1; j++) {
367 if (badblocks_list_test(bb_list, group_block +
368 j)) {
369 if (!group_bad)
370 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000371_("Warning: the backup superblock/group descriptors at block %d contain\n"
372" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000373 group_block);
374 group_bad++;
375 group = ext2fs_group_of_blk(fs, group_block+j);
376 fs->group_desc[group].bg_free_blocks_count++;
377 fs->super->s_free_blocks_count++;
378 }
379 }
380 group_block += fs->super->s_blocks_per_group;
381 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382
383 /*
384 * Mark all the bad blocks as used...
385 */
386 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
387 if (retval) {
388 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000389 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 exit(1);
391 }
392 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000393 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 badblocks_list_iterate_end(bb_iter);
395}
396
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000397static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000398{
399 errcode_t retval;
400 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000401 int i, j, num, count;
402 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000403 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000404 int sync_kludge = 0;
405 char *mke2fs_sync;
406
407 mke2fs_sync = getenv("MKE2FS_SYNC");
408 if (mke2fs_sync)
409 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410
411 buf = malloc(fs->blocksize * STRIDE_LENGTH);
412 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000413 com_err("malloc", ENOMEM,
414 _("while allocating zeroizing buffer"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 exit(1);
416 }
417 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000418
419 /*
420 * Figure out how many digits we need
421 */
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000422 i = int_log10(fs->group_desc_count);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000423 sprintf(format, "%%%dd/%%%dld", i, i);
424 memset(backup, '\b', sizeof(backup)-1);
425 backup[sizeof(backup)-1] = 0;
426 if ((2*i)+1 < sizeof(backup))
427 backup[(2*i)+1] = 0;
428
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000430 printf(_("Writing inode tables: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 for (i = 0; i < fs->group_desc_count; i++) {
432 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000433 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000435 blk = fs->group_desc[i].bg_inode_table;
436 num = fs->inode_blocks_per_group;
437
438 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
439 if (num-j > STRIDE_LENGTH)
440 count = STRIDE_LENGTH;
441 else
442 count = num - j;
443 retval = io_channel_write_blk(fs->io, blk, count, buf);
444 if (retval)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000445 printf(_("Warning: could not write %d blocks "
446 "in inode table starting at %d: %s\n"),
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000447 count, blk, error_message(retval));
448 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000450 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000451 if (sync_kludge) {
452 if (sync_kludge == 1)
453 sync();
454 else if ((i % sync_kludge) == 0)
455 sync();
456 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000458 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000460 fputs(_("done \n"), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461}
462
463static void create_root_dir(ext2_filsys fs)
464{
465 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000466 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467
468 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
469 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000470 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471 exit(1);
472 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 if (geteuid()) {
474 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
475 if (retval) {
476 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000477 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000478 exit(1);
479 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000480 inode.i_uid = getuid();
481 if (inode.i_uid)
482 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000483 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
484 if (retval) {
485 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000486 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000487 exit(1);
488 }
489 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490}
491
492static void create_lost_and_found(ext2_filsys fs)
493{
494 errcode_t retval;
495 ino_t ino;
496 const char *name = "lost+found";
497 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000498 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499
500 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
501 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000502 com_err("ext2fs_mkdir", retval,
503 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504 exit(1);
505 }
506
507 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
508 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000509 com_err("ext2_lookup", retval,
510 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 exit(1);
512 }
513
514 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000515 if ((lpf_size += fs->blocksize) >= 16*1024)
516 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 retval = ext2fs_expand_dir(fs, ino);
518 if (retval) {
519 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000520 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 exit(1);
522 }
523 }
524}
525
526static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
527{
528 errcode_t retval;
529
Theodore Ts'of3db3561997-04-26 13:34:30 +0000530 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531 fs->group_desc[0].bg_free_inodes_count--;
532 fs->super->s_free_inodes_count--;
533 retval = ext2fs_update_bb_inode(fs, bb_list);
534 if (retval) {
535 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000536 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 exit(1);
538 }
539
540}
541
542static void reserve_inodes(ext2_filsys fs)
543{
544 ino_t i;
545 int group;
546
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000547 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000548 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 group = ext2fs_group_of_ino(fs, i);
550 fs->group_desc[group].bg_free_inodes_count--;
551 fs->super->s_free_inodes_count--;
552 }
553 ext2fs_mark_ib_dirty(fs);
554}
555
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000556static void zap_sector(ext2_filsys fs, int sect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000557{
558 char buf[512];
559 int retval;
560
561 memset(buf, 0, 512);
562
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000563 io_channel_set_blksize(fs->io, 512);
564 retval = io_channel_write_blk(fs->io, sect, -512, buf);
565 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000566 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000567 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568 error_message(retval));
569}
570
571
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572static void show_stats(ext2_filsys fs)
573{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000574 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
575 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000577 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578
579 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000580 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000582
583 memset(buf, 0, sizeof(buf));
584 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000585 printf(_("Filesystem label=%s\n"), buf);
586 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000587 switch (fs->super->s_creator_os) {
588 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000589 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000590 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000591 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000592 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000593 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000594 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000595 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000596 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000597 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000598 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000599 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000600 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 s->s_r_blocks_count,
602 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000603 printf(_("First data block=%u\n"), s->s_first_data_block);
604 if (fs->group_desc_count > 1)
605 printf(_("%lu block groups\n"), fs->group_desc_count);
606 else
607 printf(_("%lu block group\n"), fs->group_desc_count);
608 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000610 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611
612 if (fs->group_desc_count == 1) {
613 printf("\n");
614 return;
615 }
616
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000617 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618 group_block = s->s_first_data_block;
619 col_left = 0;
620 for (i = 1; i < fs->group_desc_count; i++) {
621 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000622 if (!ext2fs_bg_has_super(fs, i))
623 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000624 if (i != 1)
625 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000626 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000627 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000629 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000631 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000632 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633 }
634 printf("\n\n");
635}
636
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000637#ifndef HAVE_STRCASECMP
638static int strcasecmp (char *s1, char *s2)
639{
640 while (*s1 && *s2) {
641 int ch1 = *s1++, ch2 = *s2++;
642 if (isupper (ch1))
643 ch1 = tolower (ch1);
644 if (isupper (ch2))
645 ch2 = tolower (ch2);
646 if (ch1 != ch2)
647 return ch1 - ch2;
648 }
649 return *s1 ? 1 : *s2 ? -1 : 0;
650}
651#endif
652
653/*
654 * Set the S_CREATOR_OS field. Return true if OS is known,
655 * otherwise, 0.
656 */
657static int set_os(struct ext2_super_block *sb, char *os)
658{
659 if (isdigit (*os))
660 sb->s_creator_os = atoi (os);
661 else if (strcasecmp(os, "linux") == 0)
662 sb->s_creator_os = EXT2_OS_LINUX;
663 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
664 sb->s_creator_os = EXT2_OS_HURD;
665 else if (strcasecmp(os, "masix") == 0)
666 sb->s_creator_os = EXT2_OS_MASIX;
667 else
668 return 0;
669 return 1;
670}
671
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000672#define PATH_SET "PATH=/sbin"
673
Theodore Ts'od163b091997-10-03 17:42:28 +0000674static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000675{
676 char *buf, *token, *next, *p, *arg;
677 int len;
678 int raid_usage = 0;
679
680 len = strlen(opts);
681 buf = malloc(len+1);
682 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000683 fprintf(stderr, _("Couldn't allocate memory to parse "
684 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000685 exit(1);
686 }
687 strcpy(buf, opts);
688 for (token = buf; token && *token; token = next) {
689 p = strchr(token, ',');
690 next = 0;
691 if (p) {
692 *p = 0;
693 next = p+1;
694 }
695 arg = strchr(token, '=');
696 if (arg) {
697 *arg = 0;
698 arg++;
699 }
700 if (strcmp(token, "stride") == 0) {
701 if (!arg) {
702 raid_usage++;
703 continue;
704 }
705 fs_stride = strtoul(arg, &p, 0);
706 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000707 fprintf(stderr,
708 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000709 raid_usage++;
710 continue;
711 }
712 } else
713 raid_usage++;
714 }
715 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000716 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000717 "Raid options are separated by commas, "
718 "and may take an argument which\n"
719 "\tis set off by an equals ('=') sign.\n\n"
720 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000721 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000722 exit(1);
723 }
724}
725
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000726static void parse_journal_opts(const char *opts)
727{
728 char *buf, *token, *next, *p, *arg;
729 int len;
730 int journal_usage = 0;
731
732 len = strlen(opts);
733 buf = malloc(len+1);
734 if (!buf) {
735 fprintf(stderr, _("Couldn't allocate memory to parse "
736 "journal options!\n"));
737 exit(1);
738 }
739 strcpy(buf, opts);
740 for (token = buf; token && *token; token = next) {
741 p = strchr(token, ',');
742 next = 0;
743 if (p) {
744 *p = 0;
745 next = p+1;
746 }
747 arg = strchr(token, '=');
748 if (arg) {
749 *arg = 0;
750 arg++;
751 }
752 printf("Journal option=%s, argument=%s\n", token,
753 arg ? arg : "NONE");
754 if (strcmp(token, "device") == 0) {
755 if (!arg) {
756 journal_usage++;
757 continue;
758 }
759 journal_device = arg;
760 } else if (strcmp(token, "size") == 0) {
761 if (!arg) {
762 journal_usage++;
763 continue;
764 }
765 journal_size = strtoul(arg, &p, 0);
766 journal_size_check:
767 if (*p || (journal_size < 4 || journal_size > 100)) {
768 fprintf(stderr,
769 _("Invalid journal size parameter - %s.\n"),
770 arg);
771 journal_usage++;
772 continue;
773 }
Theodore Ts'od48755e2000-12-09 14:36:04 +0000774 } else if (strcmp(token, "v1_superblock") == 0) {
775 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
776 continue;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000777 } else {
778 journal_size = strtoul(token, &p, 0);
779 if (*p)
780 journal_usage++;
781 else
782 goto journal_size_check;
783 }
784 }
785 if (journal_usage) {
786 fprintf(stderr, _("\nBad journal options specified.\n\n"
787 "Journal options are separated by commas, "
788 "and may take an argument which\n"
789 "\tis set off by an equals ('=') sign.\n\n"
790 "Valid raid options are:\n"
791 "\tsize=<journal size in megabytes>\n"
792 "\tdevice=<journal device>\n\n"
793 "Journal size must be between "
794 "4 and 100 megabytes.\n\n" ));
795 exit(1);
796 }
797}
798
Theodore Ts'o896938d1999-10-23 01:04:50 +0000799static __u32 ok_features[3] = {
800 0, /* Compat */
801 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
802 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
803};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000804
805
Theodore Ts'o3839e651997-04-26 13:21:57 +0000806static void PRS(int argc, char *argv[])
807{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000808 int c;
809 int size;
810 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000811 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000812 int blocksize = 0;
813 int inode_ratio = 0;
814 int reserved_ratio = 5;
815 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000816 errcode_t retval;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000817 int sparse_option = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000818 char * oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000819 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000820 char * raid_opts = 0;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000821 char * journal_opts = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000822 char * fs_type = 0;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000823 const char * feature_set = "filetype";
Theodore Ts'o4a600561999-10-26 14:35:51 +0000824 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000825#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000826 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000827
828 if (uname(&ut)) {
829 perror("uname");
830 exit(1);
831 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000832 if ((ut.release[0] == '1') ||
833 (ut.release[0] == '2' && ut.release[1] == '.' &&
834 ut.release[2] < '2' && ut.release[3] == '.'))
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000835 feature_set = NULL;
Theodore Ts'o27401561999-09-14 20:11:19 +0000836#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000837 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000838 if (oldpath) {
839 char *newpath;
840
841 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
842 strcpy (newpath, PATH_SET);
843 strcat (newpath, ":");
844 strcat (newpath, oldpath);
845 putenv (newpath);
846 } else
847 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000848
849 setbuf(stdout, NULL);
850 setbuf(stderr, NULL);
851 initialize_ext2_error_table();
852 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000853 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000854
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000855 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000856 E2FSPROGS_VERSION, E2FSPROGS_DATE,
857 EXT2FS_VERSION, EXT2FS_DATE);
858 if (argc && *argv)
859 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000860 while ((c = getopt (argc, argv,
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000861 "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862 switch (c) {
863 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000864 blocksize = strtoul(optarg, &tmp, 0);
865 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000866 com_err(program_name, 0,
867 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868 exit(1);
869 }
870 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000871 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000872 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000873 break;
874 case 'c':
875 case 't': /* Check for bad blocks */
876 cflag = 1;
877 break;
878 case 'f':
879 size = strtoul(optarg, &tmp, 0);
880 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000881 com_err(program_name, 0,
882 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000883 optarg);
884 exit(1);
885 }
886 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000887 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000888 printf(_("Warning: fragments not supported. "
889 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000890 break;
891 case 'g':
892 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000893 if (*tmp) {
894 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000895 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000896 exit(1);
897 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000898 if ((param.s_blocks_per_group % 8) != 0) {
899 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000900 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000901 exit(1);
902 }
903 break;
904 case 'i':
905 inode_ratio = strtoul(optarg, &tmp, 0);
906 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
907 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000908 com_err(program_name, 0,
909 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910 exit(1);
911 }
912 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000913 case 'j':
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000914 journal_opts = optarg;
915 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000917 bad_blocks_filename = malloc(strlen(optarg)+1);
918 if (!bad_blocks_filename) {
919 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000920 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000921 exit(1);
922 }
923 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924 break;
925 case 'm':
926 reserved_ratio = strtoul(optarg, &tmp, 0);
927 if (reserved_ratio > 50 || *tmp) {
928 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000929 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 optarg);
931 exit(1);
932 }
933 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000934 case 'n':
935 noaction++;
936 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000937 case 'o':
938 creator_os = optarg;
939 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000940 case 'r':
941 param.s_rev_level = atoi(optarg);
942 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000943 case 's':
944 sparse_option = atoi(optarg);
945 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000946#ifdef EXT2_DYNAMIC_REV
947 case 'I':
948 param.s_inode_size = atoi(optarg);
949 break;
950#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000951 case 'N':
952 num_inodes = atoi(optarg);
953 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 case 'v':
955 verbose = 1;
956 break;
957 case 'q':
958 quiet = 1;
959 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000960 case 'F':
961 force = 1;
962 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000963 case 'L':
964 volume_label = optarg;
965 break;
966 case 'M':
967 mount_dir = optarg;
968 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000969 case 'O':
970 feature_set = optarg;
971 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000972 case 'R':
973 raid_opts = optarg;
974 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000975 case 'S':
976 super_only = 1;
977 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000978 case 'T':
979 fs_type = optarg;
980 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000981 case 'V':
982 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000983 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000984 error_message(EXT2_ET_BASE));
985 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000986 default:
987 usage();
988 }
989 if (optind == argc)
990 usage();
991 device_name = argv[optind];
992 optind++;
993 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000994 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
995
996 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000997 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000998 argv[optind - 1]);
999 exit(1);
1000 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +00001001 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001002 }
1003 if (optind < argc)
1004 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001005
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001006 if (raid_opts)
1007 parse_raid_opts(raid_opts);
1008
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001009 if (journal_opts)
1010 parse_journal_opts(journal_opts);
1011
Theodore Ts'o74becf31997-04-26 14:37:06 +00001012 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001013 check_plausibility(device_name);
1014 check_mount(device_name);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001015
Theodore Ts'o3839e651997-04-26 13:21:57 +00001016 param.s_log_frag_size = param.s_log_block_size;
1017
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001018 if (noaction && param.s_blocks_count) {
1019 dev_size = param.s_blocks_count;
1020 retval = 0;
1021 } else
1022 retval = ext2fs_get_device_size(device_name,
1023 EXT2_BLOCK_SIZE(&param),
1024 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +00001025 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1026 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001027 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001028 exit(1);
1029 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001030 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001031 if (retval == EXT2_ET_UNIMPLEMENTED) {
1032 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001033 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001034 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001035 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001036 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001037 } else {
1038 if (dev_size == 0) {
1039 com_err(program_name, 0,
1040 _("Device size reported to be zero. "
1041 "Invalid partition specified, or\n\t"
1042 "partition table wasn't reread "
1043 "after running fdisk, due to\n\t"
1044 "a modified partition being busy "
1045 "and in use. You may need to reboot\n\t"
1046 "to re-read your partition table.\n"
1047 ));
1048 exit(1);
1049 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001050 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001051 }
1052
Theodore Ts'oa789d841998-03-30 01:20:55 +00001053 } else if (!force && (param.s_blocks_count > dev_size)) {
1054 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001055 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001056 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001057 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001058
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001059 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
1060
Theodore Ts'o521e3681997-04-29 17:48:10 +00001061 if (param.s_blocks_per_group) {
1062 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001063 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001064 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001065 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001066 exit(1);
1067 }
1068 }
1069
Theodore Ts'o3839e651997-04-26 13:21:57 +00001070 /*
1071 * Calculate number of inodes based on the inode ratio
1072 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001073 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001074 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001075 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001076
1077 /*
1078 * Calculate number of blocks to reserve
1079 */
1080 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001081
Theodore Ts'of6f65832000-10-25 03:01:37 +00001082 /* Turn off features not supported by the earlier filesystem version */
1083 if (param.s_rev_level == 0) {
1084 sparse_option = 0;
1085 feature_set = NULL;
1086 }
Theodore Ts'o27401561999-09-14 20:11:19 +00001087 if (sparse_option)
Theodore Ts'o521e3681997-04-29 17:48:10 +00001088 param_ext2->s_feature_ro_compat |=
1089 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'of6f65832000-10-25 03:01:37 +00001090
Theodore Ts'o896938d1999-10-23 01:04:50 +00001091 if (feature_set && !strncasecmp(feature_set, "none", 4))
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001092 feature_set = NULL;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001093 if (feature_set && e2p_edit_feature(feature_set,
1094 &param_ext2->s_feature_compat,
1095 ok_features)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001096 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
Theodore Ts'o896938d1999-10-23 01:04:50 +00001097 feature_set);
1098 exit(1);
1099 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100}
1101
1102int main (int argc, char *argv[])
1103{
1104 errcode_t retval = 0;
1105 ext2_filsys fs;
1106 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001107 int journal_blocks;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001108 struct ext2fs_sb *s;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001109
1110#ifdef ENABLE_NLS
1111 setlocale(LC_MESSAGES, "");
1112 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1113 textdomain(NLS_CAT_NAME);
1114#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001115 PRS(argc, argv);
1116
Theodore Ts'o3839e651997-04-26 13:21:57 +00001117 /*
1118 * Initialize the superblock....
1119 */
1120 retval = ext2fs_initialize(device_name, 0, &param,
1121 unix_io_manager, &fs);
1122 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001123 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001124 exit(1);
1125 }
1126
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001127 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001128 * Wipe out the old on-disk superblock
1129 */
1130 zap_sector(fs, 2);
1131
1132 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001133 * Generate a UUID for it...
1134 */
1135 s = (struct ext2fs_sb *) fs->super;
1136 uuid_generate(s->s_uuid);
1137
1138 /*
1139 * Override the creator OS, if applicable
1140 */
1141 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001142 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001143 exit(1);
1144 }
1145
1146 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001147 * For the Hurd, we will turn off filetype since it doesn't
1148 * support it.
1149 */
1150 if (fs->super->s_creator_os == EXT2_OS_HURD)
1151 fs->super->s_feature_incompat &=
1152 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1153
1154 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001155 * Set the volume label...
1156 */
1157 if (volume_label) {
1158 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1159 strncpy(s->s_volume_name, volume_label,
1160 sizeof(s->s_volume_name));
1161 }
1162
1163 /*
1164 * Set the last mount directory
1165 */
1166 if (mount_dir) {
1167 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1168 strncpy(s->s_last_mounted, mount_dir,
1169 sizeof(s->s_last_mounted));
1170 }
1171
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001172 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001173 show_stats(fs);
1174
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001175 if (noaction)
1176 exit(0);
1177
Theodore Ts'o3839e651997-04-26 13:21:57 +00001178 if (bad_blocks_filename)
1179 read_bb_file(fs, &bb_list, bad_blocks_filename);
1180 if (cflag)
1181 test_disk(fs, &bb_list);
1182
1183 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001184 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001185 retval = ext2fs_allocate_tables(fs);
1186 if (retval) {
1187 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001188 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001189 exit(1);
1190 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001191 if (super_only) {
1192 fs->super->s_state |= EXT2_ERROR_FS;
1193 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1194 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001195 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001196 create_root_dir(fs);
1197 create_lost_and_found(fs);
1198 reserve_inodes(fs);
1199 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001200#ifdef ZAP_BOOTBLOCK
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001201 zap_sector(fs, 0);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001202#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001203 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001204
Theodore Ts'od48755e2000-12-09 14:36:04 +00001205 journal_blocks = journal_size * 1024 / (fs->blocksize / 1024);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001206 if (journal_device) {
1207 if (!force)
1208 check_plausibility(journal_device);
1209 check_mount(journal_device);
1210
1211 if (!quiet)
1212 printf(_("Creating journal on device %s: "),
1213 journal_device);
Theodore Ts'od48755e2000-12-09 14:36:04 +00001214 retval = ext2fs_add_journal_device(fs, journal_device,
1215 journal_blocks,
1216 journal_flags);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001217 if(retval) {
1218 com_err (program_name, retval,
1219 _("while trying to create journal on device %s"),
1220 journal_device);
1221 exit(1);
1222 }
1223 if (!quiet)
1224 printf(_("done\n"));
1225 } else if (journal_size) {
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001226 if (!quiet)
1227 printf(_("Creating journal: "));
Theodore Ts'od48755e2000-12-09 14:36:04 +00001228 retval = ext2fs_add_journal_fs(fs, journal_blocks,
1229 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001230 if (retval) {
1231 com_err (program_name, retval,
1232 _("while trying to create journal"));
1233 exit(1);
1234 }
1235 if (!quiet)
1236 printf(_("done\n"));
1237 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001238
1239 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001240 printf(_("Writing superblocks and "
1241 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001242 retval = ext2fs_flush(fs);
1243 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001244 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001245 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001246 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001247 printf(_("done\n"));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001248 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001249 return 0;
1250}