blob: 2686d72930f2fa524c03e6b038566626cf50cc31 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05004 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 */
12
13/* Usage: mke2fs [options] device
14 *
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
17 */
18
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000019#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <time.h>
Theodore Ts'o756df352002-03-07 20:52:12 -050024#ifdef __linux__
Theodore Ts'o27401561999-09-14 20:11:19 +000025#include <sys/utsname.h>
26#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000027#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000029#else
30extern char *optarg;
31extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#endif
33#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000035#endif
36#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#endif
39#ifdef HAVE_ERRNO_H
40#include <errno.h>
41#endif
42#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000044#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000045#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <sys/types.h>
47
Theodore Ts'o54c637d2001-05-14 11:45:38 +000048#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000049#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000050#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000051#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000053#include "util.h"
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050054#include "profile.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000056#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
58#define STRIDE_LENGTH 8
59
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000060#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000061#define ZAP_BOOTBLOCK
62#endif
63
Theodore Ts'o3839e651997-04-26 13:21:57 +000064extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000065extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
67const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000068const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
70/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000071int cflag;
72int verbose;
73int quiet;
74int super_only;
75int force;
76int noaction;
77int journal_size;
78int journal_flags;
79char *bad_blocks_filename;
80__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050082struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000083char *creator_os;
84char *volume_label;
85char *mount_dir;
86char *journal_device;
87int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000088
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050089profile_t profile;
90
Theodore Ts'o31e29a12002-05-17 10:53:07 -040091int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040092int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040093
Theodore Ts'o63985322001-01-03 17:02:13 +000094static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000095{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -040097 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
98 "[-j] [-J journal-options]\n"
99 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
100 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
101 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
102 "[-r fs-revision] [-R options] [-qvSV]\n\tdevice [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 program_name);
104 exit(1);
105}
106
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000107static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108{
109 int l = 0;
110
111 arg >>= 1;
112 while (arg) {
113 l++;
114 arg >>= 1;
115 }
116 return l;
117}
118
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000119static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000120{
121 int l;
122
123 for (l=0; arg ; l++)
124 arg = arg / 10;
125 return l;
126}
127
Theodore Ts'od99225e2004-09-25 07:40:12 -0400128static int parse_version_number(const char *s)
129{
130 int major, minor, rev;
131 char *endptr;
132 const char *cp = s;
133
134 if (!s)
135 return 0;
136 major = strtol(cp, &endptr, 10);
137 if (cp == endptr || *endptr != '.')
138 return 0;
139 cp = endptr + 1;
140 minor = strtol(cp, &endptr, 10);
141 if (cp == endptr || *endptr != '.')
142 return 0;
143 cp = endptr + 1;
144 rev = strtol(cp, &endptr, 10);
145 if (cp == endptr)
146 return 0;
147 return ((((major * 256) + minor) * 256) + rev);
148}
149
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000150/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 * Helper function for read_bb_file and test_disk
152 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500153static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154{
Theodore Ts'o66938372002-03-08 00:14:46 -0500155 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 return;
157}
158
159/*
160 * Reads the bad blocks list from a file
161 */
162static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
163 const char *bad_blocks_file)
164{
165 FILE *f;
166 errcode_t retval;
167
168 f = fopen(bad_blocks_file, "r");
169 if (!f) {
170 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000171 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 exit(1);
173 }
174 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
175 fclose (f);
176 if (retval) {
177 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000178 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 exit(1);
180 }
181}
182
183/*
184 * Runs the badblocks program to test the disk
185 */
186static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
187{
188 FILE *f;
189 errcode_t retval;
190 char buf[1024];
191
Theodore Ts'of63978a2006-05-13 09:25:47 -0400192 sprintf(buf, "badblocks -b %d -X %s%s%s %d", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500193 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
194 fs->device_name, fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000196 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 f = popen(buf, "r");
198 if (!f) {
199 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400200 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 exit(1);
202 }
203 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000204 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 if (retval) {
206 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000207 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 exit(1);
209 }
210}
211
212static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
213{
Theodore Ts'o54434922003-12-07 01:28:50 -0500214 dgrp_t i;
215 blk_t j;
216 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 blk_t blk;
218 badblocks_iterate bb_iter;
219 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000220 blk_t group_block;
221 int group;
222 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223
224 if (!bb_list)
225 return;
226
227 /*
228 * The primary superblock and group descriptors *must* be
229 * good; if not, abort.
230 */
231 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
232 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000233 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000234 fprintf(stderr, _("Block %d in primary "
235 "superblock/group descriptor area bad.\n"), i);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500236 fprintf(stderr, _("Blocks %u through %d must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000237 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500239 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 exit(1);
241 }
242 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000243
244 /*
245 * See if any of the bad blocks are showing up in the backup
246 * superblocks and/or group descriptors. If so, issue a
247 * warning and adjust the block counts appropriately.
248 */
249 group_block = fs->super->s_first_data_block +
250 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000251
252 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000253 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000254 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000255 if (ext2fs_badblocks_list_test(bb_list,
256 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257 if (!group_bad)
258 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500259_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000260" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000261 group_block);
262 group_bad++;
263 group = ext2fs_group_of_blk(fs, group_block+j);
264 fs->group_desc[group].bg_free_blocks_count++;
265 fs->super->s_free_blocks_count++;
266 }
267 }
268 group_block += fs->super->s_blocks_per_group;
269 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270
271 /*
272 * Mark all the bad blocks as used...
273 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000274 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000276 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000277 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 exit(1);
279 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000280 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000282 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283}
284
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000285/*
286 * These functions implement a generalized progress meter.
287 */
288struct progress_struct {
289 char format[20];
290 char backup[80];
291 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400292 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000293};
294
295static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500296 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000298 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000299
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000300 memset(progress, 0, sizeof(struct progress_struct));
301 if (quiet)
302 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000303
304 /*
305 * Figure out how many digits we need
306 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000307 i = int_log10(max);
308 sprintf(progress->format, "%%%dd/%%%dld", i, i);
309 memset(progress->backup, '\b', sizeof(progress->backup)-1);
310 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500311 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000312 progress->backup[(2*i)+1] = 0;
313 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000314
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400315 progress->skip_progress = 0;
316 if (getenv("MKE2FS_SKIP_PROGRESS"))
317 progress->skip_progress++;
318
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000319 fputs(label, stdout);
320 fflush(stdout);
321}
322
323static void progress_update(struct progress_struct *progress, __u32 val)
324{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400325 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000326 return;
327 printf(progress->format, val, progress->max);
328 fputs(progress->backup, stdout);
329}
330
331static void progress_close(struct progress_struct *progress)
332{
333 if (progress->format[0] == 0)
334 return;
335 fputs(_("done \n"), stdout);
336}
337
338
339/*
340 * Helper function which zeros out _num_ blocks starting at _blk_. In
341 * case of an error, the details of the error is returned via _ret_blk_
342 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
343 * success, and an error code on an error.
344 *
345 * As a special case, if the first argument is NULL, then it will
346 * attempt to free the static zeroizing buffer. (This is to keep
347 * programs that check for memory leaks happy.)
348 */
349static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
350 struct progress_struct *progress,
351 blk_t *ret_blk, int *ret_count)
352{
353 int j, count, next_update, next_update_incr;
354 static char *buf;
355 errcode_t retval;
356
357 /* If fs is null, clean up the static buffer and return */
358 if (!fs) {
359 if (buf) {
360 free(buf);
361 buf = 0;
362 }
363 return 0;
364 }
365 /* Allocate the zeroizing buffer if necessary */
366 if (!buf) {
367 buf = malloc(fs->blocksize * STRIDE_LENGTH);
368 if (!buf) {
369 com_err("malloc", ENOMEM,
370 _("while allocating zeroizing buffer"));
371 exit(1);
372 }
373 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
374 }
375 /* OK, do the write loop */
376 next_update = 0;
377 next_update_incr = num / 100;
378 if (next_update_incr < 1)
379 next_update_incr = 1;
380 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400381 count = num - j;
382 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000383 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000384 retval = io_channel_write_blk(fs->io, blk, count, buf);
385 if (retval) {
386 if (ret_count)
387 *ret_count = count;
388 if (ret_blk)
389 *ret_blk = blk;
390 return retval;
391 }
392 if (progress && j > next_update) {
393 next_update += num / 100;
394 progress_update(progress, blk);
395 }
396 }
397 return 0;
398}
399
400static void write_inode_tables(ext2_filsys fs)
401{
402 errcode_t retval;
403 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500404 dgrp_t i;
405 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000406 struct progress_struct progress;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400407 int lazy_flag = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000408
409 if (quiet)
410 memset(&progress, 0, sizeof(progress));
411 else
412 progress_init(&progress, _("Writing inode tables: "),
413 fs->group_desc_count);
414
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400415 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
416 EXT2_FEATURE_COMPAT_LAZY_BG))
417 lazy_flag = 1;
418
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000420 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000422 blk = fs->group_desc[i].bg_inode_table;
423 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000424
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400425 if (!(lazy_flag &&
426 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
427 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
428 if (retval) {
429 fprintf(stderr, _("\nCould not write %d "
430 "blocks in inode table starting at %u: %s\n"),
431 num, blk, error_message(retval));
432 exit(1);
433 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000434 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000435 if (sync_kludge) {
436 if (sync_kludge == 1)
437 sync();
438 else if ((i % sync_kludge) == 0)
439 sync();
440 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000442 zero_blocks(0, 0, 0, 0, 0, 0);
443 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444}
445
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400446static void setup_lazy_bg(ext2_filsys fs)
447{
448 dgrp_t i;
449 int blks;
450 struct ext2_super_block *sb = fs->super;
451 struct ext2_group_desc *bg = fs->group_desc;
452
453 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
454 EXT2_FEATURE_COMPAT_LAZY_BG)) {
455 for (i = 0; i < fs->group_desc_count; i++, bg++) {
456 if ((i == 0) ||
457 (i == fs->group_desc_count-1))
458 continue;
459 if (bg->bg_free_inodes_count ==
460 sb->s_inodes_per_group) {
461 bg->bg_free_inodes_count = 0;
462 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
463 sb->s_free_inodes_count -=
464 sb->s_inodes_per_group;
465 }
466 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
467 if (bg->bg_free_blocks_count == blks) {
468 bg->bg_free_blocks_count = 0;
469 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
470 sb->s_free_blocks_count -= blks;
471 }
472 }
473 }
474}
475
476
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477static void create_root_dir(ext2_filsys fs)
478{
479 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481
482 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
483 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000484 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 exit(1);
486 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000487 if (geteuid()) {
488 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
489 if (retval) {
490 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000491 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000492 exit(1);
493 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000494 inode.i_uid = getuid();
495 if (inode.i_uid)
496 inode.i_gid = getgid();
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500497 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000498 if (retval) {
499 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000500 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501 exit(1);
502 }
503 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504}
505
506static void create_lost_and_found(ext2_filsys fs)
507{
508 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000509 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 const char *name = "lost+found";
511 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000512 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513
Theodore Ts'o6a525062001-12-24 09:40:00 -0500514 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
516 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000517 com_err("ext2fs_mkdir", retval,
518 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 exit(1);
520 }
521
522 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
523 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000524 com_err("ext2_lookup", retval,
525 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 exit(1);
527 }
528
529 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000530 if ((lpf_size += fs->blocksize) >= 16*1024)
531 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 retval = ext2fs_expand_dir(fs, ino);
533 if (retval) {
534 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000535 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 exit(1);
537 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500538 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539}
540
541static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
542{
543 errcode_t retval;
544
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 fs->group_desc[0].bg_free_inodes_count--;
547 fs->super->s_free_inodes_count--;
548 retval = ext2fs_update_bb_inode(fs, bb_list);
549 if (retval) {
550 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000551 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 exit(1);
553 }
554
555}
556
557static void reserve_inodes(ext2_filsys fs)
558{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000559 ext2_ino_t i;
560 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000562 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 group = ext2fs_group_of_ino(fs, i);
565 fs->group_desc[group].bg_free_inodes_count--;
566 fs->super->s_free_inodes_count--;
567 }
568 ext2fs_mark_ib_dirty(fs);
569}
570
Theodore Ts'o756df352002-03-07 20:52:12 -0500571#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500572#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500573#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500574
Theodore Ts'o04a96852001-08-30 21:55:26 -0400575static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000576{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400577 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500579 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000580
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400581 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600582 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500583 printf(_("Out of memory erasing sectors %d-%d\n"),
584 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600585 exit(1);
586 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500587
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500588 if (sect == 0) {
589 /* Check for a BSD disklabel, and don't erase it if so */
590 retval = io_channel_read_blk(fs->io, 0, -512, buf);
591 if (retval)
592 fprintf(stderr,
593 _("Warning: could not read block 0: %s\n"),
594 error_message(retval));
595 else {
596 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
597 if ((*magic == BSD_DISKMAGIC) ||
598 (*magic == BSD_MAGICDISK))
599 return;
600 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500601 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500602
Theodore Ts'o70988102002-07-14 08:00:00 -0400603 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000604 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400605 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000606 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400607 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500609 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
610 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000611}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000612
613static void create_journal_dev(ext2_filsys fs)
614{
615 struct progress_struct progress;
616 errcode_t retval;
617 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000618 blk_t blk;
619 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000620
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000621 retval = ext2fs_create_journal_superblock(fs,
622 fs->super->s_blocks_count, 0, &buf);
623 if (retval) {
624 com_err("create_journal_dev", retval,
625 _("while initializing journal superblock"));
626 exit(1);
627 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000628 if (quiet)
629 memset(&progress, 0, sizeof(progress));
630 else
631 progress_init(&progress, _("Zeroing journal device: "),
632 fs->super->s_blocks_count);
633
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000634 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
635 &progress, &blk, &count);
636 if (retval) {
637 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000638 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000639 blk, count);
640 exit(1);
641 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000642 zero_blocks(0, 0, 0, 0, 0, 0);
643
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000644 retval = io_channel_write_blk(fs->io,
645 fs->super->s_first_data_block+1,
646 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000647 if (retval) {
648 com_err("create_journal_dev", retval,
649 _("while writing journal superblock"));
650 exit(1);
651 }
652 progress_close(&progress);
653}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000654
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655static void show_stats(ext2_filsys fs)
656{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000657 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000658 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500659 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500661 dgrp_t i;
662 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500664 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500665 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500666 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000667
668 memset(buf, 0, sizeof(buf));
669 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000670 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500671 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500672 os = e2p_os2string(fs->super->s_creator_os);
673 fputs(os, stdout);
674 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000675 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000676 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000677 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000678 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000679 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000680 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000681 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000682 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683 s->s_r_blocks_count,
684 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000685 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500686 if (s->s_reserved_gdt_blocks)
687 printf(_("Maximum filesystem blocks=%lu\n"),
688 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
689 (fs->blocksize / sizeof(struct ext2_group_desc)) *
690 s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000691 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000692 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000693 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000694 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000695 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000697 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698
699 if (fs->group_desc_count == 1) {
700 printf("\n");
701 return;
702 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500703
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000704 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705 group_block = s->s_first_data_block;
706 col_left = 0;
707 for (i = 1; i < fs->group_desc_count; i++) {
708 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000709 if (!ext2fs_bg_has_super(fs, i))
710 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000711 if (i != 1)
712 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000713 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000714 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000716 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000718 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000719 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000720 }
721 printf("\n\n");
722}
723
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000724/*
725 * Set the S_CREATOR_OS field. Return true if OS is known,
726 * otherwise, 0.
727 */
728static int set_os(struct ext2_super_block *sb, char *os)
729{
730 if (isdigit (*os))
731 sb->s_creator_os = atoi (os);
732 else if (strcasecmp(os, "linux") == 0)
733 sb->s_creator_os = EXT2_OS_LINUX;
734 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
735 sb->s_creator_os = EXT2_OS_HURD;
736 else if (strcasecmp(os, "masix") == 0)
737 sb->s_creator_os = EXT2_OS_MASIX;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500738 else if (strcasecmp(os, "freebsd") == 0)
739 sb->s_creator_os = EXT2_OS_FREEBSD;
740 else if (strcasecmp(os, "lites") == 0)
741 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000742 else
743 return 0;
744 return 1;
745}
746
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000747#define PATH_SET "PATH=/sbin"
748
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500749static void parse_extended_opts(struct ext2_super_block *param,
750 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000751{
752 char *buf, *token, *next, *p, *arg;
753 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500754 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000755
756 len = strlen(opts);
757 buf = malloc(len+1);
758 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500759 fprintf(stderr,
760 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000761 exit(1);
762 }
763 strcpy(buf, opts);
764 for (token = buf; token && *token; token = next) {
765 p = strchr(token, ',');
766 next = 0;
767 if (p) {
768 *p = 0;
769 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500770 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000771 arg = strchr(token, '=');
772 if (arg) {
773 *arg = 0;
774 arg++;
775 }
776 if (strcmp(token, "stride") == 0) {
777 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500778 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000779 continue;
780 }
781 fs_stride = strtoul(arg, &p, 0);
782 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000783 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400784 _("Invalid stride parameter: %s\n"),
785 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500786 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000787 continue;
788 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500789 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500790 unsigned long resize, bpg, rsv_groups;
791 unsigned long group_desc_count, desc_blocks;
792 unsigned int gdpb, blocksize;
793 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500794
795 if (!arg) {
796 r_usage++;
797 continue;
798 }
799
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500800 resize = parse_num_blocks(arg,
801 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500802
803 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500804 fprintf(stderr,
805 _("Invalid resize parameter: %s\n"),
806 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500807 r_usage++;
808 continue;
809 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500810 if (resize <= param->s_blocks_count) {
811 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400812 _("The resize maximum must be greater "
813 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500814 r_usage++;
815 continue;
816 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500817
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500818 blocksize = EXT2_BLOCK_SIZE(param);
819 bpg = param->s_blocks_per_group;
820 if (!bpg)
821 bpg = blocksize * 8;
822 gdpb = blocksize / sizeof(struct ext2_group_desc);
823 group_desc_count = (param->s_blocks_count +
824 bpg - 1) / bpg;
825 desc_blocks = (group_desc_count +
826 gdpb - 1) / gdpb;
827 rsv_groups = (resize + bpg - 1) / bpg;
828 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
829 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500830 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500831 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
832
833 if (rsv_gdb > 0) {
834 param->s_feature_compat |=
835 EXT2_FEATURE_COMPAT_RESIZE_INODE;
836
837 param->s_reserved_gdt_blocks = rsv_gdb;
838 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000839 } else
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500840 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000841 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500842 if (r_usage) {
843 fprintf(stderr, _("\nBad options specified.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400844 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000845 "and may take an argument which\n"
846 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400847 "Valid extended options are:\n"
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500848 "\tstride=<stride length in blocks>\n"
849 "\tresize=<resize maximum size in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000850 exit(1);
851 }
852}
853
Theodore Ts'o896938d1999-10-23 01:04:50 +0000854static __u32 ok_features[3] = {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400855 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500856 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400857 EXT2_FEATURE_COMPAT_DIR_INDEX |
858 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000859 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400860 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
861 EXT2_FEATURE_INCOMPAT_META_BG,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000862 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
863};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000864
865
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500866static void syntax_err_report(const char *filename, long err, int line_num)
867{
868 fprintf(stderr,
869 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
870 filename, line_num, error_message(err));
871 exit(1);
872}
873
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200874static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500875
876static void edit_feature(const char *str, __u32 *compat_array)
877{
878 if (!str)
879 return;
880
881 if (e2p_edit_feature(str, compat_array, ok_features)) {
882 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
883 str);
884 exit(1);
885 }
886}
887
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888static void PRS(int argc, char *argv[])
889{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400890 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000891 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500892 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000893 int blocksize = 0;
894 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -0600895 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -0500896 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -0400897 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -0500898 int show_version_only = 0;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000899 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000900 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000901 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500902 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500903 const char * fs_type = 0;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000904 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -0500905#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +0000906 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000907#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400908 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500909 int s_opt = -1, r_opt = -1;
910 char *fs_features = 0;
911 int use_bsize;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000912
Theodore Ts'o3839e651997-04-26 13:21:57 +0000913 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000914 if (oldpath) {
915 char *newpath;
916
917 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
918 strcpy (newpath, PATH_SET);
919 strcat (newpath, ":");
920 strcat (newpath, oldpath);
921 putenv (newpath);
922 } else
923 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000925 tmp = getenv("MKE2FS_SYNC");
926 if (tmp)
927 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400928
929 /* Determine the system page size if possible */
930#ifdef HAVE_SYSCONF
931#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
932#define _SC_PAGESIZE _SC_PAGE_SIZE
933#endif
934#ifdef _SC_PAGESIZE
935 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -0600936 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400937 sys_page_size = sysval;
938#endif /* _SC_PAGESIZE */
939#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500940
941 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
942 config_fn[0] = tmp;
943 profile_set_syntax_err_cb(syntax_err_report);
944 profile_init(config_fn, &profile);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000945
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946 setbuf(stdout, NULL);
947 setbuf(stderr, NULL);
948 initialize_ext2_error_table();
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500949 memset(&fs_param, 0, sizeof(struct ext2_super_block));
950 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400951
Theodore Ts'o756df352002-03-07 20:52:12 -0500952#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000953 if (uname(&ut)) {
954 perror("uname");
955 exit(1);
956 }
Theodore Ts'od99225e2004-09-25 07:40:12 -0400957 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500958 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500959 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000960#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -0700961
962 if (argc && *argv) {
963 program_name = get_progname(*argv);
964
965 /* If called as mkfs.ext3, create a journal inode */
966 if (!strcmp(program_name, "mkfs.ext3"))
967 journal_size = -1;
968 }
969
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000970 while ((c = getopt (argc, argv,
Andreas Dilger25247852005-07-06 12:58:15 -0500971 "b:cf:g:i:jl:m:no:qr:s:tvE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000972 switch (c) {
973 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400974 blocksize = strtol(optarg, &tmp, 0);
975 b = (blocksize > 0) ? blocksize : -blocksize;
976 if (b < EXT2_MIN_BLOCK_SIZE ||
977 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000978 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400979 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000980 exit(1);
981 }
Andreas Dilger932a4892002-05-16 03:20:07 -0600982 if (blocksize > 4096)
983 fprintf(stderr, _("Warning: blocksize %d not "
984 "usable on most systems.\n"),
985 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400986 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500987 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -0400988 int_log2(blocksize >>
989 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000990 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +0000991 case 'c': /* Check for bad blocks */
992 case 't': /* deprecated */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500993 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000994 break;
995 case 'f':
996 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -0600997 if (size < EXT2_MIN_BLOCK_SIZE ||
998 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000999 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001000 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001001 optarg);
1002 exit(1);
1003 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001004 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001005 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001006 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001007 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001008 break;
1009 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001010 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001011 if (*tmp) {
1012 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001013 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001014 exit(1);
1015 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001016 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001017 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001018 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 exit(1);
1020 }
1021 break;
1022 case 'i':
1023 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001024 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1025 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001026 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001027 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001028 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001029 optarg, EXT2_MIN_BLOCK_SIZE,
1030 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001031 exit(1);
1032 }
1033 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001034 case 'J':
1035 parse_journal_opts(optarg);
1036 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001037 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001038 if (!journal_size)
1039 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001040 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001042 bad_blocks_filename = malloc(strlen(optarg)+1);
1043 if (!bad_blocks_filename) {
1044 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001045 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001046 exit(1);
1047 }
1048 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001049 break;
1050 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001051 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 if (reserved_ratio > 50 || *tmp) {
1053 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001054 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001055 optarg);
1056 exit(1);
1057 }
1058 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001059 case 'n':
1060 noaction++;
1061 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001062 case 'o':
1063 creator_os = optarg;
1064 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001065 case 'q':
1066 quiet = 1;
1067 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001068 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001069 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001070 if (*tmp) {
1071 com_err(program_name, 0,
1072 _("bad revision level - %s"), optarg);
1073 exit(1);
1074 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001075 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001076 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001077 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001078 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001079 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001080 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001081 inode_size = strtoul(optarg, &tmp, 0);
1082 if (*tmp) {
1083 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001084 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001085 exit(1);
1086 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001087 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001088 case 'v':
1089 verbose = 1;
1090 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001091 case 'F':
1092 force = 1;
1093 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001094 case 'L':
1095 volume_label = optarg;
1096 break;
1097 case 'M':
1098 mount_dir = optarg;
1099 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001100 case 'N':
1101 num_inodes = strtoul(optarg, &tmp, 0);
1102 if (*tmp) {
1103 com_err(program_name, 0,
1104 _("bad num inodes - %s"), optarg);
1105 exit(1);
1106 }
1107 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001108 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001109 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001110 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001111 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001112 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001113 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001114 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001115 case 'S':
1116 super_only = 1;
1117 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001118 case 'T':
1119 fs_type = optarg;
1120 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001121 case 'V':
1122 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001123 show_version_only++;
1124 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001125 default:
1126 usage();
1127 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001128 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001129 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001130 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001131 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001132
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001133 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001134 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1135 E2FSPROGS_DATE);
1136
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001137 if (show_version_only) {
1138 fprintf(stderr, _("\tUsing %s\n"),
1139 error_message(EXT2_ET_BASE));
1140 exit(0);
1141 }
1142
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001143 /*
1144 * If there's no blocksize specified and there is a journal
1145 * device, use it to figure out the blocksize
1146 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001147 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001148 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001149 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001150
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001151#ifdef CONFIG_TESTIO_DEBUG
1152 io_ptr = test_io_manager;
1153 test_io_backing_manager = unix_io_manager;
1154#else
1155 io_ptr = unix_io_manager;
1156#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001157 retval = ext2fs_open(journal_device,
1158 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001159 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001160 if (retval) {
1161 com_err(program_name, retval,
1162 _("while trying to open journal device %s\n"),
1163 journal_device);
1164 exit(1);
1165 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001166 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001167 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001168 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001169 "minimum blocksize %d\n"), jfs->blocksize,
1170 -blocksize);
1171 exit(1);
1172 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001173 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001174 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001175 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1176 ext2fs_close(jfs);
1177 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001178
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001179 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001180 if (!force) {
1181 com_err(program_name, 0,
1182 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001183 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001184 proceed_question();
1185 }
1186 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1187 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001188 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001189 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001190 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001191 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1192 fs_param.s_log_block_size);
1193 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001194 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001195 argv[optind - 1]);
1196 exit(1);
1197 }
1198 }
1199 if (optind < argc)
1200 usage();
1201
Theodore Ts'o74becf31997-04-26 14:37:06 +00001202 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001203 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001204 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001205
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001206 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001207
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001208 if (noaction && fs_param.s_blocks_count) {
1209 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001210 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001211 } else {
1212 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001213 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001214 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001215 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001216 if ((retval == EFBIG) &&
1217 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001218 (fs_param.s_log_block_size == 0)) {
1219 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001220 blocksize = 4096;
1221 goto retry;
1222 }
1223 }
1224
Theodore Ts'oa789d841998-03-30 01:20:55 +00001225 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1226 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001227 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001228 exit(1);
1229 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001230 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001231 if (retval == EXT2_ET_UNIMPLEMENTED) {
1232 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001233 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001234 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001235 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001236 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001237 } else {
1238 if (dev_size == 0) {
1239 com_err(program_name, 0,
1240 _("Device size reported to be zero. "
1241 "Invalid partition specified, or\n\t"
1242 "partition table wasn't reread "
1243 "after running fdisk, due to\n\t"
1244 "a modified partition being busy "
1245 "and in use. You may need to reboot\n\t"
1246 "to re-read your partition table.\n"
1247 ));
1248 exit(1);
1249 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001250 fs_param.s_blocks_count = dev_size;
1251 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1252 fs_param.s_blocks_count &= ~((sys_page_size /
1253 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001254 }
1255
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001256 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001257 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001258 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001259 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001260 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001261
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001262 if (!fs_type) {
1263 int megs = fs_param.s_blocks_count *
1264 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1265
1266 if (megs <= 3)
1267 fs_type = "floppy";
1268 else if (megs <= 512)
1269 fs_type = "small";
1270 else
1271 fs_type = "default";
1272 }
1273
1274 /* Figure out what features should be enabled */
1275
1276 if (r_opt == EXT2_GOOD_OLD_REV && fs_features) {
1277 fprintf(stderr, _("Filesystem features not supported "
1278 "with revision 0 filesystems\n"));
1279 exit(1);
1280 }
1281
1282 profile_get_string(profile, "defaults", "base_features", 0,
1283 "filetype,sparse_super", &tmp);
1284 profile_get_string(profile, "fs_types", fs_type, "base_features",
1285 tmp, &tmp2);
1286 edit_feature(tmp2, &fs_param.s_feature_compat);
1287 free(tmp);
1288 free(tmp2);
1289
1290 profile_get_string(profile, "defaults", "default_features", 0,
1291 "", &tmp);
1292 profile_get_string(profile, "fs_types", fs_type,
1293 "default_features", tmp, &tmp2);
1294 edit_feature(fs_features ? fs_features : tmp2,
1295 &fs_param.s_feature_compat);
1296 free(tmp);
1297 free(tmp2);
1298
1299 if (s_opt > 0)
1300 fs_param.s_feature_ro_compat |=
1301 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1302 else if (s_opt == 0)
1303 fs_param.s_feature_ro_compat &=
1304 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1305
1306 if (journal_size != 0)
1307 fs_param.s_feature_compat |=
1308 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1309
1310 if (fs_param.s_feature_incompat &
1311 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1312 if (!fs_type)
1313 fs_type = "journal";
1314 reserved_ratio = 0;
1315 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1316 fs_param.s_feature_compat = 0;
1317 fs_param.s_feature_ro_compat = 0;
1318 }
1319
1320 if (fs_param.s_rev_level == EXT2_GOOD_OLD_REV) {
1321 fs_param.s_feature_incompat = 0;
1322 fs_param.s_feature_compat = 0;
1323 fs_param.s_feature_ro_compat = 0;
1324 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001325
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001326 /* Set first meta blockgroup via an environment variable */
1327 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001328 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001329 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001330 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001331
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001332 /* Get the hardware sector size, if available */
1333 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1334 if (retval) {
1335 com_err(program_name, retval,
1336 _("while trying to determine hardware sector size"));
1337 exit(1);
1338 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001339
Theodore Ts'o54434922003-12-07 01:28:50 -05001340 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001341 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001342
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001343 if (blocksize <= 0) {
1344 profile_get_integer(profile, "defaults", "blocksize", 0,
1345 1024, &use_bsize);
1346 profile_get_integer(profile, "fs_types", fs_type,
1347 "blocksize", use_bsize, &use_bsize);
1348
1349 if (use_bsize == -1) {
1350 use_bsize = sys_page_size;
1351 if ((linux_version_code < (2*65536 + 6*256)) &&
1352 (use_bsize > 4096))
1353 use_bsize = 4096;
1354 }
1355 if (sector_size && use_bsize < sector_size)
1356 use_bsize = sector_size;
1357 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1358 use_bsize = -blocksize;
1359 blocksize = use_bsize;
1360 fs_param.s_blocks_count /= blocksize / 1024;
1361 }
1362
1363 if (inode_ratio == 0) {
1364 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1365 8192, &inode_ratio);
1366 profile_get_integer(profile, "fs_types", fs_type,
1367 "inode_ratio", inode_ratio,
1368 &inode_ratio);
1369
1370 if (inode_ratio < blocksize)
1371 inode_ratio = blocksize;
1372 }
1373
1374 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1375 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1376
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001377 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001378
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001379 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001380 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001381
1382 /* Since sparse_super is the default, we would only have a problem
1383 * here if it was explicitly disabled.
1384 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001385 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1386 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001387 com_err(program_name, 0,
1388 _("reserved online resize blocks not supported "
1389 "on non-sparse filesystem"));
1390 exit(1);
1391 }
1392
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001393 if (fs_param.s_blocks_per_group) {
1394 if (fs_param.s_blocks_per_group < 256 ||
1395 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001396 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001397 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001398 exit(1);
1399 }
1400 }
1401
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001402 if (!force && fs_param.s_blocks_count >= (1 << 31)) {
Theodore Ts'o675b79c2005-06-30 20:00:41 -04001403 com_err(program_name, 0,
1404 _("Filesystem too large. No more than 2**31-1 blocks\n"
1405 "\t (8TB using a blocksize of 4k) are currently supported."));
1406 exit(1);
1407 }
1408
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001409 if ((blocksize > 4096) &&
1410 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1411 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1412 "blocksizes greater than 4096\n\tusing ext3. "
1413 "Use -b 4096 if this is an issue for you.\n\n"));
1414
Andreas Dilger067911a2006-07-15 22:08:20 -04001415 if (inode_size == 0) {
1416 profile_get_integer(profile, "defaults", "inode_size", NULL,
1417 0, &inode_size);
1418 profile_get_integer(profile, "fs_types", fs_type,
1419 "inode_size", inode_size,
1420 &inode_size);
1421 }
1422
1423 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001424 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001425 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001426 inode_size & (inode_size - 1)) {
1427 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001428 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001429 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001430 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001431 exit(1);
1432 }
1433 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1434 fprintf(stderr, _("Warning: %d-byte inodes not usable "
Andreas Dilger067911a2006-07-15 22:08:20 -04001435 "on older systems\n"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001436 inode_size);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001437 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001438 }
1439
Theodore Ts'o3839e651997-04-26 13:21:57 +00001440 /*
1441 * Calculate number of inodes based on the inode ratio
1442 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001443 fs_param.s_inodes_count = num_inodes ? num_inodes :
1444 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001445 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001446
1447 /*
1448 * Calculate number of blocks to reserve
1449 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001450 fs_param.s_r_blocks_count = (fs_param.s_blocks_count * reserved_ratio)
1451 / 100;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001452}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001453
Theodore Ts'o3839e651997-04-26 13:21:57 +00001454int main (int argc, char *argv[])
1455{
1456 errcode_t retval = 0;
1457 ext2_filsys fs;
1458 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001459 int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001460 unsigned int i;
1461 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001462 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001463
1464#ifdef ENABLE_NLS
1465 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001466 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001467 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1468 textdomain(NLS_CAT_NAME);
1469#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001470 PRS(argc, argv);
1471
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001472#ifdef CONFIG_TESTIO_DEBUG
1473 io_ptr = test_io_manager;
1474 test_io_backing_manager = unix_io_manager;
1475#else
1476 io_ptr = unix_io_manager;
1477#endif
1478
Theodore Ts'o3839e651997-04-26 13:21:57 +00001479 /*
1480 * Initialize the superblock....
1481 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001482 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001483 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001484 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001485 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001486 exit(1);
1487 }
1488
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001489 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001490 * Wipe out the old on-disk superblock
1491 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001492 if (!noaction)
1493 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001494
1495 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001496 * Generate a UUID for it...
1497 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001498 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001499
1500 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001501 * Initialize the directory index variables
1502 */
1503 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1504 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1505
1506 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001507 * Add "jitter" to the superblock's check interval so that we
1508 * don't check all the filesystems at the same time. We use a
1509 * kludgy hack of using the UUID to derive a random jitter value.
1510 */
1511 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1512 val += fs->super->s_uuid[i];
1513 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1514
1515 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001516 * Override the creator OS, if applicable
1517 */
1518 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001519 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001520 exit(1);
1521 }
1522
1523 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001524 * For the Hurd, we will turn off filetype since it doesn't
1525 * support it.
1526 */
1527 if (fs->super->s_creator_os == EXT2_OS_HURD)
1528 fs->super->s_feature_incompat &=
1529 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1530
1531 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001532 * Set the volume label...
1533 */
1534 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001535 memset(fs->super->s_volume_name, 0,
1536 sizeof(fs->super->s_volume_name));
1537 strncpy(fs->super->s_volume_name, volume_label,
1538 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001539 }
1540
1541 /*
1542 * Set the last mount directory
1543 */
1544 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001545 memset(fs->super->s_last_mounted, 0,
1546 sizeof(fs->super->s_last_mounted));
1547 strncpy(fs->super->s_last_mounted, mount_dir,
1548 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001549 }
1550
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001551 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001552 show_stats(fs);
1553
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001554 if (noaction)
1555 exit(0);
1556
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001557 if (fs->super->s_feature_incompat &
1558 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1559 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001560 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001561 }
1562
Theodore Ts'o3839e651997-04-26 13:21:57 +00001563 if (bad_blocks_filename)
1564 read_bb_file(fs, &bb_list, bad_blocks_filename);
1565 if (cflag)
1566 test_disk(fs, &bb_list);
1567
1568 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001569 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001570 retval = ext2fs_allocate_tables(fs);
1571 if (retval) {
1572 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001573 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001574 exit(1);
1575 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001576 if (super_only) {
1577 fs->super->s_state |= EXT2_ERROR_FS;
1578 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1579 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001580 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001581 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001582 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001583 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001584 blk_t ret_blk;
1585
1586#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001587 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001588#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001589
1590 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001591 * Wipe out any old MD RAID (or other) metadata at the end
1592 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001593 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001594 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001595 start = (blocks & ~(rsv - 1));
1596 if (start > rsv)
1597 start -= rsv;
1598 if (start > 0)
1599 retval = zero_blocks(fs, start, blocks - start,
1600 NULL, &ret_blk, NULL);
1601
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001602 if (retval) {
1603 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001604 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001605 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001606 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001607 setup_lazy_bg(fs);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001608 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001609 create_root_dir(fs);
1610 create_lost_and_found(fs);
1611 reserve_inodes(fs);
1612 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001613 if (fs->super->s_feature_compat &
1614 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1615 retval = ext2fs_create_resize_inode(fs);
1616 if (retval) {
1617 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001618 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001619 exit(1);
1620 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001621 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001622 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001623
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001624 if (journal_device) {
1625 ext2_filsys jfs;
1626
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001627 if (!force)
1628 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001629 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001630
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001631 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1632 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1633 fs->blocksize, unix_io_manager, &jfs);
1634 if (retval) {
1635 com_err(program_name, retval,
1636 _("while trying to open journal device %s\n"),
1637 journal_device);
1638 exit(1);
1639 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001640 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001641 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001642 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001643 fflush(stdout);
1644 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001645 retval = ext2fs_add_journal_device(fs, jfs);
1646 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001647 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001648 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001649 journal_device);
1650 exit(1);
1651 }
1652 if (!quiet)
1653 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001654 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001655 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001656 } else if ((journal_size) ||
1657 (fs_param.s_feature_compat &
1658 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001659 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001660
1661 if (!journal_blocks) {
1662 fs->super->s_feature_compat &=
1663 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1664 goto no_journal;
1665 }
1666 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001667 printf(_("Creating journal (%d blocks): "),
1668 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001669 fflush(stdout);
1670 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001671 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1672 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001673 if (retval) {
1674 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001675 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001676 exit(1);
1677 }
1678 if (!quiet)
1679 printf(_("done\n"));
1680 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001681no_journal:
1682
Theodore Ts'o3839e651997-04-26 13:21:57 +00001683 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001684 printf(_("Writing superblocks and "
1685 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001686 retval = ext2fs_flush(fs);
1687 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05001688 fprintf(stderr,
1689 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001690 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001691 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001692 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001693 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1694 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001695 }
Andreas Dilger568101f2001-10-13 01:22:25 -06001696 val = ext2fs_close(fs);
1697 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001698}