blob: 9940d3f1ac58fcdf75ce298f46ae8144c8736448 [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'oa6d83022006-12-26 03:38:07 -050055#include "prof_err.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000057#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058
59#define STRIDE_LENGTH 8
60
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000061#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000062#define ZAP_BOOTBLOCK
63#endif
64
Theodore Ts'o3839e651997-04-26 13:21:57 +000065extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000066extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000069const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000070
71/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000072int cflag;
73int verbose;
74int quiet;
75int super_only;
76int force;
77int noaction;
78int journal_size;
79int journal_flags;
Theodore Ts'oa4396e92008-04-18 10:19:27 -040080int lazy_itable_init;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000081char *bad_blocks_filename;
82__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050084struct ext2_super_block fs_param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000085char *creator_os;
86char *volume_label;
87char *mount_dir;
88char *journal_device;
89int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000090
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -050091profile_t profile;
92
Theodore Ts'o31e29a12002-05-17 10:53:07 -040093int sys_page_size = 4096;
Theodore Ts'od99225e2004-09-25 07:40:12 -040094int linux_version_code = 0;
Theodore Ts'o31e29a12002-05-17 10:53:07 -040095
Theodore Ts'o63985322001-01-03 17:02:13 +000096static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000097{
Theodore Ts'obdd80f22008-02-28 21:12:31 -050098 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
Andreas Dilger067911a2006-07-15 22:08:20 -040099 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500100 "[-J journal-options]\n"
Andreas Dilger067911a2006-07-15 22:08:20 -0400101 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
102 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
103 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
Theodore Ts'obdd80f22008-02-28 21:12:31 -0500104 "[-r fs-revision] [-E extended-option[,...]]\n"
105 "\t[-T fs-type] [-jnqvFSV] 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'od99225e2004-09-25 07:40:12 -0400131static int parse_version_number(const char *s)
132{
133 int major, minor, rev;
134 char *endptr;
135 const char *cp = s;
136
137 if (!s)
138 return 0;
139 major = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
141 return 0;
142 cp = endptr + 1;
143 minor = strtol(cp, &endptr, 10);
144 if (cp == endptr || *endptr != '.')
145 return 0;
146 cp = endptr + 1;
147 rev = strtol(cp, &endptr, 10);
148 if (cp == endptr)
149 return 0;
150 return ((((major * 256) + minor) * 256) + rev);
151}
152
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000153/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 * Helper function for read_bb_file and test_disk
155 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500156static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157{
Theodore Ts'o66938372002-03-08 00:14:46 -0500158 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 return;
160}
161
162/*
163 * Reads the bad blocks list from a file
164 */
165static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
166 const char *bad_blocks_file)
167{
168 FILE *f;
169 errcode_t retval;
170
171 f = fopen(bad_blocks_file, "r");
172 if (!f) {
173 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000174 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 exit(1);
176 }
177 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
178 fclose (f);
179 if (retval) {
180 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000181 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 exit(1);
183 }
184}
185
186/*
187 * Runs the badblocks program to test the disk
188 */
189static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
190{
191 FILE *f;
192 errcode_t retval;
193 char buf[1024];
194
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400195 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500196 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
Theodore Ts'o8ade4792006-11-08 00:41:50 -0500197 fs->device_name, fs->super->s_blocks_count-1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000199 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 f = popen(buf, "r");
201 if (!f) {
202 com_err("popen", errno,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400203 _("while trying to run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 exit(1);
205 }
206 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000207 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 if (retval) {
209 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000210 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 exit(1);
212 }
213}
214
215static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
216{
Theodore Ts'o54434922003-12-07 01:28:50 -0500217 dgrp_t i;
218 blk_t j;
219 unsigned must_be_good;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 blk_t blk;
221 badblocks_iterate bb_iter;
222 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000223 blk_t group_block;
224 int group;
225 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226
227 if (!bb_list)
228 return;
229
230 /*
231 * The primary superblock and group descriptors *must* be
232 * good; if not, abort.
233 */
234 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
235 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000236 if (ext2fs_badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000237 fprintf(stderr, _("Block %d in primary "
238 "superblock/group descriptor area bad.\n"), i);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400239 fprintf(stderr, _("Blocks %u through %u must be good "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000240 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 fs->super->s_first_data_block, must_be_good);
Theodore Ts'o54434922003-12-07 01:28:50 -0500242 fputs(_("Aborting....\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 exit(1);
244 }
245 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000246
247 /*
248 * See if any of the bad blocks are showing up in the backup
249 * superblocks and/or group descriptors. If so, issue a
250 * warning and adjust the block counts appropriately.
251 */
252 group_block = fs->super->s_first_data_block +
253 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000254
255 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000256 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257 for (j=0; j < fs->desc_blocks+1; j++) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000258 if (ext2fs_badblocks_list_test(bb_list,
259 group_block + j)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000260 if (!group_bad)
261 fprintf(stderr,
Takashi Sato8deb80a2006-03-18 21:43:46 -0500262_("Warning: the backup superblock/group descriptors at block %u contain\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000263" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000264 group_block);
265 group_bad++;
266 group = ext2fs_group_of_blk(fs, group_block+j);
267 fs->group_desc[group].bg_free_blocks_count++;
268 fs->super->s_free_blocks_count++;
269 }
270 }
271 group_block += fs->super->s_blocks_per_group;
272 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273
274 /*
275 * Mark all the bad blocks as used...
276 */
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000277 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000279 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000280 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281 exit(1);
282 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000283 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000284 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000285 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286}
287
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000288/*
289 * These functions implement a generalized progress meter.
290 */
291struct progress_struct {
292 char format[20];
293 char backup[80];
294 __u32 max;
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400295 int skip_progress;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000296};
297
298static void progress_init(struct progress_struct *progress,
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500299 const char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000301 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000302
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000303 memset(progress, 0, sizeof(struct progress_struct));
304 if (quiet)
305 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000306
307 /*
308 * Figure out how many digits we need
309 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000310 i = int_log10(max);
311 sprintf(progress->format, "%%%dd/%%%dld", i, i);
312 memset(progress->backup, '\b', sizeof(progress->backup)-1);
313 progress->backup[sizeof(progress->backup)-1] = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500314 if ((2*i)+1 < (int) sizeof(progress->backup))
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000315 progress->backup[(2*i)+1] = 0;
316 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000317
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400318 progress->skip_progress = 0;
319 if (getenv("MKE2FS_SKIP_PROGRESS"))
320 progress->skip_progress++;
321
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000322 fputs(label, stdout);
323 fflush(stdout);
324}
325
326static void progress_update(struct progress_struct *progress, __u32 val)
327{
Theodore Ts'o1cca86f2003-09-01 09:28:18 -0400328 if ((progress->format[0] == 0) || progress->skip_progress)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000329 return;
330 printf(progress->format, val, progress->max);
331 fputs(progress->backup, stdout);
332}
333
334static void progress_close(struct progress_struct *progress)
335{
336 if (progress->format[0] == 0)
337 return;
338 fputs(_("done \n"), stdout);
339}
340
341
342/*
343 * Helper function which zeros out _num_ blocks starting at _blk_. In
344 * case of an error, the details of the error is returned via _ret_blk_
345 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
346 * success, and an error code on an error.
347 *
348 * As a special case, if the first argument is NULL, then it will
349 * attempt to free the static zeroizing buffer. (This is to keep
350 * programs that check for memory leaks happy.)
351 */
352static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
353 struct progress_struct *progress,
354 blk_t *ret_blk, int *ret_count)
355{
356 int j, count, next_update, next_update_incr;
357 static char *buf;
358 errcode_t retval;
359
360 /* If fs is null, clean up the static buffer and return */
361 if (!fs) {
362 if (buf) {
363 free(buf);
364 buf = 0;
365 }
366 return 0;
367 }
368 /* Allocate the zeroizing buffer if necessary */
369 if (!buf) {
370 buf = malloc(fs->blocksize * STRIDE_LENGTH);
371 if (!buf) {
372 com_err("malloc", ENOMEM,
373 _("while allocating zeroizing buffer"));
374 exit(1);
375 }
376 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
377 }
378 /* OK, do the write loop */
379 next_update = 0;
380 next_update_incr = num / 100;
381 if (next_update_incr < 1)
382 next_update_incr = 1;
383 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
Theodore Ts'of044b4d2002-08-17 13:32:21 -0400384 count = num - j;
385 if (count > STRIDE_LENGTH)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000386 count = STRIDE_LENGTH;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000387 retval = io_channel_write_blk(fs->io, blk, count, buf);
388 if (retval) {
389 if (ret_count)
390 *ret_count = count;
391 if (ret_blk)
392 *ret_blk = blk;
393 return retval;
394 }
395 if (progress && j > next_update) {
396 next_update += num / 100;
397 progress_update(progress, blk);
398 }
399 }
400 return 0;
401}
402
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400403static void write_inode_tables(ext2_filsys fs, int lazy_flag)
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000404{
405 errcode_t retval;
406 blk_t blk;
Theodore Ts'o54434922003-12-07 01:28:50 -0500407 dgrp_t i;
408 int num;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000409 struct progress_struct progress;
410
411 if (quiet)
412 memset(&progress, 0, sizeof(progress));
413 else
414 progress_init(&progress, _("Writing inode tables: "),
415 fs->group_desc_count);
416
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400417 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
418 EXT2_FEATURE_COMPAT_LAZY_BG))
419 lazy_flag = 1;
420
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000422 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000424 blk = fs->group_desc[i].bg_inode_table;
425 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000426
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400427 if (!(lazy_flag &&
428 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
429 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
430 if (retval) {
431 fprintf(stderr, _("\nCould not write %d "
432 "blocks in inode table starting at %u: %s\n"),
433 num, blk, error_message(retval));
434 exit(1);
435 }
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500436 /* The kernel doesn't need to zero the itable blocks */
437 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000438 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000439 if (sync_kludge) {
440 if (sync_kludge == 1)
441 sync();
442 else if ((i % sync_kludge) == 0)
443 sync();
444 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000446 zero_blocks(0, 0, 0, 0, 0, 0);
447 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448}
449
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400450static void setup_lazy_bg(ext2_filsys fs)
451{
452 dgrp_t i;
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500453 int blks, csum_flag;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400454 struct ext2_super_block *sb = fs->super;
455 struct ext2_group_desc *bg = fs->group_desc;
456
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500457 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
458 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
459 if (EXT2_HAS_COMPAT_FEATURE(fs->super, EXT2_FEATURE_COMPAT_LAZY_BG) ||
460 csum_flag) {
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400461 for (i = 0; i < fs->group_desc_count; i++, bg++) {
462 if ((i == 0) ||
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500463 (i == fs->group_desc_count - 1 && !csum_flag))
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400464 continue;
465 if (bg->bg_free_inodes_count ==
466 sb->s_inodes_per_group) {
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400467 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500468 if (!csum_flag) {
469 bg->bg_free_inodes_count = 0;
470 sb->s_free_inodes_count -=
471 sb->s_inodes_per_group;
472 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400473 }
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500474
475 /* Skip groups with GDT backups because the resize
476 * inode has blocks allocated in them, and the last
477 * group because it needs block bitmap padding. */
478 if ((ext2fs_bg_has_super(fs, i) &&
479 sb->s_reserved_gdt_blocks) ||
480 i == fs->group_desc_count - 1)
481 continue;
482
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400483 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500484 if (bg->bg_free_blocks_count == blks &&
485 bg->bg_flags & EXT2_BG_INODE_UNINIT) {
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400486 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500487 if (!csum_flag) {
488 bg->bg_free_blocks_count = 0;
489 sb->s_free_blocks_count -= blks;
490 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400491 }
492 }
493 }
494}
495
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496static void create_root_dir(ext2_filsys fs)
497{
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400498 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499 struct ext2_inode inode;
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400500 __u32 uid, gid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501
502 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
503 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000504 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 exit(1);
506 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000507 if (geteuid()) {
508 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
509 if (retval) {
510 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000511 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000512 exit(1);
513 }
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400514 uid = getuid();
515 inode.i_uid = uid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500516 ext2fs_set_i_uid_high(inode, uid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400517 if (uid) {
518 gid = getgid();
519 inode.i_gid = gid;
Theodore Ts'o15343922008-01-21 09:45:25 -0500520 ext2fs_set_i_gid_high(inode, gid >> 16);
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400521 }
Theodore Ts'oe27b4562005-03-21 01:02:53 -0500522 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000523 if (retval) {
524 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000525 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000526 exit(1);
527 }
528 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529}
530
531static void create_lost_and_found(ext2_filsys fs)
532{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400533 unsigned int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000535 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 const char *name = "lost+found";
537 int i;
538
Theodore Ts'o6a525062001-12-24 09:40:00 -0500539 fs->umask = 077;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
541 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000542 com_err("ext2fs_mkdir", retval,
543 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 exit(1);
545 }
546
547 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
548 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000549 com_err("ext2_lookup", retval,
550 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 exit(1);
552 }
553
554 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Andreas Dilger8c7c6eb2008-01-09 20:59:47 +0100555 /* Ensure that lost+found is at least 2 blocks, so we always
556 * test large empty blocks for big-block filesystems. */
557 if ((lpf_size += fs->blocksize) >= 16*1024 &&
558 lpf_size >= 2 * fs->blocksize)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000559 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560 retval = ext2fs_expand_dir(fs, ino);
561 if (retval) {
562 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000563 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 exit(1);
565 }
Theodore Ts'o6a525062001-12-24 09:40:00 -0500566 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567}
568
569static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
570{
571 errcode_t retval;
572
Theodore Ts'of3db3561997-04-26 13:34:30 +0000573 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 fs->group_desc[0].bg_free_inodes_count--;
575 fs->super->s_free_inodes_count--;
576 retval = ext2fs_update_bb_inode(fs, bb_list);
577 if (retval) {
578 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000579 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 exit(1);
581 }
582
583}
584
585static void reserve_inodes(ext2_filsys fs)
586{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000587 ext2_ino_t i;
588 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000590 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000591 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 group = ext2fs_group_of_ino(fs, i);
593 fs->group_desc[group].bg_free_inodes_count--;
594 fs->super->s_free_inodes_count--;
595 }
596 ext2fs_mark_ib_dirty(fs);
597}
598
Theodore Ts'o756df352002-03-07 20:52:12 -0500599#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500600#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
Theodore Ts'o756df352002-03-07 20:52:12 -0500601#define BSD_LABEL_OFFSET 64
Theodore Ts'o756df352002-03-07 20:52:12 -0500602
Theodore Ts'o04a96852001-08-30 21:55:26 -0400603static void zap_sector(ext2_filsys fs, int sect, int nsect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000604{
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400605 char *buf;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000606 int retval;
Theodore Ts'o756df352002-03-07 20:52:12 -0500607 unsigned int *magic;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400609 buf = malloc(512*nsect);
Andreas Dilger568101f2001-10-13 01:22:25 -0600610 if (!buf) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500611 printf(_("Out of memory erasing sectors %d-%d\n"),
612 sect, sect + nsect - 1);
Andreas Dilger568101f2001-10-13 01:22:25 -0600613 exit(1);
614 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500615
Theodore Ts'o7ef3bb82002-03-08 03:01:53 -0500616 if (sect == 0) {
617 /* Check for a BSD disklabel, and don't erase it if so */
618 retval = io_channel_read_blk(fs->io, 0, -512, buf);
619 if (retval)
620 fprintf(stderr,
621 _("Warning: could not read block 0: %s\n"),
622 error_message(retval));
623 else {
624 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
625 if ((*magic == BSD_DISKMAGIC) ||
626 (*magic == BSD_MAGICDISK))
627 return;
628 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500629 }
Theodore Ts'o756df352002-03-07 20:52:12 -0500630
Theodore Ts'o70988102002-07-14 08:00:00 -0400631 memset(buf, 0, 512*nsect);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000632 io_channel_set_blksize(fs->io, 512);
Theodore Ts'o04a96852001-08-30 21:55:26 -0400633 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000634 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'o3f85f652001-09-17 10:38:06 -0400635 free(buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000636 if (retval)
Theodore Ts'o66938372002-03-08 00:14:46 -0500637 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
638 sect, error_message(retval));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000639}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000640
641static void create_journal_dev(ext2_filsys fs)
642{
643 struct progress_struct progress;
644 errcode_t retval;
645 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000646 blk_t blk;
647 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000648
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000649 retval = ext2fs_create_journal_superblock(fs,
650 fs->super->s_blocks_count, 0, &buf);
651 if (retval) {
652 com_err("create_journal_dev", retval,
653 _("while initializing journal superblock"));
654 exit(1);
655 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000656 if (quiet)
657 memset(&progress, 0, sizeof(progress));
658 else
659 progress_init(&progress, _("Zeroing journal device: "),
660 fs->super->s_blocks_count);
661
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000662 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
663 &progress, &blk, &count);
664 if (retval) {
665 com_err("create_journal_dev", retval,
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +0000666 _("while zeroing journal device (block %u, count %d)"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000667 blk, count);
668 exit(1);
669 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000670 zero_blocks(0, 0, 0, 0, 0, 0);
671
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000672 retval = io_channel_write_blk(fs->io,
673 fs->super->s_first_data_block+1,
674 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000675 if (retval) {
676 com_err("create_journal_dev", retval,
677 _("while writing journal superblock"));
678 exit(1);
679 }
680 progress_close(&progress);
681}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000682
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683static void show_stats(ext2_filsys fs)
684{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000685 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000686 char buf[80];
Theodore Ts'o63253942005-03-19 01:13:22 -0500687 char *os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688 blk_t group_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500689 dgrp_t i;
690 int need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500692 if (fs_param.s_blocks_count != s->s_blocks_count)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500693 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500694 fs_param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000695
696 memset(buf, 0, sizeof(buf));
697 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000698 printf(_("Filesystem label=%s\n"), buf);
Theodore Ts'o54434922003-12-07 01:28:50 -0500699 fputs(_("OS type: "), stdout);
Theodore Ts'o63253942005-03-19 01:13:22 -0500700 os = e2p_os2string(fs->super->s_creator_os);
701 fputs(os, stdout);
702 free(os);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000703 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000704 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000705 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000706 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000707 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000708 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000710 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711 s->s_r_blocks_count,
712 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000713 printf(_("First data block=%u\n"), s->s_first_data_block);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500714 if (s->s_reserved_gdt_blocks)
715 printf(_("Maximum filesystem blocks=%lu\n"),
716 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
Valerie Clementf2de1d32007-08-30 17:38:13 +0200717 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000718 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000719 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000720 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000721 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000722 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000724 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725
726 if (fs->group_desc_count == 1) {
727 printf("\n");
728 return;
729 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500730
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000731 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732 group_block = s->s_first_data_block;
733 col_left = 0;
734 for (i = 1; i < fs->group_desc_count; i++) {
735 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000736 if (!ext2fs_bg_has_super(fs, i))
737 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000738 if (i != 1)
739 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000740 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000741 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000742 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000743 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000745 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000746 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 }
748 printf("\n\n");
749}
750
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000751/*
752 * Set the S_CREATOR_OS field. Return true if OS is known,
753 * otherwise, 0.
754 */
755static int set_os(struct ext2_super_block *sb, char *os)
756{
757 if (isdigit (*os))
758 sb->s_creator_os = atoi (os);
759 else if (strcasecmp(os, "linux") == 0)
760 sb->s_creator_os = EXT2_OS_LINUX;
761 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
762 sb->s_creator_os = EXT2_OS_HURD;
Theodore Ts'oea1e8f42005-01-19 18:18:44 -0500763 else if (strcasecmp(os, "freebsd") == 0)
764 sb->s_creator_os = EXT2_OS_FREEBSD;
765 else if (strcasecmp(os, "lites") == 0)
766 sb->s_creator_os = EXT2_OS_LITES;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000767 else
768 return 0;
769 return 1;
770}
771
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000772#define PATH_SET "PATH=/sbin"
773
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500774static void parse_extended_opts(struct ext2_super_block *param,
775 const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000776{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400777 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000778 int len;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500779 int r_usage = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000780
781 len = strlen(opts);
782 buf = malloc(len+1);
783 if (!buf) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500784 fprintf(stderr,
785 _("Couldn't allocate memory to parse options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000786 exit(1);
787 }
788 strcpy(buf, opts);
789 for (token = buf; token && *token; token = next) {
790 p = strchr(token, ',');
791 next = 0;
792 if (p) {
793 *p = 0;
794 next = p+1;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500795 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000796 arg = strchr(token, '=');
797 if (arg) {
798 *arg = 0;
799 arg++;
800 }
801 if (strcmp(token, "stride") == 0) {
802 if (!arg) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500803 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500804 badopt = token;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000805 continue;
806 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500807 param->s_raid_stride = strtoul(arg, &p, 0);
808 if (*p || (param->s_raid_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000809 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400810 _("Invalid stride parameter: %s\n"),
811 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500812 r_usage++;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000813 continue;
814 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500815 } else if (strcmp(token, "stripe-width") == 0 ||
816 strcmp(token, "stripe_width") == 0) {
817 if (!arg) {
818 r_usage++;
819 badopt = token;
820 continue;
821 }
822 param->s_raid_stripe_width = strtoul(arg, &p, 0);
823 if (*p || (param->s_raid_stripe_width == 0)) {
824 fprintf(stderr,
825 _("Invalid stripe-width parameter: %s\n"),
826 arg);
827 r_usage++;
828 continue;
829 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500830 } else if (!strcmp(token, "resize")) {
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500831 unsigned long resize, bpg, rsv_groups;
832 unsigned long group_desc_count, desc_blocks;
833 unsigned int gdpb, blocksize;
834 int rsv_gdb;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500835
836 if (!arg) {
837 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500838 badopt = token;
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500839 continue;
840 }
841
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500842 resize = parse_num_blocks(arg,
843 param->s_log_block_size);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500844
845 if (resize == 0) {
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500846 fprintf(stderr,
847 _("Invalid resize parameter: %s\n"),
848 arg);
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500849 r_usage++;
850 continue;
851 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500852 if (resize <= param->s_blocks_count) {
853 fprintf(stderr,
Theodore Ts'of37ab682005-05-05 23:15:55 -0400854 _("The resize maximum must be greater "
855 "than the filesystem size.\n"));
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500856 r_usage++;
857 continue;
858 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500859
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500860 blocksize = EXT2_BLOCK_SIZE(param);
861 bpg = param->s_blocks_per_group;
862 if (!bpg)
863 bpg = blocksize * 8;
Valerie Clementf2de1d32007-08-30 17:38:13 +0200864 gdpb = EXT2_DESC_PER_BLOCK(param);
Theodore Ts'o69022e02006-08-30 01:57:00 -0400865 group_desc_count =
866 ext2fs_div_ceil(param->s_blocks_count, bpg);
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500867 desc_blocks = (group_desc_count +
868 gdpb - 1) / gdpb;
Theodore Ts'o69022e02006-08-30 01:57:00 -0400869 rsv_groups = ext2fs_div_ceil(resize, bpg);
870 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500871 desc_blocks;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500872 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500873 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
874
875 if (rsv_gdb > 0) {
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400876 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
877 fprintf(stderr,
878 _("On-line resizing not supported with revision 0 filesystems\n"));
Brian Behlendorf21400382007-05-31 11:30:47 -0400879 free(buf);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -0400880 exit(1);
881 }
Theodore Ts'oc6a44132005-01-05 11:12:20 -0500882 param->s_feature_compat |=
883 EXT2_FEATURE_COMPAT_RESIZE_INODE;
884
885 param->s_reserved_gdt_blocks = rsv_gdb;
886 }
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500887 } else if (!strcmp(token, "test_fs")) {
888 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400889 } else if (!strcmp(token, "lazy_itable_init")) {
890 if (!arg) {
891 r_usage++;
892 badopt = token;
893 continue;
894 }
895 lazy_itable_init = strtoul(arg, &p, 0);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500896 } else {
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500897 r_usage++;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500898 badopt = token;
899 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000900 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500901 if (r_usage) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500902 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400903 "Extended options are separated by commas, "
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000904 "and may take an argument which\n"
905 "\tis set off by an equals ('=') sign.\n\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400906 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500907 "\tstride=<RAID per-disk data chunk in blocks>\n"
908 "\tstripe-width=<RAID stride * data disks in blocks>\n"
Theodore Ts'oa4396e92008-04-18 10:19:27 -0400909 "\tresize=<resize maximum size in blocks>\n"
910 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
911 "\ttest_fs\n\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400912 badopt ? badopt : "");
Brian Behlendorf21400382007-05-31 11:30:47 -0400913 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000914 exit(1);
915 }
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500916 if (param->s_raid_stride &&
917 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
918 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
919 "multiple of stride %u.\n\n"),
920 param->s_raid_stripe_width, param->s_raid_stride);
921
Brian Behlendorf21400382007-05-31 11:30:47 -0400922 free(buf);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000923}
924
Theodore Ts'o896938d1999-10-23 01:04:50 +0000925static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500926 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400927 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'od323f8f2004-12-15 14:39:16 -0500928 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400929 EXT2_FEATURE_COMPAT_DIR_INDEX |
Theodore Ts'o0d1de232008-01-27 19:38:46 -0500930 EXT2_FEATURE_COMPAT_LAZY_BG |
Theodore Ts'o558df542008-02-27 15:01:19 -0500931 EXT2_FEATURE_COMPAT_EXT_ATTR,
932 /* Incompat */
933 EXT2_FEATURE_INCOMPAT_FILETYPE|
Theodore Ts'obf6b8482008-02-20 08:13:19 -0500934 EXT3_FEATURE_INCOMPAT_EXTENTS|
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400935 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
Jose R. Santosc2d43002007-08-13 23:32:57 -0500936 EXT2_FEATURE_INCOMPAT_META_BG|
937 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500938 /* R/O compat */
939 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
Jose R. Santosd2d22a22007-10-21 21:03:36 -0500940 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
941 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000942};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000943
944
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500945static void syntax_err_report(const char *filename, long err, int line_num)
946{
947 fprintf(stderr,
948 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
949 filename, line_num, error_message(err));
950 exit(1);
951}
952
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200953static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -0500954
955static void edit_feature(const char *str, __u32 *compat_array)
956{
957 if (!str)
958 return;
959
960 if (e2p_edit_feature(str, compat_array, ok_features)) {
961 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
962 str);
963 exit(1);
964 }
965}
966
Theodore Ts'o3d438362008-02-19 08:32:58 -0500967struct str_list {
968 char **list;
969 int num;
970 int max;
971};
972
973static errcode_t init_list(struct str_list *sl)
974{
975 sl->num = 0;
976 sl->max = 0;
977 sl->list = malloc((sl->max+1) * sizeof(char *));
978 if (!sl->list)
979 return ENOMEM;
980 sl->list[0] = 0;
981 return 0;
982}
983
984static errcode_t push_string(struct str_list *sl, const char *str)
985{
986 char **new_list;
987
988 if (sl->num >= sl->max) {
989 sl->max += 2;
990 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
991 if (!new_list)
992 return ENOMEM;
993 sl->list = new_list;
994 }
995 sl->list[sl->num] = malloc(strlen(str)+1);
996 if (sl->list[sl->num] == 0)
997 return ENOMEM;
998 strcpy(sl->list[sl->num], str);
999 sl->num++;
1000 sl->list[sl->num] = 0;
1001 return 0;
1002}
1003
1004static void print_str_list(char **list)
1005{
1006 char **cpp;
1007
1008 for (cpp = list; *cpp; cpp++) {
1009 printf("'%s'", *cpp);
1010 if (cpp[1])
1011 fputs(", ", stdout);
1012 }
1013 fputc('\n', stdout);
1014}
1015
1016static char **parse_fs_type(const char *fs_type,
1017 const char *usage_types,
1018 struct ext2_super_block *fs_param,
1019 char *progname)
1020{
1021 const char *ext_type = 0;
1022 char *parse_str;
1023 char *profile_type = 0;
1024 char *cp, *t;
1025 const char *size_type;
1026 struct str_list list;
1027 int state = 0;
1028 unsigned long meg;
1029
1030 if (init_list(&list))
1031 return 0;
1032
1033 if (fs_type)
1034 ext_type = fs_type;
1035 else if (progname) {
1036 ext_type = strrchr(progname, '/');
1037 if (ext_type)
1038 ext_type++;
1039 else
1040 ext_type = progname;
1041
1042 if (!strncmp(ext_type, "mkfs.", 5)) {
1043 ext_type += 5;
1044 if (ext_type[0] == 0)
1045 ext_type = 0;
1046 } else
1047 ext_type = 0;
1048 }
1049
1050 if (!ext_type) {
1051 profile_get_string(profile, "defaults", "fs_type", 0,
1052 "ext2", &profile_type);
1053 ext_type = profile_type;
1054 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1055 ext_type = "ext3";
1056 }
1057
1058 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1059 if (fs_param->s_blocks_count < 3 * meg)
1060 size_type = "floppy";
1061 else if (fs_param->s_blocks_count < 512 * meg)
1062 size_type = "small";
1063 else
1064 size_type = "default";
1065
1066 if (!usage_types)
1067 usage_types = size_type;
1068
1069 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1070 if (!parse_str) {
1071 free(list.list);
1072 return 0;
1073 }
1074 if (usage_types)
1075 strcpy(parse_str, usage_types);
1076 else
1077 *parse_str = '\0';
1078
1079 if (ext_type)
1080 push_string(&list, ext_type);
1081 cp = parse_str;
1082 while (1) {
1083 t = strchr(cp, ',');
1084 if (t)
1085 *t = '\0';
1086
1087 if (*cp)
1088 push_string(&list, cp);
1089 if (t)
1090 cp = t+1;
1091 else {
1092 cp = "";
1093 break;
1094 }
1095 }
1096 free(parse_str);
1097 if (profile_type)
1098 free(profile_type);
1099 return (list.list);
1100}
1101
1102static char *get_string_from_profile(char **fs_types, const char *opt,
1103 const char *def_val)
1104{
1105 char *ret = 0;
1106 char **cpp;
1107 int i;
1108
1109 for (i=0; fs_types[i]; i++);
1110 for (i-=1; i >=0 ; i--) {
1111 profile_get_string(profile, "fs_types", fs_types[i],
1112 opt, 0, &ret);
1113 if (ret)
1114 return ret;
1115 }
1116 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1117 return (ret);
1118}
1119
1120static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1121{
1122 int ret;
1123 char **cpp;
1124
1125 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1126 for (cpp = fs_types; *cpp; cpp++)
1127 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1128 return ret;
1129}
1130
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001131static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1132{
1133 int ret;
1134 char **cpp;
1135
1136 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1137 for (cpp = fs_types; *cpp; cpp++)
1138 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1139 return ret;
1140}
Theodore Ts'o3d438362008-02-19 08:32:58 -05001141
Theodore Ts'od48bc602007-07-04 14:10:46 -04001142extern const char *mke2fs_default_profile;
1143static const char *default_files[] = { "<default>", 0 };
1144
Theodore Ts'o3839e651997-04-26 13:21:57 +00001145static void PRS(int argc, char *argv[])
1146{
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001147 int b, c;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001148 int size;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001149 char *tmp, *tmp2;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001150 int blocksize = 0;
1151 int inode_ratio = 0;
Andreas Dilger932a4892002-05-16 03:20:07 -06001152 int inode_size = 0;
Andreas Dilgerce911142005-07-06 11:50:08 -05001153 double reserved_ratio = 5.0;
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001154 int sector_size = 0;
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001155 int show_version_only = 0;
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001156 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001157 errcode_t retval;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001158 char * oldpath = getenv("PATH");
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001159 char * extended_opts = 0;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -05001160 const char * fs_type = 0;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001161 const char * usage_types = 0;
1162 char **fs_types;
Theodore Ts'o4a600561999-10-26 14:35:51 +00001163 blk_t dev_size;
Theodore Ts'o756df352002-03-07 20:52:12 -05001164#ifdef __linux__
Theodore Ts'o4a600561999-10-26 14:35:51 +00001165 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +00001166#endif
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001167 long sysval;
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001168 int s_opt = -1, r_opt = -1;
1169 char *fs_features = 0;
1170 int use_bsize;
Theodore Ts'o642935c2006-11-14 23:38:17 -05001171 char *newpath;
1172 int pathlen = sizeof(PATH_SET) + 1;
1173
1174 if (oldpath)
1175 pathlen += strlen(oldpath);
1176 newpath = malloc(pathlen);
1177 strcpy(newpath, PATH_SET);
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001178
Theodore Ts'o3839e651997-04-26 13:21:57 +00001179 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001180 if (oldpath) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001181 strcat (newpath, ":");
1182 strcat (newpath, oldpath);
Theodore Ts'o642935c2006-11-14 23:38:17 -05001183 }
1184 putenv (newpath);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001185
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001186 tmp = getenv("MKE2FS_SYNC");
1187 if (tmp)
1188 sync_kludge = atoi(tmp);
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001189
1190 /* Determine the system page size if possible */
1191#ifdef HAVE_SYSCONF
1192#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1193#define _SC_PAGESIZE _SC_PAGE_SIZE
1194#endif
1195#ifdef _SC_PAGESIZE
1196 sysval = sysconf(_SC_PAGESIZE);
Andreas Dilgeraab6fe72002-05-18 13:27:33 -06001197 if (sysval > 0)
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001198 sys_page_size = sysval;
1199#endif /* _SC_PAGESIZE */
1200#endif /* HAVE_SYSCONF */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001201
1202 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1203 config_fn[0] = tmp;
1204 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'od48bc602007-07-04 14:10:46 -04001205 retval = profile_init(config_fn, &profile);
1206 if (retval == ENOENT) {
1207 profile_init(default_files, &profile);
1208 profile_set_default(profile, mke2fs_default_profile);
1209 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001210
Theodore Ts'o3839e651997-04-26 13:21:57 +00001211 setbuf(stdout, NULL);
1212 setbuf(stderr, NULL);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001213 add_error_table(&et_ext2_error_table);
1214 add_error_table(&et_prof_error_table);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001215 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1216 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o843049c2002-09-22 15:37:40 -04001217
Theodore Ts'o756df352002-03-07 20:52:12 -05001218#ifdef __linux__
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001219 if (uname(&ut)) {
1220 perror("uname");
1221 exit(1);
1222 }
Theodore Ts'od99225e2004-09-25 07:40:12 -04001223 linux_version_code = parse_version_number(ut.release);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001224 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001225 fs_param.s_rev_level = 0;
Theodore Ts'o14bbcbc2001-05-13 22:58:27 +00001226#endif
Andreas Dilger0072f8d2002-02-25 23:11:26 -07001227
1228 if (argc && *argv) {
1229 program_name = get_progname(*argv);
1230
1231 /* If called as mkfs.ext3, create a journal inode */
1232 if (!strcmp(program_name, "mkfs.ext3"))
1233 journal_size = -1;
1234 }
1235
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001236 while ((c = getopt (argc, argv,
Theodore Ts'o3d438362008-02-19 08:32:58 -05001237 "b:cf:g:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001238 switch (c) {
1239 case 'b':
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001240 blocksize = strtol(optarg, &tmp, 0);
1241 b = (blocksize > 0) ? blocksize : -blocksize;
1242 if (b < EXT2_MIN_BLOCK_SIZE ||
1243 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001244 com_err(program_name, 0,
Theodore Ts'of37ab682005-05-05 23:15:55 -04001245 _("invalid block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001246 exit(1);
1247 }
Andreas Dilger932a4892002-05-16 03:20:07 -06001248 if (blocksize > 4096)
1249 fprintf(stderr, _("Warning: blocksize %d not "
1250 "usable on most systems.\n"),
1251 blocksize);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001252 if (blocksize > 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001253 fs_param.s_log_block_size =
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001254 int_log2(blocksize >>
1255 EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001256 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001257 case 'c': /* Check for bad blocks */
Theodore Ts'o3ed57c22001-12-24 15:01:59 -05001258 cflag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001259 break;
1260 case 'f':
1261 size = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001262 if (size < EXT2_MIN_BLOCK_SIZE ||
1263 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001264 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001265 _("invalid fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001266 optarg);
1267 exit(1);
1268 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001269 fs_param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +00001270 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o66938372002-03-08 00:14:46 -05001271 fprintf(stderr, _("Warning: fragments not supported. "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001272 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001273 break;
1274 case 'g':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001275 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001276 if (*tmp) {
1277 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001278 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001279 exit(1);
1280 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001281 if ((fs_param.s_blocks_per_group % 8) != 0) {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001282 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001283 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001284 exit(1);
1285 }
1286 break;
1287 case 'i':
1288 inode_ratio = strtoul(optarg, &tmp, 0);
Andreas Dilger932a4892002-05-16 03:20:07 -06001289 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1290 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001292 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001293 _("invalid inode ratio %s (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001294 optarg, EXT2_MIN_BLOCK_SIZE,
1295 EXT2_MAX_BLOCK_SIZE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001296 exit(1);
1297 }
1298 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001299 case 'J':
1300 parse_journal_opts(optarg);
1301 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001302 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001303 if (!journal_size)
1304 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001305 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001306 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +00001307 bad_blocks_filename = malloc(strlen(optarg)+1);
1308 if (!bad_blocks_filename) {
1309 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001310 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +00001311 exit(1);
1312 }
1313 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001314 break;
1315 case 'm':
Andreas Dilgerce911142005-07-06 11:50:08 -05001316 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001317 if (reserved_ratio > 50 || *tmp) {
1318 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001319 _("invalid reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001320 optarg);
1321 exit(1);
1322 }
1323 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001324 case 'n':
1325 noaction++;
1326 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001327 case 'o':
1328 creator_os = optarg;
1329 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001330 case 'q':
1331 quiet = 1;
1332 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001333 case 'r':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001334 r_opt = strtoul(optarg, &tmp, 0);
Andreas Dilger25247852005-07-06 12:58:15 -05001335 if (*tmp) {
1336 com_err(program_name, 0,
1337 _("bad revision level - %s"), optarg);
1338 exit(1);
1339 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001340 fs_param.s_rev_level = r_opt;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001341 break;
Theodore Ts'ob10fd5e2001-04-22 03:47:23 +00001342 case 's': /* deprecated */
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001343 s_opt = atoi(optarg);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001344 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001345 case 'I':
Andreas Dilger932a4892002-05-16 03:20:07 -06001346 inode_size = strtoul(optarg, &tmp, 0);
1347 if (*tmp) {
1348 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001349 _("invalid inode size - %s"), optarg);
Andreas Dilger932a4892002-05-16 03:20:07 -06001350 exit(1);
1351 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +00001352 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001353 case 'v':
1354 verbose = 1;
1355 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001356 case 'F':
Andreas Dilgerc16e6102006-08-05 19:05:53 -04001357 force++;
Theodore Ts'o74becf31997-04-26 14:37:06 +00001358 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001359 case 'L':
1360 volume_label = optarg;
1361 break;
1362 case 'M':
1363 mount_dir = optarg;
1364 break;
Andreas Dilger25247852005-07-06 12:58:15 -05001365 case 'N':
1366 num_inodes = strtoul(optarg, &tmp, 0);
1367 if (*tmp) {
1368 com_err(program_name, 0,
1369 _("bad num inodes - %s"), optarg);
1370 exit(1);
1371 }
1372 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001373 case 'O':
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001374 fs_features = optarg;
Theodore Ts'o896938d1999-10-23 01:04:50 +00001375 break;
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001376 case 'E':
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001377 case 'R':
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001378 extended_opts = optarg;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001379 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001380 case 'S':
1381 super_only = 1;
1382 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001383 case 't':
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001384 fs_type = optarg;
1385 break;
Theodore Ts'o3d438362008-02-19 08:32:58 -05001386 case 'T':
1387 usage_types = optarg;
1388 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001389 case 'V':
1390 /* Print version number and exit */
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001391 show_version_only++;
1392 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001393 default:
1394 usage();
1395 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001396 }
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001397 if ((optind == argc) && !show_version_only)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001398 usage();
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001399 device_name = argv[optind++];
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001400
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001401 if (!quiet || show_version_only)
Theodore Ts'o38798572003-04-16 15:29:39 -04001402 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1403 E2FSPROGS_DATE);
1404
Theodore Ts'o1e6e4c52003-12-07 02:28:24 -05001405 if (show_version_only) {
1406 fprintf(stderr, _("\tUsing %s\n"),
1407 error_message(EXT2_ET_BASE));
1408 exit(0);
1409 }
1410
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001411 /*
1412 * If there's no blocksize specified and there is a journal
1413 * device, use it to figure out the blocksize
1414 */
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001415 if (blocksize <= 0 && journal_device) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001416 ext2_filsys jfs;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001417 io_manager io_ptr;
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001418
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001419#ifdef CONFIG_TESTIO_DEBUG
1420 io_ptr = test_io_manager;
1421 test_io_backing_manager = unix_io_manager;
1422#else
1423 io_ptr = unix_io_manager;
1424#endif
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001425 retval = ext2fs_open(journal_device,
1426 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001427 0, io_ptr, &jfs);
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001428 if (retval) {
1429 com_err(program_name, retval,
1430 _("while trying to open journal device %s\n"),
1431 journal_device);
1432 exit(1);
1433 }
Theodore Ts'o54434922003-12-07 01:28:50 -05001434 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001435 com_err(program_name, 0,
Theodore Ts'oddc32a02003-05-03 18:45:55 -04001436 _("Journal dev blocksize (%d) smaller than "
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001437 "minimum blocksize %d\n"), jfs->blocksize,
1438 -blocksize);
1439 exit(1);
1440 }
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001441 blocksize = jfs->blocksize;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001442 fs_param.s_log_block_size =
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001443 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1444 ext2fs_close(jfs);
1445 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001446
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001447 if (blocksize > sys_page_size) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001448 if (!force) {
1449 com_err(program_name, 0,
1450 _("%d-byte blocks too big for system (max %d)"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001451 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001452 proceed_question();
1453 }
1454 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1455 "(max %d), forced to continue\n"),
Theodore Ts'o31e29a12002-05-17 10:53:07 -04001456 blocksize, sys_page_size);
Andreas Dilger932a4892002-05-16 03:20:07 -06001457 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001458 if (optind < argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001459 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1460 fs_param.s_log_block_size);
1461 if (!fs_param.s_blocks_count) {
Theodore Ts'of37ab682005-05-05 23:15:55 -04001462 com_err(program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -05001463 argv[optind - 1]);
1464 exit(1);
1465 }
1466 }
1467 if (optind < argc)
1468 usage();
1469
Theodore Ts'o74becf31997-04-26 14:37:06 +00001470 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001471 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +00001472 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001473
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001474 fs_param.s_log_frag_size = fs_param.s_log_block_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001475
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001476 if (noaction && fs_param.s_blocks_count) {
1477 dev_size = fs_param.s_blocks_count;
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001478 retval = 0;
Theodore Ts'o20953122005-01-27 19:07:26 -05001479 } else {
1480 retry:
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001481 retval = ext2fs_get_device_size(device_name,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001482 EXT2_BLOCK_SIZE(&fs_param),
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001483 &dev_size);
Theodore Ts'o20953122005-01-27 19:07:26 -05001484 if ((retval == EFBIG) &&
1485 (blocksize == 0) &&
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001486 (fs_param.s_log_block_size == 0)) {
1487 fs_param.s_log_block_size = 2;
Theodore Ts'o20953122005-01-27 19:07:26 -05001488 blocksize = 4096;
1489 goto retry;
1490 }
1491 }
1492
Theodore Ts'oa789d841998-03-30 01:20:55 +00001493 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1494 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001495 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001496 exit(1);
1497 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001498 if (!fs_param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001499 if (retval == EXT2_ET_UNIMPLEMENTED) {
1500 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001501 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +00001502 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001503 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001504 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001505 } else {
1506 if (dev_size == 0) {
1507 com_err(program_name, 0,
1508 _("Device size reported to be zero. "
1509 "Invalid partition specified, or\n\t"
1510 "partition table wasn't reread "
1511 "after running fdisk, due to\n\t"
1512 "a modified partition being busy "
1513 "and in use. You may need to reboot\n\t"
1514 "to re-read your partition table.\n"
1515 ));
1516 exit(1);
1517 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001518 fs_param.s_blocks_count = dev_size;
1519 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1520 fs_param.s_blocks_count &= ~((sys_page_size /
1521 EXT2_BLOCK_SIZE(&fs_param))-1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001522 }
1523
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001524 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001525 com_err(program_name, 0,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001526 _("Filesystem larger than apparent device size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001527 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001528 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001529
Theodore Ts'o3d438362008-02-19 08:32:58 -05001530 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1531 if (!fs_types) {
1532 fprintf(stderr, _("Failed to parse fs types list\n"));
1533 exit(1);
1534 }
1535 if (verbose) {
1536 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1537 print_str_list(fs_types);
1538 }
1539
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001540 if (!fs_type) {
Eric Sandeend1b4b852006-09-12 14:56:18 -04001541 int megs = (__u64)fs_param.s_blocks_count *
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001542 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1543
Brian Behlendorfe0661502007-03-19 08:25:38 -04001544 if (fs_param.s_feature_incompat &
1545 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1546 fs_type = "journal";
1547 else if (megs <= 3)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001548 fs_type = "floppy";
1549 else if (megs <= 512)
1550 fs_type = "small";
1551 else
1552 fs_type = "default";
1553 }
1554
1555 /* Figure out what features should be enabled */
1556
Theodore Ts'o3d438362008-02-19 08:32:58 -05001557 tmp = NULL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001558 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001559 char **cpp;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001560
Theodore Ts'o3d438362008-02-19 08:32:58 -05001561 tmp = get_string_from_profile(fs_types, "base_features",
1562 "sparse_super,filetype,resize_inode,dir_index");
1563 edit_feature(tmp, &fs_param.s_feature_compat);
1564 free(tmp);
1565
1566 for (cpp = fs_types; *cpp; cpp++) {
1567 tmp = NULL;
1568 profile_get_string(profile, "fs_types", *cpp,
1569 "features", "", &tmp);
1570 if (tmp && *tmp)
1571 edit_feature(tmp, &fs_param.s_feature_compat);
1572 if (tmp)
1573 free(tmp);
1574 }
1575 tmp = get_string_from_profile(fs_types, "default_features",
1576 "");
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001577 }
Theodore Ts'o3d438362008-02-19 08:32:58 -05001578 edit_feature(fs_features ? fs_features : tmp,
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001579 &fs_param.s_feature_compat);
1580 if (tmp)
1581 free(tmp);
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001582
1583 if (r_opt == EXT2_GOOD_OLD_REV &&
1584 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1585 fs_param.s_feature_incompat)) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001586 fprintf(stderr, _("Filesystem features not supported "
1587 "with revision 0 filesystems\n"));
1588 exit(1);
1589 }
1590
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001591 if (s_opt > 0) {
1592 if (r_opt == EXT2_GOOD_OLD_REV) {
1593 fprintf(stderr, _("Sparse superblocks not supported "
1594 "with revision 0 filesystems\n"));
1595 exit(1);
1596 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001597 fs_param.s_feature_ro_compat |=
1598 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001599 } else if (s_opt == 0)
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001600 fs_param.s_feature_ro_compat &=
1601 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1602
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001603 if (journal_size != 0) {
1604 if (r_opt == EXT2_GOOD_OLD_REV) {
1605 fprintf(stderr, _("Journals not supported "
1606 "with revision 0 filesystems\n"));
1607 exit(1);
1608 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001609 fs_param.s_feature_compat |=
1610 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'ob290d2d2006-10-18 00:31:11 -04001611 }
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001612
1613 if (fs_param.s_feature_incompat &
1614 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001615 reserved_ratio = 0;
1616 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1617 fs_param.s_feature_compat = 0;
1618 fs_param.s_feature_ro_compat = 0;
1619 }
1620
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001621 /* Set first meta blockgroup via an environment variable */
1622 /* (this is mostly for debugging purposes) */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001623 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001624 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001625 fs_param.s_first_meta_bg = atoi(tmp);
Theodore Ts'oc046ac72002-10-20 00:38:57 -04001626
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001627 /* Get the hardware sector size, if available */
1628 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1629 if (retval) {
1630 com_err(program_name, retval,
1631 _("while trying to determine hardware sector size"));
1632 exit(1);
1633 }
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001634
Theodore Ts'o54434922003-12-07 01:28:50 -05001635 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04001636 sector_size = atoi(tmp);
Theodore Ts'o93d5c382003-05-21 17:28:29 -04001637
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001638 if (blocksize <= 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001639 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001640
1641 if (use_bsize == -1) {
1642 use_bsize = sys_page_size;
1643 if ((linux_version_code < (2*65536 + 6*256)) &&
1644 (use_bsize > 4096))
1645 use_bsize = 4096;
1646 }
1647 if (sector_size && use_bsize < sector_size)
1648 use_bsize = sector_size;
1649 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1650 use_bsize = -blocksize;
1651 blocksize = use_bsize;
1652 fs_param.s_blocks_count /= blocksize / 1024;
1653 }
1654
1655 if (inode_ratio == 0) {
Theodore Ts'o3d438362008-02-19 08:32:58 -05001656 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1657 8192);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001658 if (inode_ratio < blocksize)
1659 inode_ratio = blocksize;
1660 }
1661
1662 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1663 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1664
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001665 blocksize = EXT2_BLOCK_SIZE(&fs_param);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001666
1667 lazy_itable_init = get_bool_from_profile(fs_types,
1668 "lazy_itable_init", 0);
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001669
Theodore Ts'oc6a44132005-01-05 11:12:20 -05001670 if (extended_opts)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001671 parse_extended_opts(&fs_param, extended_opts);
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001672
1673 /* Since sparse_super is the default, we would only have a problem
1674 * here if it was explicitly disabled.
1675 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001676 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1677 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001678 com_err(program_name, 0,
1679 _("reserved online resize blocks not supported "
1680 "on non-sparse filesystem"));
1681 exit(1);
1682 }
1683
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001684 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1685 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
1686 EXT2_HAS_COMPAT_FEATURE(&fs_param, EXT2_FEATURE_COMPAT_LAZY_BG)) {
1687 com_err(program_name, 0,
1688 _("lazy_bg and uninit_bg can not be enabled "
1689 "at the same time;\n\t"
1690 "use -E lazy_itable_init=1 to defer zeroing the "
1691 "inode table."));
1692 exit(1);
1693 }
1694
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001695 if (fs_param.s_blocks_per_group) {
1696 if (fs_param.s_blocks_per_group < 256 ||
1697 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001698 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001699 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001700 exit(1);
1701 }
1702 }
1703
Theodore Ts'o3d438362008-02-19 08:32:58 -05001704 if (inode_size == 0)
1705 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
Andreas Dilger067911a2006-07-15 22:08:20 -04001706
1707 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
Andreas Dilger932a4892002-05-16 03:20:07 -06001708 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001709 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
Andreas Dilger932a4892002-05-16 03:20:07 -06001710 inode_size & (inode_size - 1)) {
1711 com_err(program_name, 0,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001712 _("invalid inode size %d (min %d/max %d)"),
Andreas Dilger932a4892002-05-16 03:20:07 -06001713 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
Theodore Ts'oc5290fa2003-04-11 22:10:50 -04001714 blocksize);
Andreas Dilger932a4892002-05-16 03:20:07 -06001715 exit(1);
1716 }
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001717 fs_param.s_inode_size = inode_size;
Andreas Dilger932a4892002-05-16 03:20:07 -06001718 }
1719
Eric Sandeenf3358642006-09-12 14:56:17 -04001720 /* Make sure number of inodes specified will fit in 32 bits */
1721 if (num_inodes == 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001722 unsigned long long n;
1723 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
Eric Sandeenf3358642006-09-12 14:56:17 -04001724 if (n > ~0U) {
1725 com_err(program_name, 0,
1726 _("too many inodes (%llu), raise inode ratio?"), n);
1727 exit(1);
1728 }
1729 } else if (num_inodes > ~0U) {
1730 com_err(program_name, 0,
1731 _("too many inodes (%llu), specify < 2^32 inodes"),
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001732 num_inodes);
Eric Sandeenf3358642006-09-12 14:56:17 -04001733 exit(1);
1734 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001735 /*
1736 * Calculate number of inodes based on the inode ratio
1737 */
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001738 fs_param.s_inodes_count = num_inodes ? num_inodes :
1739 ((__u64) fs_param.s_blocks_count * blocksize)
Theodore Ts'of3db3561997-04-26 13:34:30 +00001740 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001741
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001742 if ((((long long)fs_param.s_inodes_count) *
1743 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1744 (((long long)fs_param.s_blocks_count) *
1745 EXT2_BLOCK_SIZE(&fs_param))) {
1746 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1747 "(%u) too big for a\n\t"
1748 "filesystem with %lu blocks, "
1749 "specify higher inode_ratio (-i)\n\t"
1750 "or lower inode count (-N).\n"),
1751 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -04001752 fs_param.s_inodes_count,
1753 (unsigned long) fs_param.s_blocks_count);
Andreas Dilgerdcf7b092007-05-22 16:04:51 -04001754 exit(1);
1755 }
1756
Theodore Ts'o3839e651997-04-26 13:21:57 +00001757 /*
1758 * Calculate number of blocks to reserve
1759 */
Theodore Ts'oa8862d92006-08-30 03:08:13 -04001760 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1761 fs_param.s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001762}
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001763
Theodore Ts'o3839e651997-04-26 13:21:57 +00001764int main (int argc, char *argv[])
1765{
1766 errcode_t retval = 0;
1767 ext2_filsys fs;
1768 badblocks_list bb_list = 0;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001769 unsigned int journal_blocks;
Theodore Ts'o54434922003-12-07 01:28:50 -05001770 unsigned int i;
1771 int val;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001772 io_manager io_ptr;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001773
1774#ifdef ENABLE_NLS
1775 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001776 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001777 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1778 textdomain(NLS_CAT_NAME);
1779#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001780 PRS(argc, argv);
1781
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001782#ifdef CONFIG_TESTIO_DEBUG
1783 io_ptr = test_io_manager;
1784 test_io_backing_manager = unix_io_manager;
1785#else
1786 io_ptr = unix_io_manager;
1787#endif
1788
Theodore Ts'o3839e651997-04-26 13:21:57 +00001789 /*
1790 * Initialize the superblock....
1791 */
Theodore Ts'o616059b2006-03-18 20:02:05 -05001792 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -04001793 io_ptr, &fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001794 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001795 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001796 exit(1);
1797 }
1798
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001799 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1800 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1801
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001802 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001803 * Wipe out the old on-disk superblock
1804 */
Theodore Ts'o04a96852001-08-30 21:55:26 -04001805 if (!noaction)
1806 zap_sector(fs, 2, 6);
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001807
1808 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001809 * Generate a UUID for it...
1810 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001811 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001812
1813 /*
Theodore Ts'o843049c2002-09-22 15:37:40 -04001814 * Initialize the directory index variables
1815 */
1816 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1817 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1818
1819 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001820 * Add "jitter" to the superblock's check interval so that we
1821 * don't check all the filesystems at the same time. We use a
1822 * kludgy hack of using the UUID to derive a random jitter value.
1823 */
1824 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1825 val += fs->super->s_uuid[i];
1826 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1827
1828 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001829 * Override the creator OS, if applicable
1830 */
1831 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001832 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001833 exit(1);
1834 }
1835
1836 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001837 * For the Hurd, we will turn off filetype since it doesn't
1838 * support it.
1839 */
1840 if (fs->super->s_creator_os == EXT2_OS_HURD)
1841 fs->super->s_feature_incompat &=
1842 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1843
1844 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001845 * Set the volume label...
1846 */
1847 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001848 memset(fs->super->s_volume_name, 0,
1849 sizeof(fs->super->s_volume_name));
1850 strncpy(fs->super->s_volume_name, volume_label,
1851 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001852 }
1853
1854 /*
1855 * Set the last mount directory
1856 */
1857 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001858 memset(fs->super->s_last_mounted, 0,
1859 sizeof(fs->super->s_last_mounted));
1860 strncpy(fs->super->s_last_mounted, mount_dir,
1861 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001862 }
1863
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001864 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001865 show_stats(fs);
1866
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001867 if (noaction)
1868 exit(0);
1869
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001870 if (fs->super->s_feature_incompat &
1871 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1872 create_journal_dev(fs);
Andreas Dilger568101f2001-10-13 01:22:25 -06001873 exit(ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001874 }
1875
Theodore Ts'o3839e651997-04-26 13:21:57 +00001876 if (bad_blocks_filename)
1877 read_bb_file(fs, &bb_list, bad_blocks_filename);
1878 if (cflag)
1879 test_disk(fs, &bb_list);
1880
1881 handle_bad_blocks(fs, bb_list);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001882 fs->stride = fs_stride = fs->super->s_raid_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001883 retval = ext2fs_allocate_tables(fs);
1884 if (retval) {
1885 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001886 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001887 exit(1);
1888 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001889 if (super_only) {
1890 fs->super->s_state |= EXT2_ERROR_FS;
1891 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1892 } else {
Andreas Dilger59f27242001-08-30 15:39:04 -06001893 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
Theodore Ts'o54434922003-12-07 01:28:50 -05001894 unsigned int rsv = 65536 / fs->blocksize;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001895 unsigned long blocks = fs->super->s_blocks_count;
Andreas Dilger59f27242001-08-30 15:39:04 -06001896 unsigned long start;
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001897 blk_t ret_blk;
1898
1899#ifdef ZAP_BOOTBLOCK
Theodore Ts'o04a96852001-08-30 21:55:26 -04001900 zap_sector(fs, 0, 2);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001901#endif
Andreas Dilger59f27242001-08-30 15:39:04 -06001902
1903 /*
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001904 * Wipe out any old MD RAID (or other) metadata at the end
1905 * of the device. This will also verify that the device is
Andreas Dilger59f27242001-08-30 15:39:04 -06001906 * as large as we think. Be careful with very small devices.
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001907 */
Andreas Dilger59f27242001-08-30 15:39:04 -06001908 start = (blocks & ~(rsv - 1));
1909 if (start > rsv)
1910 start -= rsv;
1911 if (start > 0)
1912 retval = zero_blocks(fs, start, blocks - start,
1913 NULL, &ret_blk, NULL);
1914
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001915 if (retval) {
1916 com_err(program_name, retval,
Theodore Ts'of044b4d2002-08-17 13:32:21 -04001917 _("while zeroing block %u at end of filesystem"),
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001918 ret_blk);
Theodore Ts'o1400bbb2001-05-14 04:19:25 +00001919 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -04001920 setup_lazy_bg(fs);
Theodore Ts'oa4396e92008-04-18 10:19:27 -04001921 write_inode_tables(fs, lazy_itable_init);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001922 create_root_dir(fs);
1923 create_lost_and_found(fs);
1924 reserve_inodes(fs);
1925 create_bad_block_inode(fs, bb_list);
Theodore Ts'oea774312005-01-28 11:45:28 -05001926 if (fs->super->s_feature_compat &
1927 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1928 retval = ext2fs_create_resize_inode(fs);
1929 if (retval) {
1930 com_err("ext2fs_create_resize_inode", retval,
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001931 _("while reserving blocks for online resize"));
Theodore Ts'oea774312005-01-28 11:45:28 -05001932 exit(1);
1933 }
Theodore Ts'od323f8f2004-12-15 14:39:16 -05001934 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001935 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001936
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001937 if (journal_device) {
1938 ext2_filsys jfs;
1939
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001940 if (!force)
1941 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001942 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001943
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001944 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1945 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1946 fs->blocksize, unix_io_manager, &jfs);
1947 if (retval) {
1948 com_err(program_name, retval,
1949 _("while trying to open journal device %s\n"),
1950 journal_device);
1951 exit(1);
1952 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001953 if (!quiet) {
Theodore Ts'o77dc4eb2001-07-27 22:00:18 -04001954 printf(_("Adding journal to device %s: "),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001955 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001956 fflush(stdout);
1957 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001958 retval = ext2fs_add_journal_device(fs, jfs);
1959 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001960 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001961 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001962 journal_device);
1963 exit(1);
1964 }
1965 if (!quiet)
1966 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001967 ext2fs_close(jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -06001968 free(journal_device);
Theodore Ts'o9dc6ad12006-03-23 22:00:01 -05001969 } else if ((journal_size) ||
1970 (fs_param.s_feature_compat &
1971 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001972 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001973
1974 if (!journal_blocks) {
1975 fs->super->s_feature_compat &=
1976 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1977 goto no_journal;
1978 }
1979 if (!quiet) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001980 printf(_("Creating journal (%u blocks): "),
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001981 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001982 fflush(stdout);
1983 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001984 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1985 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001986 if (retval) {
1987 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001988 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001989 exit(1);
1990 }
1991 if (!quiet)
1992 printf(_("done\n"));
1993 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001994no_journal:
1995
Andreas Dilgerf628ace2008-03-31 10:50:19 -04001996 if (!super_only) {
1997 retval = ext2fs_set_gdt_csum(fs);
1998 if (retval) {
1999 com_err(program_name, retval,
2000 _("\n\twhile setting block group checksum info"));
2001 exit(1);
2002 }
2003 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00002004 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002005 printf(_("Writing superblocks and "
2006 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002007 retval = ext2fs_flush(fs);
2008 if (retval) {
Theodore Ts'o66938372002-03-08 00:14:46 -05002009 fprintf(stderr,
2010 _("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00002011 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00002012 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00002013 printf(_("done\n\n"));
Theodore Ts'o1cca86f2003-09-01 09:28:18 -04002014 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2015 print_check_message(fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00002016 }
Andreas Dilger568101f2001-10-13 01:22:25 -06002017 val = ext2fs_close(fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002018 remove_error_table(&et_ext2_error_table);
2019 remove_error_table(&et_prof_error_table);
Theodore Ts'o3d438362008-02-19 08:32:58 -05002020 profile_release(profile);
Andreas Dilger568101f2001-10-13 01:22:25 -06002021 return (retval || val) ? 1 : 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002022}