Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mke2fs.c - Make a ext2fs filesystem. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 4 | * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
| 5 | * 2003, 2004, 2005 by Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the GNU Public |
| 9 | * License. |
| 10 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* Usage: mke2fs [options] device |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 14 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | * The device may be a block device or a image of one, but this isn't |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 16 | * enforced (but it's not much fun on a character device :-). |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Theodore Ts'o | ebabf2a | 2008-07-13 15:32:37 -0400 | [diff] [blame] | 19 | #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */ |
| 20 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 21 | #include "config.h" |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | #include <string.h> |
Theodore Ts'o | d0c5377 | 2009-07-18 18:41:51 -0400 | [diff] [blame] | 24 | #include <strings.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <ctype.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | #include <time.h> |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 28 | #ifdef __linux__ |
Theodore Ts'o | 2740156 | 1999-09-14 20:11:19 +0000 | [diff] [blame] | 29 | #include <sys/utsname.h> |
| 30 | #endif |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 31 | #ifdef HAVE_GETOPT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 32 | #include <getopt.h> |
Theodore Ts'o | 373b833 | 2000-04-03 16:22:35 +0000 | [diff] [blame] | 33 | #else |
| 34 | extern char *optarg; |
| 35 | extern int optind; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 36 | #endif |
| 37 | #ifdef HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 39 | #endif |
| 40 | #ifdef HAVE_STDLIB_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 42 | #endif |
| 43 | #ifdef HAVE_ERRNO_H |
| 44 | #include <errno.h> |
| 45 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 46 | #include <sys/ioctl.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 47 | #include <sys/types.h> |
Eric Sandeen | 13b0b12 | 2010-01-23 21:50:45 -0600 | [diff] [blame] | 48 | #include <sys/stat.h> |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 49 | #include <libgen.h> |
Theodore Ts'o | 3658579 | 2008-06-07 22:07:50 -0400 | [diff] [blame] | 50 | #include <limits.h> |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 51 | #include <blkid/blkid.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 52 | |
Theodore Ts'o | 54c637d | 2001-05-14 11:45:38 +0000 | [diff] [blame] | 53 | #include "ext2fs/ext2_fs.h" |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 54 | #include "ext2fs/ext2fsP.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 55 | #include "et/com_err.h" |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 56 | #include "uuid/uuid.h" |
Theodore Ts'o | 896938d | 1999-10-23 01:04:50 +0000 | [diff] [blame] | 57 | #include "e2p/e2p.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 58 | #include "ext2fs/ext2fs.h" |
Theodore Ts'o | 6398532 | 2001-01-03 17:02:13 +0000 | [diff] [blame] | 59 | #include "util.h" |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 60 | #include "profile.h" |
Theodore Ts'o | a6d8302 | 2006-12-26 03:38:07 -0500 | [diff] [blame] | 61 | #include "prof_err.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | #include "../version.h" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 63 | #include "nls-enable.h" |
Aditya Kali | 1f5d7a8 | 2011-07-20 11:40:04 -0700 | [diff] [blame] | 64 | #include "quota/mkquota.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | |
| 66 | #define STRIDE_LENGTH 8 |
| 67 | |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 68 | #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1) |
| 69 | |
Theodore Ts'o | 6733c2f | 1999-11-23 02:23:30 +0000 | [diff] [blame] | 70 | #ifndef __sparc__ |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 71 | #define ZAP_BOOTBLOCK |
| 72 | #endif |
| 73 | |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 74 | #define DISCARD_STEP_MB (2048) |
| 75 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 76 | extern int isatty(int); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 77 | extern FILE *fpopen(const char *cmd, const char *mode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 78 | |
| 79 | const char * program_name = "mke2fs"; |
Theodore Ts'o | d48755e | 2000-12-09 14:36:04 +0000 | [diff] [blame] | 80 | const char * device_name /* = NULL */; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 81 | |
| 82 | /* Command line options */ |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 83 | int cflag; |
| 84 | int verbose; |
| 85 | int quiet; |
| 86 | int super_only; |
Lukas Czerner | 7fe5ff3 | 2010-11-18 14:38:41 +0100 | [diff] [blame] | 87 | int discard = 1; /* attempt to discard device before fs creation */ |
Theodore Ts'o | 37c8db7 | 2012-03-22 16:00:49 -0400 | [diff] [blame] | 88 | int direct_io; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 89 | int force; |
| 90 | int noaction; |
| 91 | int journal_size; |
| 92 | int journal_flags; |
Lukas Czerner | 7fe5ff3 | 2010-11-18 14:38:41 +0100 | [diff] [blame] | 93 | int lazy_itable_init; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 94 | char *bad_blocks_filename; |
| 95 | __u32 fs_stride; |
Aditya Kali | d678fef | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 96 | int quotatype = -1; /* Initialize both user and group quotas by default */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 97 | |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 98 | struct ext2_super_block fs_param; |
Theodore Ts'o | b0afdda | 2009-01-20 13:18:23 -0500 | [diff] [blame] | 99 | char *fs_uuid = NULL; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 100 | char *creator_os; |
| 101 | char *volume_label; |
| 102 | char *mount_dir; |
| 103 | char *journal_device; |
| 104 | int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 105 | char **fs_types; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 106 | |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 107 | profile_t profile; |
| 108 | |
Theodore Ts'o | 31e29a1 | 2002-05-17 10:53:07 -0400 | [diff] [blame] | 109 | int sys_page_size = 4096; |
Theodore Ts'o | d99225e | 2004-09-25 07:40:12 -0400 | [diff] [blame] | 110 | int linux_version_code = 0; |
Theodore Ts'o | 31e29a1 | 2002-05-17 10:53:07 -0400 | [diff] [blame] | 111 | |
Theodore Ts'o | 6398532 | 2001-01-03 17:02:13 +0000 | [diff] [blame] | 112 | static void usage(void) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 113 | { |
Theodore Ts'o | bdd80f2 | 2008-02-28 21:12:31 -0500 | [diff] [blame] | 114 | fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] " |
Eric Sandeen | 28e2cb9 | 2011-10-04 17:12:11 -0500 | [diff] [blame] | 115 | "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] " |
Theodore Ts'o | bdd80f2 | 2008-02-28 21:12:31 -0500 | [diff] [blame] | 116 | "[-J journal-options]\n" |
Yongqiang Yang | 8dbcbe1 | 2012-01-23 11:46:43 -0500 | [diff] [blame] | 117 | "\t[-G flex-group-size] [-N number-of-inodes]\n" |
Theodore Ts'o | 9ba4000 | 2008-04-22 08:27:01 -0400 | [diff] [blame] | 118 | "\t[-m reserved-blocks-percentage] [-o creator-os]\n" |
| 119 | "\t[-g blocks-per-group] [-L volume-label] " |
Andreas Dilger | 067911a | 2006-07-15 22:08:20 -0400 | [diff] [blame] | 120 | "[-M last-mounted-directory]\n\t[-O feature[,...]] " |
Theodore Ts'o | bdd80f2 | 2008-02-28 21:12:31 -0500 | [diff] [blame] | 121 | "[-r fs-revision] [-E extended-option[,...]]\n" |
Mike Frysinger | 65794cf | 2012-01-09 21:17:57 -0500 | [diff] [blame] | 122 | "\t[-t fs-type] [-T usage-type ] [-U UUID] " |
Theodore Ts'o | 37c8db7 | 2012-03-22 16:00:49 -0400 | [diff] [blame] | 123 | "[-jnqvDFKSV] device [blocks-count]\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 124 | program_name); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 128 | static int int_log2(unsigned long long arg) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 129 | { |
| 130 | int l = 0; |
| 131 | |
| 132 | arg >>= 1; |
| 133 | while (arg) { |
| 134 | l++; |
| 135 | arg >>= 1; |
| 136 | } |
| 137 | return l; |
| 138 | } |
| 139 | |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 140 | static int int_log10(unsigned long long arg) |
Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 141 | { |
| 142 | int l; |
| 143 | |
| 144 | for (l=0; arg ; l++) |
| 145 | arg = arg / 10; |
| 146 | return l; |
| 147 | } |
| 148 | |
Theodore Ts'o | d99225e | 2004-09-25 07:40:12 -0400 | [diff] [blame] | 149 | static int parse_version_number(const char *s) |
| 150 | { |
| 151 | int major, minor, rev; |
| 152 | char *endptr; |
| 153 | const char *cp = s; |
| 154 | |
| 155 | if (!s) |
| 156 | return 0; |
| 157 | major = strtol(cp, &endptr, 10); |
| 158 | if (cp == endptr || *endptr != '.') |
| 159 | return 0; |
| 160 | cp = endptr + 1; |
| 161 | minor = strtol(cp, &endptr, 10); |
| 162 | if (cp == endptr || *endptr != '.') |
| 163 | return 0; |
| 164 | cp = endptr + 1; |
| 165 | rev = strtol(cp, &endptr, 10); |
| 166 | if (cp == endptr) |
| 167 | return 0; |
| 168 | return ((((major * 256) + minor) * 256) + rev); |
| 169 | } |
| 170 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 171 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | * Helper function for read_bb_file and test_disk |
| 173 | */ |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 174 | static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 175 | { |
Theodore Ts'o | 6693837 | 2002-03-08 00:14:46 -0500 | [diff] [blame] | 176 | fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Reads the bad blocks list from a file |
| 182 | */ |
| 183 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, |
| 184 | const char *bad_blocks_file) |
| 185 | { |
| 186 | FILE *f; |
| 187 | errcode_t retval; |
| 188 | |
| 189 | f = fopen(bad_blocks_file, "r"); |
| 190 | if (!f) { |
| 191 | com_err("read_bad_blocks_file", errno, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 192 | _("while trying to open %s"), bad_blocks_file); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 193 | exit(1); |
| 194 | } |
| 195 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
| 196 | fclose (f); |
| 197 | if (retval) { |
| 198 | com_err("ext2fs_read_bb_FILE", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 199 | _("while reading in list of bad blocks from file")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 200 | exit(1); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Runs the badblocks program to test the disk |
| 206 | */ |
| 207 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) |
| 208 | { |
| 209 | FILE *f; |
| 210 | errcode_t retval; |
| 211 | char buf[1024]; |
| 212 | |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 213 | sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize, |
Theodore Ts'o | 3ed57c2 | 2001-12-24 15:01:59 -0500 | [diff] [blame] | 214 | quiet ? "" : "-s ", (cflag > 1) ? "-w " : "", |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 215 | fs->device_name, ext2fs_blocks_count(fs->super)-1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 216 | if (verbose) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 217 | printf(_("Running command: %s\n"), buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 218 | f = popen(buf, "r"); |
| 219 | if (!f) { |
| 220 | com_err("popen", errno, |
Theodore Ts'o | f37ab68 | 2005-05-05 23:15:55 -0400 | [diff] [blame] | 221 | _("while trying to run '%s'"), buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 222 | exit(1); |
| 223 | } |
| 224 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 225 | pclose(f); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 226 | if (retval) { |
| 227 | com_err("ext2fs_read_bb_FILE", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 228 | _("while processing list of bad blocks from program")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 229 | exit(1); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) |
| 234 | { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 235 | dgrp_t i; |
| 236 | blk_t j; |
| 237 | unsigned must_be_good; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 238 | blk_t blk; |
| 239 | badblocks_iterate bb_iter; |
| 240 | errcode_t retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 241 | blk_t group_block; |
| 242 | int group; |
| 243 | int group_bad; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 244 | |
| 245 | if (!bb_list) |
| 246 | return; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 247 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 248 | /* |
| 249 | * The primary superblock and group descriptors *must* be |
| 250 | * good; if not, abort. |
| 251 | */ |
| 252 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; |
| 253 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { |
Theodore Ts'o | cbbf031 | 2001-06-13 00:12:04 +0000 | [diff] [blame] | 254 | if (ext2fs_badblocks_list_test(bb_list, i)) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 255 | fprintf(stderr, _("Block %d in primary " |
| 256 | "superblock/group descriptor area bad.\n"), i); |
Eric Sandeen | d0ff90d | 2006-09-12 14:56:15 -0400 | [diff] [blame] | 257 | fprintf(stderr, _("Blocks %u through %u must be good " |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 258 | "in order to build a filesystem.\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 259 | fs->super->s_first_data_block, must_be_good); |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 260 | fputs(_("Aborting....\n"), stderr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 261 | exit(1); |
| 262 | } |
| 263 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * See if any of the bad blocks are showing up in the backup |
| 267 | * superblocks and/or group descriptors. If so, issue a |
| 268 | * warning and adjust the block counts appropriately. |
| 269 | */ |
| 270 | group_block = fs->super->s_first_data_block + |
| 271 | fs->super->s_blocks_per_group; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 272 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 273 | for (i = 1; i < fs->group_desc_count; i++) { |
Theodore Ts'o | 92bcc59 | 1998-02-16 22:29:34 +0000 | [diff] [blame] | 274 | group_bad = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 275 | for (j=0; j < fs->desc_blocks+1; j++) { |
Theodore Ts'o | cbbf031 | 2001-06-13 00:12:04 +0000 | [diff] [blame] | 276 | if (ext2fs_badblocks_list_test(bb_list, |
| 277 | group_block + j)) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 278 | if (!group_bad) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 279 | fprintf(stderr, |
Takashi Sato | 8deb80a | 2006-03-18 21:43:46 -0500 | [diff] [blame] | 280 | _("Warning: the backup superblock/group descriptors at block %u contain\n" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 281 | " bad blocks.\n\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 282 | group_block); |
| 283 | group_bad++; |
Theodore Ts'o | 6493f8e | 2009-10-25 20:50:15 -0400 | [diff] [blame] | 284 | group = ext2fs_group_of_blk2(fs, group_block+j); |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 285 | ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1); |
Theodore Ts'o | 5711ed2 | 2008-04-21 01:29:01 -0400 | [diff] [blame] | 286 | ext2fs_group_desc_csum_set(fs, group); |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 287 | ext2fs_free_blocks_count_add(fs->super, 1); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | group_block += fs->super->s_blocks_per_group; |
| 291 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 292 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 293 | /* |
| 294 | * Mark all the bad blocks as used... |
| 295 | */ |
Theodore Ts'o | cbbf031 | 2001-06-13 00:12:04 +0000 | [diff] [blame] | 296 | retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 297 | if (retval) { |
Theodore Ts'o | cbbf031 | 2001-06-13 00:12:04 +0000 | [diff] [blame] | 298 | com_err("ext2fs_badblocks_list_iterate_begin", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 299 | _("while marking bad blocks as used")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 300 | exit(1); |
| 301 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 302 | while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 303 | ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk)); |
Theodore Ts'o | cbbf031 | 2001-06-13 00:12:04 +0000 | [diff] [blame] | 304 | ext2fs_badblocks_list_iterate_end(bb_iter); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Eric Sandeen | 6fcd6f8 | 2010-08-20 16:41:14 -0500 | [diff] [blame] | 307 | static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed) |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 308 | { |
| 309 | errcode_t retval; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 310 | blk64_t blk; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 311 | dgrp_t i; |
Theodore Ts'o | e163242 | 2010-12-20 10:42:57 -0500 | [diff] [blame] | 312 | int num; |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 313 | struct ext2fs_numeric_progress_struct progress; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 314 | |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 315 | ext2fs_numeric_progress_init(fs, &progress, |
| 316 | _("Writing inode tables: "), |
| 317 | fs->group_desc_count); |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 318 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 319 | for (i = 0; i < fs->group_desc_count; i++) { |
Theodore Ts'o | c498cb1 | 2012-09-22 21:26:48 -0400 | [diff] [blame^] | 320 | ext2fs_numeric_progress_update(fs, &progress, i); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 321 | |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 322 | blk = ext2fs_inode_table_loc(fs, i); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 323 | num = fs->inode_blocks_per_group; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 324 | |
Theodore Ts'o | e163242 | 2010-12-20 10:42:57 -0500 | [diff] [blame] | 325 | if (lazy_flag) |
| 326 | num = ext2fs_div_ceil((fs->super->s_inodes_per_group - |
| 327 | ext2fs_bg_itable_unused(fs, i)) * |
| 328 | EXT2_INODE_SIZE(fs->super), |
| 329 | EXT2_BLOCK_SIZE(fs->super)); |
Eric Sandeen | 6fcd6f8 | 2010-08-20 16:41:14 -0500 | [diff] [blame] | 330 | if (!lazy_flag || itable_zeroed) { |
Jose R. Santos | d2d22a2 | 2007-10-21 21:03:36 -0500 | [diff] [blame] | 331 | /* The kernel doesn't need to zero the itable blocks */ |
Eric Sandeen | e633b58 | 2009-10-25 21:41:32 -0400 | [diff] [blame] | 332 | ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED); |
Theodore Ts'o | 5711ed2 | 2008-04-21 01:29:01 -0400 | [diff] [blame] | 333 | ext2fs_group_desc_csum_set(fs, i); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 334 | } |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 335 | retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num); |
Theodore Ts'o | 0f2794c | 2008-04-22 23:18:37 -0400 | [diff] [blame] | 336 | if (retval) { |
| 337 | fprintf(stderr, _("\nCould not write %d " |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 338 | "blocks in inode table starting at %llu: %s\n"), |
Theodore Ts'o | 0f2794c | 2008-04-22 23:18:37 -0400 | [diff] [blame] | 339 | num, blk, error_message(retval)); |
| 340 | exit(1); |
| 341 | } |
Theodore Ts'o | 7d5633c | 1999-02-09 08:14:28 +0000 | [diff] [blame] | 342 | if (sync_kludge) { |
| 343 | if (sync_kludge == 1) |
| 344 | sync(); |
| 345 | else if ((i % sync_kludge) == 0) |
| 346 | sync(); |
| 347 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 348 | } |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 349 | ext2fs_zero_blocks2(0, 0, 0, 0, 0); |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 350 | ext2fs_numeric_progress_close(fs, &progress, |
| 351 | _("done \n")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void create_root_dir(ext2_filsys fs) |
| 355 | { |
Eric Sandeen | 5113a6e | 2007-05-08 00:10:54 -0400 | [diff] [blame] | 356 | errcode_t retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 357 | struct ext2_inode inode; |
Eric Sandeen | 5113a6e | 2007-05-08 00:10:54 -0400 | [diff] [blame] | 358 | __u32 uid, gid; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 359 | |
| 360 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); |
| 361 | if (retval) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 362 | com_err("ext2fs_mkdir", retval, _("while creating root dir")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 363 | exit(1); |
| 364 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 365 | if (geteuid()) { |
| 366 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); |
| 367 | if (retval) { |
| 368 | com_err("ext2fs_read_inode", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 369 | _("while reading root inode")); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 370 | exit(1); |
| 371 | } |
Eric Sandeen | 5113a6e | 2007-05-08 00:10:54 -0400 | [diff] [blame] | 372 | uid = getuid(); |
| 373 | inode.i_uid = uid; |
Theodore Ts'o | 1534392 | 2008-01-21 09:45:25 -0500 | [diff] [blame] | 374 | ext2fs_set_i_uid_high(inode, uid >> 16); |
Eric Sandeen | 5113a6e | 2007-05-08 00:10:54 -0400 | [diff] [blame] | 375 | if (uid) { |
| 376 | gid = getgid(); |
| 377 | inode.i_gid = gid; |
Theodore Ts'o | 1534392 | 2008-01-21 09:45:25 -0500 | [diff] [blame] | 378 | ext2fs_set_i_gid_high(inode, gid >> 16); |
Eric Sandeen | 5113a6e | 2007-05-08 00:10:54 -0400 | [diff] [blame] | 379 | } |
Theodore Ts'o | e27b456 | 2005-03-21 01:02:53 -0500 | [diff] [blame] | 380 | retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 381 | if (retval) { |
| 382 | com_err("ext2fs_write_inode", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 383 | _("while setting root inode ownership")); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 384 | exit(1); |
| 385 | } |
| 386 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void create_lost_and_found(ext2_filsys fs) |
| 390 | { |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 391 | unsigned int lpf_size = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 392 | errcode_t retval; |
Theodore Ts'o | dfcdc32 | 2001-01-11 15:38:00 +0000 | [diff] [blame] | 393 | ext2_ino_t ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 394 | const char *name = "lost+found"; |
| 395 | int i; |
| 396 | |
Theodore Ts'o | 6a52506 | 2001-12-24 09:40:00 -0500 | [diff] [blame] | 397 | fs->umask = 077; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 398 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); |
| 399 | if (retval) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 400 | com_err("ext2fs_mkdir", retval, |
| 401 | _("while creating /lost+found")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 402 | exit(1); |
| 403 | } |
| 404 | |
| 405 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); |
| 406 | if (retval) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 407 | com_err("ext2_lookup", retval, |
| 408 | _("while looking up /lost+found")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 409 | exit(1); |
| 410 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 411 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 412 | for (i=1; i < EXT2_NDIR_BLOCKS; i++) { |
Andreas Dilger | 8c7c6eb | 2008-01-09 20:59:47 +0100 | [diff] [blame] | 413 | /* Ensure that lost+found is at least 2 blocks, so we always |
| 414 | * test large empty blocks for big-block filesystems. */ |
| 415 | if ((lpf_size += fs->blocksize) >= 16*1024 && |
| 416 | lpf_size >= 2 * fs->blocksize) |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 417 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 418 | retval = ext2fs_expand_dir(fs, ino); |
| 419 | if (retval) { |
| 420 | com_err("ext2fs_expand_dir", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 421 | _("while expanding /lost+found")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 422 | exit(1); |
| 423 | } |
Theodore Ts'o | 6a52506 | 2001-12-24 09:40:00 -0500 | [diff] [blame] | 424 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) |
| 428 | { |
| 429 | errcode_t retval; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 430 | |
Valerie Aurora Henson | 463e732 | 2009-08-05 00:17:56 -0400 | [diff] [blame] | 431 | ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO); |
Theodore Ts'o | 5711ed2 | 2008-04-21 01:29:01 -0400 | [diff] [blame] | 432 | ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 433 | retval = ext2fs_update_bb_inode(fs, bb_list); |
| 434 | if (retval) { |
| 435 | com_err("ext2fs_update_bb_inode", retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 436 | _("while setting bad block inode")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 437 | exit(1); |
| 438 | } |
| 439 | |
| 440 | } |
| 441 | |
| 442 | static void reserve_inodes(ext2_filsys fs) |
| 443 | { |
Theodore Ts'o | dfcdc32 | 2001-01-11 15:38:00 +0000 | [diff] [blame] | 444 | ext2_ino_t i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 445 | |
Theodore Ts'o | 5711ed2 | 2008-04-21 01:29:01 -0400 | [diff] [blame] | 446 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) |
| 447 | ext2fs_inode_alloc_stats2(fs, i, +1, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 448 | ext2fs_mark_ib_dirty(fs); |
| 449 | } |
| 450 | |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 451 | #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */ |
Theodore Ts'o | 7ef3bb8 | 2002-03-08 03:01:53 -0500 | [diff] [blame] | 452 | #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */ |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 453 | #define BSD_LABEL_OFFSET 64 |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 454 | |
Theodore Ts'o | 04a9685 | 2001-08-30 21:55:26 -0400 | [diff] [blame] | 455 | static void zap_sector(ext2_filsys fs, int sect, int nsect) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 456 | { |
Theodore Ts'o | 3f85f65 | 2001-09-17 10:38:06 -0400 | [diff] [blame] | 457 | char *buf; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 458 | int retval; |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 459 | unsigned int *magic; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 460 | |
Theodore Ts'o | 3f85f65 | 2001-09-17 10:38:06 -0400 | [diff] [blame] | 461 | buf = malloc(512*nsect); |
Andreas Dilger | 568101f | 2001-10-13 01:22:25 -0600 | [diff] [blame] | 462 | if (!buf) { |
Theodore Ts'o | 4ea7bd0 | 2001-12-16 23:23:37 -0500 | [diff] [blame] | 463 | printf(_("Out of memory erasing sectors %d-%d\n"), |
| 464 | sect, sect + nsect - 1); |
Andreas Dilger | 568101f | 2001-10-13 01:22:25 -0600 | [diff] [blame] | 465 | exit(1); |
| 466 | } |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 467 | |
Theodore Ts'o | 7ef3bb8 | 2002-03-08 03:01:53 -0500 | [diff] [blame] | 468 | if (sect == 0) { |
| 469 | /* Check for a BSD disklabel, and don't erase it if so */ |
Valerie Aurora Henson | 24a117a | 2009-09-07 21:14:24 -0400 | [diff] [blame] | 470 | retval = io_channel_read_blk64(fs->io, 0, -512, buf); |
Theodore Ts'o | 7ef3bb8 | 2002-03-08 03:01:53 -0500 | [diff] [blame] | 471 | if (retval) |
| 472 | fprintf(stderr, |
| 473 | _("Warning: could not read block 0: %s\n"), |
| 474 | error_message(retval)); |
| 475 | else { |
| 476 | magic = (unsigned int *) (buf + BSD_LABEL_OFFSET); |
| 477 | if ((*magic == BSD_DISKMAGIC) || |
| 478 | (*magic == BSD_MAGICDISK)) |
| 479 | return; |
| 480 | } |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 481 | } |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 482 | |
Theodore Ts'o | 7098810 | 2002-07-14 08:00:00 -0400 | [diff] [blame] | 483 | memset(buf, 0, 512*nsect); |
Theodore Ts'o | e41784e | 2000-08-14 14:44:15 +0000 | [diff] [blame] | 484 | io_channel_set_blksize(fs->io, 512); |
Valerie Aurora Henson | 24a117a | 2009-09-07 21:14:24 -0400 | [diff] [blame] | 485 | retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf); |
Theodore Ts'o | e41784e | 2000-08-14 14:44:15 +0000 | [diff] [blame] | 486 | io_channel_set_blksize(fs->io, fs->blocksize); |
Theodore Ts'o | 3f85f65 | 2001-09-17 10:38:06 -0400 | [diff] [blame] | 487 | free(buf); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 488 | if (retval) |
Theodore Ts'o | 6693837 | 2002-03-08 00:14:46 -0500 | [diff] [blame] | 489 | fprintf(stderr, _("Warning: could not erase sector %d: %s\n"), |
| 490 | sect, error_message(retval)); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 491 | } |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 492 | |
| 493 | static void create_journal_dev(ext2_filsys fs) |
| 494 | { |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 495 | struct ext2fs_numeric_progress_struct progress; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 496 | errcode_t retval; |
| 497 | char *buf; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 498 | blk64_t blk, err_blk; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 499 | int c, count, err_count; |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 500 | |
Theodore Ts'o | d6a27e0 | 2001-04-17 02:34:41 +0000 | [diff] [blame] | 501 | retval = ext2fs_create_journal_superblock(fs, |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 502 | ext2fs_blocks_count(fs->super), 0, &buf); |
Theodore Ts'o | d6a27e0 | 2001-04-17 02:34:41 +0000 | [diff] [blame] | 503 | if (retval) { |
| 504 | com_err("create_journal_dev", retval, |
| 505 | _("while initializing journal superblock")); |
| 506 | exit(1); |
| 507 | } |
Andreas Dilger | 6c54689 | 2011-06-11 12:17:29 -0400 | [diff] [blame] | 508 | |
| 509 | if (journal_flags & EXT2_MKJOURNAL_LAZYINIT) |
| 510 | goto write_superblock; |
| 511 | |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 512 | ext2fs_numeric_progress_init(fs, &progress, |
| 513 | _("Zeroing journal device: "), |
| 514 | ext2fs_blocks_count(fs->super)); |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 515 | blk = 0; |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 516 | count = ext2fs_blocks_count(fs->super); |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 517 | while (count > 0) { |
| 518 | if (count > 1024) |
| 519 | c = 1024; |
| 520 | else |
| 521 | c = count; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 522 | retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count); |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 523 | if (retval) { |
| 524 | com_err("create_journal_dev", retval, |
| 525 | _("while zeroing journal device " |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 526 | "(block %llu, count %d)"), |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 527 | err_blk, err_count); |
| 528 | exit(1); |
| 529 | } |
| 530 | blk += c; |
| 531 | count -= c; |
Theodore Ts'o | c498cb1 | 2012-09-22 21:26:48 -0400 | [diff] [blame^] | 532 | ext2fs_numeric_progress_update(fs, &progress, blk); |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 533 | } |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 534 | ext2fs_zero_blocks2(0, 0, 0, 0, 0); |
Theodore Ts'o | dc2ec52 | 2001-01-18 01:51:15 +0000 | [diff] [blame] | 535 | |
Andreas Dilger | 6c54689 | 2011-06-11 12:17:29 -0400 | [diff] [blame] | 536 | ext2fs_numeric_progress_close(fs, &progress, NULL); |
| 537 | write_superblock: |
Valerie Aurora Henson | 24a117a | 2009-09-07 21:14:24 -0400 | [diff] [blame] | 538 | retval = io_channel_write_blk64(fs->io, |
| 539 | fs->super->s_first_data_block+1, |
| 540 | 1, buf); |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 541 | if (retval) { |
| 542 | com_err("create_journal_dev", retval, |
| 543 | _("while writing journal superblock")); |
| 544 | exit(1); |
| 545 | } |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 546 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 547 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 548 | static void show_stats(ext2_filsys fs) |
| 549 | { |
Theodore Ts'o | ef9abe5 | 2001-01-01 15:31:53 +0000 | [diff] [blame] | 550 | struct ext2_super_block *s = fs->super; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 551 | char buf[80]; |
Theodore Ts'o | 6325394 | 2005-03-19 01:13:22 -0500 | [diff] [blame] | 552 | char *os; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 553 | blk64_t group_block; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 554 | dgrp_t i; |
| 555 | int need, col_left; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 556 | |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 557 | if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s)) |
| 558 | fprintf(stderr, _("warning: %llu blocks unused.\n\n"), |
| 559 | ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s)); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 560 | |
| 561 | memset(buf, 0, sizeof(buf)); |
| 562 | strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 563 | printf(_("Filesystem label=%s\n"), buf); |
Theodore Ts'o | 74e1211 | 2010-12-21 18:12:12 -0500 | [diff] [blame] | 564 | os = e2p_os2string(fs->super->s_creator_os); |
| 565 | if (os) |
| 566 | printf(_("OS type: %s\n"), os); |
Theodore Ts'o | 6325394 | 2005-03-19 01:13:22 -0500 | [diff] [blame] | 567 | free(os); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 568 | printf(_("Block size=%u (log=%u)\n"), fs->blocksize, |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 569 | s->s_log_block_size); |
Theodore Ts'o | 412376e | 2011-02-25 21:43:54 -0500 | [diff] [blame] | 570 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
| 571 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) |
| 572 | printf(_("Cluster size=%u (log=%u)\n"), |
Theodore Ts'o | 1da5ef7 | 2011-06-04 10:20:47 -0400 | [diff] [blame] | 573 | fs->blocksize << fs->cluster_ratio_bits, |
| 574 | s->s_log_cluster_size); |
Theodore Ts'o | 412376e | 2011-02-25 21:43:54 -0500 | [diff] [blame] | 575 | else |
Theodore Ts'o | 1da5ef7 | 2011-06-04 10:20:47 -0400 | [diff] [blame] | 576 | printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s), |
Theodore Ts'o | 412376e | 2011-02-25 21:43:54 -0500 | [diff] [blame] | 577 | s->s_log_cluster_size); |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 578 | printf(_("Stride=%u blocks, Stripe width=%u blocks\n"), |
| 579 | s->s_raid_stride, s->s_raid_stripe_width); |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 580 | printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count, |
| 581 | ext2fs_blocks_count(s)); |
| 582 | printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"), |
| 583 | ext2fs_r_blocks_count(s), |
| 584 | 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s)); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 585 | printf(_("First data block=%u\n"), s->s_first_data_block); |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 586 | if (s->s_reserved_gdt_blocks) |
| 587 | printf(_("Maximum filesystem blocks=%lu\n"), |
| 588 | (s->s_reserved_gdt_blocks + fs->desc_blocks) * |
Valerie Clement | f2de1d3 | 2007-08-30 17:38:13 +0200 | [diff] [blame] | 589 | EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 590 | if (fs->group_desc_count > 1) |
Theodore Ts'o | c8c071a | 2001-01-11 16:08:23 +0000 | [diff] [blame] | 591 | printf(_("%u block groups\n"), fs->group_desc_count); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 592 | else |
Theodore Ts'o | c8c071a | 2001-01-11 16:08:23 +0000 | [diff] [blame] | 593 | printf(_("%u block group\n"), fs->group_desc_count); |
Theodore Ts'o | 412376e | 2011-02-25 21:43:54 -0500 | [diff] [blame] | 594 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
| 595 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) |
| 596 | printf(_("%u blocks per group, %u clusters per group\n"), |
| 597 | s->s_blocks_per_group, s->s_clusters_per_group); |
| 598 | else |
| 599 | printf(_("%u blocks per group, %u fragments per group\n"), |
| 600 | s->s_blocks_per_group, s->s_clusters_per_group); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 601 | printf(_("%u inodes per group\n"), s->s_inodes_per_group); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 602 | |
| 603 | if (fs->group_desc_count == 1) { |
| 604 | printf("\n"); |
| 605 | return; |
| 606 | } |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 607 | |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 608 | printf(_("Superblock backups stored on blocks: ")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 609 | group_block = s->s_first_data_block; |
| 610 | col_left = 0; |
| 611 | for (i = 1; i < fs->group_desc_count; i++) { |
| 612 | group_block += s->s_blocks_per_group; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 613 | if (!ext2fs_bg_has_super(fs, i)) |
| 614 | continue; |
Theodore Ts'o | 7671433 | 1999-10-20 18:06:29 +0000 | [diff] [blame] | 615 | if (i != 1) |
| 616 | printf(", "); |
Theodore Ts'o | 6733c2f | 1999-11-23 02:23:30 +0000 | [diff] [blame] | 617 | need = int_log10(group_block) + 2; |
Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 618 | if (need > col_left) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 619 | printf("\n\t"); |
Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 620 | col_left = 72; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 621 | } |
Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 622 | col_left -= need; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 623 | printf("%llu", group_block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 624 | } |
| 625 | printf("\n\n"); |
| 626 | } |
| 627 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 628 | /* |
| 629 | * Set the S_CREATOR_OS field. Return true if OS is known, |
| 630 | * otherwise, 0. |
| 631 | */ |
| 632 | static int set_os(struct ext2_super_block *sb, char *os) |
| 633 | { |
| 634 | if (isdigit (*os)) |
| 635 | sb->s_creator_os = atoi (os); |
| 636 | else if (strcasecmp(os, "linux") == 0) |
| 637 | sb->s_creator_os = EXT2_OS_LINUX; |
| 638 | else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) |
| 639 | sb->s_creator_os = EXT2_OS_HURD; |
Theodore Ts'o | ea1e8f4 | 2005-01-19 18:18:44 -0500 | [diff] [blame] | 640 | else if (strcasecmp(os, "freebsd") == 0) |
| 641 | sb->s_creator_os = EXT2_OS_FREEBSD; |
| 642 | else if (strcasecmp(os, "lites") == 0) |
| 643 | sb->s_creator_os = EXT2_OS_LITES; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 644 | else |
| 645 | return 0; |
| 646 | return 1; |
| 647 | } |
| 648 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 649 | #define PATH_SET "PATH=/sbin" |
| 650 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 651 | static void parse_extended_opts(struct ext2_super_block *param, |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 652 | const char *opts) |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 653 | { |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 654 | char *buf, *token, *next, *p, *arg, *badopt = 0; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 655 | int len; |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 656 | int r_usage = 0; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 657 | |
| 658 | len = strlen(opts); |
| 659 | buf = malloc(len+1); |
| 660 | if (!buf) { |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 661 | fprintf(stderr, |
| 662 | _("Couldn't allocate memory to parse options!\n")); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 663 | exit(1); |
| 664 | } |
| 665 | strcpy(buf, opts); |
| 666 | for (token = buf; token && *token; token = next) { |
| 667 | p = strchr(token, ','); |
| 668 | next = 0; |
| 669 | if (p) { |
| 670 | *p = 0; |
| 671 | next = p+1; |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 672 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 673 | arg = strchr(token, '='); |
| 674 | if (arg) { |
| 675 | *arg = 0; |
| 676 | arg++; |
| 677 | } |
Andreas Dilger | 0f5eba7 | 2011-09-24 13:48:55 -0400 | [diff] [blame] | 678 | if (strcmp(token, "mmp_update_interval") == 0) { |
| 679 | if (!arg) { |
| 680 | r_usage++; |
| 681 | badopt = token; |
| 682 | continue; |
| 683 | } |
| 684 | param->s_mmp_update_interval = strtoul(arg, &p, 0); |
| 685 | if (*p) { |
| 686 | fprintf(stderr, |
| 687 | _("Invalid mmp_update_interval: %s\n"), |
| 688 | arg); |
| 689 | r_usage++; |
| 690 | continue; |
| 691 | } |
| 692 | } else if (strcmp(token, "stride") == 0) { |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 693 | if (!arg) { |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 694 | r_usage++; |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 695 | badopt = token; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 696 | continue; |
| 697 | } |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 698 | param->s_raid_stride = strtoul(arg, &p, 0); |
Theodore Ts'o | 5b734a0 | 2011-07-04 20:22:19 -0400 | [diff] [blame] | 699 | if (*p) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 700 | fprintf(stderr, |
Theodore Ts'o | f37ab68 | 2005-05-05 23:15:55 -0400 | [diff] [blame] | 701 | _("Invalid stride parameter: %s\n"), |
| 702 | arg); |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 703 | r_usage++; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 704 | continue; |
| 705 | } |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 706 | } else if (strcmp(token, "stripe-width") == 0 || |
| 707 | strcmp(token, "stripe_width") == 0) { |
| 708 | if (!arg) { |
| 709 | r_usage++; |
| 710 | badopt = token; |
| 711 | continue; |
| 712 | } |
| 713 | param->s_raid_stripe_width = strtoul(arg, &p, 0); |
Theodore Ts'o | 5b734a0 | 2011-07-04 20:22:19 -0400 | [diff] [blame] | 714 | if (*p) { |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 715 | fprintf(stderr, |
| 716 | _("Invalid stripe-width parameter: %s\n"), |
| 717 | arg); |
| 718 | r_usage++; |
| 719 | continue; |
| 720 | } |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 721 | } else if (!strcmp(token, "resize")) { |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 722 | blk64_t resize; |
| 723 | unsigned long bpg, rsv_groups; |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 724 | unsigned long group_desc_count, desc_blocks; |
| 725 | unsigned int gdpb, blocksize; |
| 726 | int rsv_gdb; |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 727 | |
| 728 | if (!arg) { |
| 729 | r_usage++; |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 730 | badopt = token; |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 731 | continue; |
| 732 | } |
| 733 | |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 734 | resize = parse_num_blocks2(arg, |
| 735 | param->s_log_block_size); |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 736 | |
| 737 | if (resize == 0) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 738 | fprintf(stderr, |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 739 | _("Invalid resize parameter: %s\n"), |
| 740 | arg); |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 741 | r_usage++; |
| 742 | continue; |
| 743 | } |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 744 | if (resize <= ext2fs_blocks_count(param)) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 745 | fprintf(stderr, |
Theodore Ts'o | f37ab68 | 2005-05-05 23:15:55 -0400 | [diff] [blame] | 746 | _("The resize maximum must be greater " |
| 747 | "than the filesystem size.\n")); |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 748 | r_usage++; |
| 749 | continue; |
| 750 | } |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 751 | |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 752 | blocksize = EXT2_BLOCK_SIZE(param); |
| 753 | bpg = param->s_blocks_per_group; |
| 754 | if (!bpg) |
| 755 | bpg = blocksize * 8; |
Valerie Clement | f2de1d3 | 2007-08-30 17:38:13 +0200 | [diff] [blame] | 756 | gdpb = EXT2_DESC_PER_BLOCK(param); |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 757 | group_desc_count = (__u32) ext2fs_div64_ceil( |
| 758 | ext2fs_blocks_count(param), bpg); |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 759 | desc_blocks = (group_desc_count + |
| 760 | gdpb - 1) / gdpb; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 761 | rsv_groups = ext2fs_div64_ceil(resize, bpg); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 762 | rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 763 | desc_blocks; |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 764 | if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param)) |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 765 | rsv_gdb = EXT2_ADDR_PER_BLOCK(param); |
| 766 | |
| 767 | if (rsv_gdb > 0) { |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 768 | if (param->s_rev_level == EXT2_GOOD_OLD_REV) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 769 | fprintf(stderr, |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 770 | _("On-line resizing not supported with revision 0 filesystems\n")); |
Brian Behlendorf | 2140038 | 2007-05-31 11:30:47 -0400 | [diff] [blame] | 771 | free(buf); |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 772 | exit(1); |
| 773 | } |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 774 | param->s_feature_compat |= |
| 775 | EXT2_FEATURE_COMPAT_RESIZE_INODE; |
| 776 | |
| 777 | param->s_reserved_gdt_blocks = rsv_gdb; |
| 778 | } |
Theodore Ts'o | 6cb2740 | 2008-01-26 19:06:35 -0500 | [diff] [blame] | 779 | } else if (!strcmp(token, "test_fs")) { |
| 780 | param->s_flags |= EXT2_FLAGS_TEST_FILESYS; |
Theodore Ts'o | a4396e9 | 2008-04-18 10:19:27 -0400 | [diff] [blame] | 781 | } else if (!strcmp(token, "lazy_itable_init")) { |
Theodore Ts'o | 43781b9 | 2008-04-27 19:38:02 -0400 | [diff] [blame] | 782 | if (arg) |
| 783 | lazy_itable_init = strtoul(arg, &p, 0); |
| 784 | else |
| 785 | lazy_itable_init = 1; |
Andreas Dilger | 6c54689 | 2011-06-11 12:17:29 -0400 | [diff] [blame] | 786 | } else if (!strcmp(token, "lazy_journal_init")) { |
| 787 | if (arg) |
| 788 | journal_flags |= strtoul(arg, &p, 0) ? |
| 789 | EXT2_MKJOURNAL_LAZYINIT : 0; |
| 790 | else |
| 791 | journal_flags |= EXT2_MKJOURNAL_LAZYINIT; |
Lukas Czerner | 0bc85df | 2010-11-18 14:38:39 +0100 | [diff] [blame] | 792 | } else if (!strcmp(token, "discard")) { |
| 793 | discard = 1; |
| 794 | } else if (!strcmp(token, "nodiscard")) { |
| 795 | discard = 0; |
Aditya Kali | d678fef | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 796 | } else if (!strcmp(token, "quotatype")) { |
| 797 | if (!arg) { |
| 798 | r_usage++; |
| 799 | badopt = token; |
| 800 | continue; |
| 801 | } |
| 802 | if (!strncmp(arg, "usr", 3)) { |
| 803 | quotatype = 0; |
| 804 | } else if (!strncmp(arg, "grp", 3)) { |
| 805 | quotatype = 1; |
| 806 | } else { |
| 807 | fprintf(stderr, |
| 808 | _("Invalid quotatype parameter: %s\n"), |
| 809 | arg); |
| 810 | r_usage++; |
| 811 | continue; |
| 812 | } |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 813 | } else { |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 814 | r_usage++; |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 815 | badopt = token; |
| 816 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 817 | } |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 818 | if (r_usage) { |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 819 | fprintf(stderr, _("\nBad option(s) specified: %s\n\n" |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 820 | "Extended options are separated by commas, " |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 821 | "and may take an argument which\n" |
| 822 | "\tis set off by an equals ('=') sign.\n\n" |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 823 | "Valid extended options are:\n" |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 824 | "\tstride=<RAID per-disk data chunk in blocks>\n" |
| 825 | "\tstripe-width=<RAID stride * data disks in blocks>\n" |
Theodore Ts'o | a4396e9 | 2008-04-18 10:19:27 -0400 | [diff] [blame] | 826 | "\tresize=<resize maximum size in blocks>\n" |
| 827 | "\tlazy_itable_init=<0 to disable, 1 to enable>\n" |
Andreas Dilger | 6c54689 | 2011-06-11 12:17:29 -0400 | [diff] [blame] | 828 | "\tlazy_journal_init=<0 to disable, 1 to enable>\n" |
Lukas Czerner | 0bc85df | 2010-11-18 14:38:39 +0100 | [diff] [blame] | 829 | "\ttest_fs\n" |
| 830 | "\tdiscard\n" |
Aditya Kali | d678fef | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 831 | "\tnodiscard\n" |
| 832 | "\tquotatype=<usr OR grp>\n\n"), |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 833 | badopt ? badopt : ""); |
Brian Behlendorf | 2140038 | 2007-05-31 11:30:47 -0400 | [diff] [blame] | 834 | free(buf); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 835 | exit(1); |
| 836 | } |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 837 | if (param->s_raid_stride && |
| 838 | (param->s_raid_stripe_width % param->s_raid_stride) != 0) |
| 839 | fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even " |
| 840 | "multiple of stride %u.\n\n"), |
| 841 | param->s_raid_stripe_width, param->s_raid_stride); |
| 842 | |
Brian Behlendorf | 2140038 | 2007-05-31 11:30:47 -0400 | [diff] [blame] | 843 | free(buf); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 844 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 845 | |
Theodore Ts'o | 896938d | 1999-10-23 01:04:50 +0000 | [diff] [blame] | 846 | static __u32 ok_features[3] = { |
Theodore Ts'o | 558df54 | 2008-02-27 15:01:19 -0500 | [diff] [blame] | 847 | /* Compat */ |
Theodore Ts'o | 843049c | 2002-09-22 15:37:40 -0400 | [diff] [blame] | 848 | EXT3_FEATURE_COMPAT_HAS_JOURNAL | |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 849 | EXT2_FEATURE_COMPAT_RESIZE_INODE | |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 850 | EXT2_FEATURE_COMPAT_DIR_INDEX | |
Theodore Ts'o | 558df54 | 2008-02-27 15:01:19 -0500 | [diff] [blame] | 851 | EXT2_FEATURE_COMPAT_EXT_ATTR, |
| 852 | /* Incompat */ |
| 853 | EXT2_FEATURE_INCOMPAT_FILETYPE| |
Theodore Ts'o | bf6b848 | 2008-02-20 08:13:19 -0500 | [diff] [blame] | 854 | EXT3_FEATURE_INCOMPAT_EXTENTS| |
Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 855 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV| |
Jose R. Santos | c2d4300 | 2007-08-13 23:32:57 -0500 | [diff] [blame] | 856 | EXT2_FEATURE_INCOMPAT_META_BG| |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 857 | EXT4_FEATURE_INCOMPAT_FLEX_BG| |
Andreas Dilger | 0f5eba7 | 2011-09-24 13:48:55 -0400 | [diff] [blame] | 858 | EXT4_FEATURE_INCOMPAT_MMP | |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 859 | EXT4_FEATURE_INCOMPAT_64BIT, |
Theodore Ts'o | 558df54 | 2008-02-27 15:01:19 -0500 | [diff] [blame] | 860 | /* R/O compat */ |
| 861 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE| |
Theodore Ts'o | 2be8fe4 | 2008-07-10 10:49:59 -0400 | [diff] [blame] | 862 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE| |
| 863 | EXT4_FEATURE_RO_COMPAT_DIR_NLINK| |
| 864 | EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE| |
Jose R. Santos | d2d22a2 | 2007-10-21 21:03:36 -0500 | [diff] [blame] | 865 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 866 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM| |
Aditya Kali | 1f5d7a8 | 2011-07-20 11:40:04 -0700 | [diff] [blame] | 867 | EXT4_FEATURE_RO_COMPAT_BIGALLOC| |
Theodore Ts'o | 7becb20 | 2011-11-14 10:40:43 -0500 | [diff] [blame] | 868 | #ifdef CONFIG_QUOTA |
| 869 | EXT4_FEATURE_RO_COMPAT_QUOTA| |
| 870 | #endif |
| 871 | 0 |
Theodore Ts'o | 896938d | 1999-10-23 01:04:50 +0000 | [diff] [blame] | 872 | }; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 873 | |
| 874 | |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 875 | static void syntax_err_report(const char *filename, long err, int line_num) |
| 876 | { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 877 | fprintf(stderr, |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 878 | _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"), |
| 879 | filename, line_num, error_message(err)); |
| 880 | exit(1); |
| 881 | } |
| 882 | |
Matthias Andree | abcfdfd | 2006-06-10 16:08:18 +0200 | [diff] [blame] | 883 | static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 }; |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 884 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 885 | static void edit_feature(const char *str, __u32 *compat_array) |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 886 | { |
| 887 | if (!str) |
| 888 | return; |
| 889 | |
| 890 | if (e2p_edit_feature(str, compat_array, ok_features)) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 891 | fprintf(stderr, _("Invalid filesystem option set: %s\n"), |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 892 | str); |
| 893 | exit(1); |
| 894 | } |
| 895 | } |
| 896 | |
Eric Sandeen | 6a426c9 | 2011-02-17 15:56:17 -0600 | [diff] [blame] | 897 | static void edit_mntopts(const char *str, __u32 *mntopts) |
| 898 | { |
| 899 | if (!str) |
| 900 | return; |
| 901 | |
| 902 | if (e2p_edit_mntopts(str, mntopts, ~0)) { |
| 903 | fprintf(stderr, _("Invalid mount option set: %s\n"), |
| 904 | str); |
| 905 | exit(1); |
| 906 | } |
| 907 | } |
| 908 | |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 909 | struct str_list { |
| 910 | char **list; |
| 911 | int num; |
| 912 | int max; |
| 913 | }; |
| 914 | |
| 915 | static errcode_t init_list(struct str_list *sl) |
| 916 | { |
| 917 | sl->num = 0; |
| 918 | sl->max = 0; |
| 919 | sl->list = malloc((sl->max+1) * sizeof(char *)); |
| 920 | if (!sl->list) |
| 921 | return ENOMEM; |
| 922 | sl->list[0] = 0; |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | static errcode_t push_string(struct str_list *sl, const char *str) |
| 927 | { |
| 928 | char **new_list; |
| 929 | |
| 930 | if (sl->num >= sl->max) { |
| 931 | sl->max += 2; |
| 932 | new_list = realloc(sl->list, (sl->max+1) * sizeof(char *)); |
| 933 | if (!new_list) |
| 934 | return ENOMEM; |
| 935 | sl->list = new_list; |
| 936 | } |
| 937 | sl->list[sl->num] = malloc(strlen(str)+1); |
| 938 | if (sl->list[sl->num] == 0) |
| 939 | return ENOMEM; |
| 940 | strcpy(sl->list[sl->num], str); |
| 941 | sl->num++; |
| 942 | sl->list[sl->num] = 0; |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | static void print_str_list(char **list) |
| 947 | { |
| 948 | char **cpp; |
| 949 | |
| 950 | for (cpp = list; *cpp; cpp++) { |
| 951 | printf("'%s'", *cpp); |
| 952 | if (cpp[1]) |
| 953 | fputs(", ", stdout); |
| 954 | } |
| 955 | fputc('\n', stdout); |
| 956 | } |
| 957 | |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 958 | /* |
| 959 | * Return TRUE if the profile has the given subsection |
| 960 | */ |
| 961 | static int profile_has_subsection(profile_t profile, const char *section, |
| 962 | const char *subsection) |
| 963 | { |
| 964 | void *state; |
| 965 | const char *names[4]; |
| 966 | char *name; |
| 967 | int ret = 0; |
| 968 | |
| 969 | names[0] = section; |
| 970 | names[1] = subsection; |
| 971 | names[2] = 0; |
| 972 | |
| 973 | if (profile_iterator_create(profile, names, |
| 974 | PROFILE_ITER_LIST_SECTION | |
| 975 | PROFILE_ITER_RELATIONS_ONLY, &state)) |
| 976 | return 0; |
| 977 | |
| 978 | if ((profile_iterator(&state, &name, 0) == 0) && name) { |
| 979 | free(name); |
| 980 | ret = 1; |
| 981 | } |
| 982 | |
| 983 | profile_iterator_free(&state); |
| 984 | return ret; |
| 985 | } |
| 986 | |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 987 | static char **parse_fs_type(const char *fs_type, |
| 988 | const char *usage_types, |
| 989 | struct ext2_super_block *fs_param, |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 990 | blk64_t fs_blocks_count, |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 991 | char *progname) |
| 992 | { |
| 993 | const char *ext_type = 0; |
| 994 | char *parse_str; |
| 995 | char *profile_type = 0; |
| 996 | char *cp, *t; |
| 997 | const char *size_type; |
| 998 | struct str_list list; |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 999 | unsigned long long meg; |
Theodore Ts'o | 7f5601e | 2008-07-10 09:24:25 -0400 | [diff] [blame] | 1000 | int is_hurd = 0; |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1001 | |
| 1002 | if (init_list(&list)) |
| 1003 | return 0; |
| 1004 | |
Theodore Ts'o | 7f5601e | 2008-07-10 09:24:25 -0400 | [diff] [blame] | 1005 | if (creator_os && (!strcasecmp(creator_os, "GNU") || |
| 1006 | !strcasecmp(creator_os, "hurd"))) |
| 1007 | is_hurd = 1; |
| 1008 | |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1009 | if (fs_type) |
| 1010 | ext_type = fs_type; |
Theodore Ts'o | 7f5601e | 2008-07-10 09:24:25 -0400 | [diff] [blame] | 1011 | else if (is_hurd) |
| 1012 | ext_type = "ext2"; |
Theodore Ts'o | 1a71bd4 | 2009-01-20 12:02:40 -0500 | [diff] [blame] | 1013 | else if (!strcmp(program_name, "mke3fs")) |
| 1014 | ext_type = "ext3"; |
Eric Sandeen | 237b7b2 | 2012-02-15 15:11:35 -0500 | [diff] [blame] | 1015 | else if (!strcmp(program_name, "mke4fs")) |
| 1016 | ext_type = "ext4"; |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1017 | else if (progname) { |
| 1018 | ext_type = strrchr(progname, '/'); |
| 1019 | if (ext_type) |
| 1020 | ext_type++; |
| 1021 | else |
| 1022 | ext_type = progname; |
| 1023 | |
| 1024 | if (!strncmp(ext_type, "mkfs.", 5)) { |
| 1025 | ext_type += 5; |
| 1026 | if (ext_type[0] == 0) |
| 1027 | ext_type = 0; |
| 1028 | } else |
| 1029 | ext_type = 0; |
| 1030 | } |
| 1031 | |
| 1032 | if (!ext_type) { |
| 1033 | profile_get_string(profile, "defaults", "fs_type", 0, |
| 1034 | "ext2", &profile_type); |
| 1035 | ext_type = profile_type; |
| 1036 | if (!strcmp(ext_type, "ext2") && (journal_size != 0)) |
| 1037 | ext_type = "ext3"; |
| 1038 | } |
| 1039 | |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 1040 | |
| 1041 | if (!profile_has_subsection(profile, "fs_types", ext_type) && |
| 1042 | strcmp(ext_type, "ext2")) { |
| 1043 | printf(_("\nYour mke2fs.conf file does not define the " |
| 1044 | "%s filesystem type.\n"), ext_type); |
| 1045 | if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") || |
| 1046 | !strcmp(ext_type, "ext4dev")) { |
Theodore Ts'o | bad89b2 | 2008-08-22 21:57:29 -0400 | [diff] [blame] | 1047 | printf(_("You probably need to install an updated " |
| 1048 | "mke2fs.conf file.\n\n")); |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 1049 | } |
| 1050 | if (!force) { |
| 1051 | printf(_("Aborting...\n")); |
| 1052 | exit(1); |
Theodore Ts'o | bad89b2 | 2008-08-22 21:57:29 -0400 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1056 | meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param); |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1057 | if (fs_blocks_count < 3 * meg) |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1058 | size_type = "floppy"; |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1059 | else if (fs_blocks_count < 512 * meg) |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1060 | size_type = "small"; |
Namhyung Kim | 22d8ce5 | 2010-11-29 17:55:13 +0900 | [diff] [blame] | 1061 | else if (fs_blocks_count < 4 * 1024 * 1024 * meg) |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1062 | size_type = "default"; |
Namhyung Kim | 22d8ce5 | 2010-11-29 17:55:13 +0900 | [diff] [blame] | 1063 | else if (fs_blocks_count < 16 * 1024 * 1024 * meg) |
| 1064 | size_type = "big"; |
| 1065 | else |
| 1066 | size_type = "huge"; |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1067 | |
| 1068 | if (!usage_types) |
| 1069 | usage_types = size_type; |
| 1070 | |
Eric Sandeen | 4d5cf8b | 2011-09-16 15:49:19 -0500 | [diff] [blame] | 1071 | parse_str = malloc(strlen(usage_types)+1); |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1072 | if (!parse_str) { |
| 1073 | free(list.list); |
| 1074 | return 0; |
| 1075 | } |
Eric Sandeen | 4d5cf8b | 2011-09-16 15:49:19 -0500 | [diff] [blame] | 1076 | strcpy(parse_str, usage_types); |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1077 | |
| 1078 | if (ext_type) |
| 1079 | push_string(&list, ext_type); |
| 1080 | cp = parse_str; |
| 1081 | while (1) { |
| 1082 | t = strchr(cp, ','); |
| 1083 | if (t) |
| 1084 | *t = '\0'; |
| 1085 | |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 1086 | if (*cp) { |
Theodore Ts'o | 0f7479b | 2010-12-22 18:31:36 -0500 | [diff] [blame] | 1087 | if (profile_has_subsection(profile, "fs_types", cp)) |
| 1088 | push_string(&list, cp); |
| 1089 | else if (strcmp(cp, "default") != 0) |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 1090 | fprintf(stderr, |
| 1091 | _("\nWarning: the fs_type %s is not " |
Theodore Ts'o | 0f7479b | 2010-12-22 18:31:36 -0500 | [diff] [blame] | 1092 | "defined in mke2fs.conf\n\n"), |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 1093 | cp); |
Theodore Ts'o | 2ee4544 | 2010-12-01 18:28:35 -0500 | [diff] [blame] | 1094 | } |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1095 | if (t) |
| 1096 | cp = t+1; |
| 1097 | else { |
| 1098 | cp = ""; |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | free(parse_str); |
Jim Meyering | 45e338f | 2009-02-23 18:07:50 +0100 | [diff] [blame] | 1103 | free(profile_type); |
Theodore Ts'o | 7f5601e | 2008-07-10 09:24:25 -0400 | [diff] [blame] | 1104 | if (is_hurd) |
| 1105 | push_string(&list, "hurd"); |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1106 | return (list.list); |
| 1107 | } |
| 1108 | |
| 1109 | static char *get_string_from_profile(char **fs_types, const char *opt, |
| 1110 | const char *def_val) |
| 1111 | { |
| 1112 | char *ret = 0; |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1113 | int i; |
| 1114 | |
| 1115 | for (i=0; fs_types[i]; i++); |
| 1116 | for (i-=1; i >=0 ; i--) { |
| 1117 | profile_get_string(profile, "fs_types", fs_types[i], |
| 1118 | opt, 0, &ret); |
| 1119 | if (ret) |
| 1120 | return ret; |
| 1121 | } |
| 1122 | profile_get_string(profile, "defaults", opt, 0, def_val, &ret); |
| 1123 | return (ret); |
| 1124 | } |
| 1125 | |
| 1126 | static int get_int_from_profile(char **fs_types, const char *opt, int def_val) |
| 1127 | { |
| 1128 | int ret; |
| 1129 | char **cpp; |
| 1130 | |
| 1131 | profile_get_integer(profile, "defaults", opt, 0, def_val, &ret); |
| 1132 | for (cpp = fs_types; *cpp; cpp++) |
| 1133 | profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret); |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
Aditya Kali | d3859af | 2011-05-10 14:51:31 -0700 | [diff] [blame] | 1137 | static double get_double_from_profile(char **fs_types, const char *opt, |
| 1138 | double def_val) |
| 1139 | { |
| 1140 | double ret; |
| 1141 | char **cpp; |
| 1142 | |
| 1143 | profile_get_double(profile, "defaults", opt, 0, def_val, &ret); |
| 1144 | for (cpp = fs_types; *cpp; cpp++) |
| 1145 | profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret); |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
Theodore Ts'o | a4396e9 | 2008-04-18 10:19:27 -0400 | [diff] [blame] | 1149 | static int get_bool_from_profile(char **fs_types, const char *opt, int def_val) |
| 1150 | { |
| 1151 | int ret; |
| 1152 | char **cpp; |
| 1153 | |
| 1154 | profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret); |
| 1155 | for (cpp = fs_types; *cpp; cpp++) |
| 1156 | profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret); |
| 1157 | return ret; |
| 1158 | } |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1159 | |
Theodore Ts'o | d48bc60 | 2007-07-04 14:10:46 -0400 | [diff] [blame] | 1160 | extern const char *mke2fs_default_profile; |
| 1161 | static const char *default_files[] = { "<default>", 0 }; |
| 1162 | |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1163 | #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY |
| 1164 | /* |
| 1165 | * Sets the geometry of a device (stripe/stride), and returns the |
| 1166 | * device's alignment offset, if any, or a negative error. |
| 1167 | */ |
Theodore Ts'o | 1599b47 | 2010-11-22 10:50:42 -0500 | [diff] [blame] | 1168 | static int get_device_geometry(const char *file, |
| 1169 | struct ext2_super_block *fs_param, |
| 1170 | int psector_size) |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1171 | { |
| 1172 | int rc = -1; |
| 1173 | int blocksize; |
| 1174 | blkid_probe pr; |
| 1175 | blkid_topology tp; |
| 1176 | unsigned long min_io; |
| 1177 | unsigned long opt_io; |
Eric Sandeen | 13b0b12 | 2010-01-23 21:50:45 -0600 | [diff] [blame] | 1178 | struct stat statbuf; |
| 1179 | |
| 1180 | /* Nothing to do for a regular file */ |
| 1181 | if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode)) |
| 1182 | return 0; |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1183 | |
| 1184 | pr = blkid_new_probe_from_filename(file); |
| 1185 | if (!pr) |
| 1186 | goto out; |
| 1187 | |
| 1188 | tp = blkid_probe_get_topology(pr); |
| 1189 | if (!tp) |
| 1190 | goto out; |
| 1191 | |
| 1192 | min_io = blkid_topology_get_minimum_io_size(tp); |
| 1193 | opt_io = blkid_topology_get_optimal_io_size(tp); |
| 1194 | blocksize = EXT2_BLOCK_SIZE(fs_param); |
Theodore Ts'o | 1599b47 | 2010-11-22 10:50:42 -0500 | [diff] [blame] | 1195 | if ((min_io == 0) && (psector_size > blocksize)) |
| 1196 | min_io = psector_size; |
| 1197 | if ((opt_io == 0) && min_io) |
| 1198 | opt_io = min_io; |
| 1199 | if ((opt_io == 0) && (psector_size > blocksize)) |
| 1200 | opt_io = psector_size; |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1201 | |
Eric Sandeen | d568782 | 2011-04-04 15:11:52 -0400 | [diff] [blame] | 1202 | /* setting stripe/stride to blocksize is pointless */ |
| 1203 | if (min_io > blocksize) |
| 1204 | fs_param->s_raid_stride = min_io / blocksize; |
| 1205 | if (opt_io > blocksize) |
| 1206 | fs_param->s_raid_stripe_width = opt_io / blocksize; |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1207 | |
| 1208 | rc = blkid_topology_get_alignment_offset(tp); |
| 1209 | out: |
| 1210 | blkid_free_probe(pr); |
| 1211 | return rc; |
| 1212 | } |
| 1213 | #endif |
| 1214 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1215 | static void PRS(int argc, char *argv[]) |
| 1216 | { |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1217 | int b, c; |
Theodore Ts'o | 2d34a25 | 2011-06-14 14:30:22 -0400 | [diff] [blame] | 1218 | int cluster_size = 0; |
Eric Sandeen | 79e6240 | 2008-07-06 18:36:56 -0400 | [diff] [blame] | 1219 | char *tmp, **cpp; |
Theodore Ts'o | 4a60056 | 1999-10-26 14:35:51 +0000 | [diff] [blame] | 1220 | int blocksize = 0; |
| 1221 | int inode_ratio = 0; |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1222 | int inode_size = 0; |
Theodore Ts'o | 9ba4000 | 2008-04-22 08:27:01 -0400 | [diff] [blame] | 1223 | unsigned long flex_bg_size = 0; |
Aditya Kali | d3859af | 2011-05-10 14:51:31 -0700 | [diff] [blame] | 1224 | double reserved_ratio = -1.0; |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1225 | int lsector_size = 0, psector_size = 0; |
Theodore Ts'o | 1e6e4c5 | 2003-12-07 02:28:24 -0500 | [diff] [blame] | 1226 | int show_version_only = 0; |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 1227 | unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */ |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1228 | errcode_t retval; |
Theodore Ts'o | 4a60056 | 1999-10-26 14:35:51 +0000 | [diff] [blame] | 1229 | char * oldpath = getenv("PATH"); |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 1230 | char * extended_opts = 0; |
Theodore Ts'o | 4c2b28a | 2011-06-14 14:17:56 -0400 | [diff] [blame] | 1231 | char * fs_type = 0; |
| 1232 | char * usage_types = 0; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 1233 | blk64_t dev_size; |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1234 | blk64_t fs_blocks_count = 0; |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 1235 | #ifdef __linux__ |
Theodore Ts'o | 4a60056 | 1999-10-26 14:35:51 +0000 | [diff] [blame] | 1236 | struct utsname ut; |
Theodore Ts'o | 2740156 | 1999-09-14 20:11:19 +0000 | [diff] [blame] | 1237 | #endif |
Theodore Ts'o | 31e29a1 | 2002-05-17 10:53:07 -0400 | [diff] [blame] | 1238 | long sysval; |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1239 | int s_opt = -1, r_opt = -1; |
| 1240 | char *fs_features = 0; |
| 1241 | int use_bsize; |
Theodore Ts'o | 642935c | 2006-11-14 23:38:17 -0500 | [diff] [blame] | 1242 | char *newpath; |
| 1243 | int pathlen = sizeof(PATH_SET) + 1; |
| 1244 | |
| 1245 | if (oldpath) |
| 1246 | pathlen += strlen(oldpath); |
| 1247 | newpath = malloc(pathlen); |
Namhyung Kim | 9ec68af | 2010-12-16 18:40:40 +0900 | [diff] [blame] | 1248 | if (!newpath) { |
| 1249 | fprintf(stderr, _("Couldn't allocate memory for new PATH.\n")); |
| 1250 | exit(1); |
| 1251 | } |
Theodore Ts'o | 642935c | 2006-11-14 23:38:17 -0500 | [diff] [blame] | 1252 | strcpy(newpath, PATH_SET); |
Theodore Ts'o | 14bbcbc | 2001-05-13 22:58:27 +0000 | [diff] [blame] | 1253 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1254 | /* Update our PATH to include /sbin */ |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1255 | if (oldpath) { |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1256 | strcat (newpath, ":"); |
| 1257 | strcat (newpath, oldpath); |
Theodore Ts'o | 642935c | 2006-11-14 23:38:17 -0500 | [diff] [blame] | 1258 | } |
| 1259 | putenv (newpath); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1260 | |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 1261 | tmp = getenv("MKE2FS_SYNC"); |
| 1262 | if (tmp) |
| 1263 | sync_kludge = atoi(tmp); |
Theodore Ts'o | 31e29a1 | 2002-05-17 10:53:07 -0400 | [diff] [blame] | 1264 | |
| 1265 | /* Determine the system page size if possible */ |
| 1266 | #ifdef HAVE_SYSCONF |
| 1267 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) |
| 1268 | #define _SC_PAGESIZE _SC_PAGE_SIZE |
| 1269 | #endif |
| 1270 | #ifdef _SC_PAGESIZE |
| 1271 | sysval = sysconf(_SC_PAGESIZE); |
Andreas Dilger | aab6fe7 | 2002-05-18 13:27:33 -0600 | [diff] [blame] | 1272 | if (sysval > 0) |
Theodore Ts'o | 31e29a1 | 2002-05-17 10:53:07 -0400 | [diff] [blame] | 1273 | sys_page_size = sysval; |
| 1274 | #endif /* _SC_PAGESIZE */ |
| 1275 | #endif /* HAVE_SYSCONF */ |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1276 | |
| 1277 | if ((tmp = getenv("MKE2FS_CONFIG")) != NULL) |
| 1278 | config_fn[0] = tmp; |
| 1279 | profile_set_syntax_err_cb(syntax_err_report); |
Theodore Ts'o | d48bc60 | 2007-07-04 14:10:46 -0400 | [diff] [blame] | 1280 | retval = profile_init(config_fn, &profile); |
| 1281 | if (retval == ENOENT) { |
Namhyung Kim | 9ec68af | 2010-12-16 18:40:40 +0900 | [diff] [blame] | 1282 | retval = profile_init(default_files, &profile); |
| 1283 | if (retval) |
| 1284 | goto profile_error; |
| 1285 | retval = profile_set_default(profile, mke2fs_default_profile); |
| 1286 | if (retval) |
| 1287 | goto profile_error; |
| 1288 | } else if (retval) { |
| 1289 | profile_error: |
| 1290 | fprintf(stderr, _("Couldn't init profile successfully" |
| 1291 | " (error: %ld).\n"), retval); |
| 1292 | exit(1); |
Theodore Ts'o | d48bc60 | 2007-07-04 14:10:46 -0400 | [diff] [blame] | 1293 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1294 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1295 | setbuf(stdout, NULL); |
| 1296 | setbuf(stderr, NULL); |
Theodore Ts'o | a6d8302 | 2006-12-26 03:38:07 -0500 | [diff] [blame] | 1297 | add_error_table(&et_ext2_error_table); |
| 1298 | add_error_table(&et_prof_error_table); |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1299 | memset(&fs_param, 0, sizeof(struct ext2_super_block)); |
| 1300 | fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */ |
Theodore Ts'o | 843049c | 2002-09-22 15:37:40 -0400 | [diff] [blame] | 1301 | |
Theodore Ts'o | 756df35 | 2002-03-07 20:52:12 -0500 | [diff] [blame] | 1302 | #ifdef __linux__ |
Theodore Ts'o | 14bbcbc | 2001-05-13 22:58:27 +0000 | [diff] [blame] | 1303 | if (uname(&ut)) { |
| 1304 | perror("uname"); |
| 1305 | exit(1); |
| 1306 | } |
Theodore Ts'o | d99225e | 2004-09-25 07:40:12 -0400 | [diff] [blame] | 1307 | linux_version_code = parse_version_number(ut.release); |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1308 | if (linux_version_code && linux_version_code < (2*65536 + 2*256)) |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1309 | fs_param.s_rev_level = 0; |
Theodore Ts'o | 14bbcbc | 2001-05-13 22:58:27 +0000 | [diff] [blame] | 1310 | #endif |
Andreas Dilger | 0072f8d | 2002-02-25 23:11:26 -0700 | [diff] [blame] | 1311 | |
| 1312 | if (argc && *argv) { |
| 1313 | program_name = get_progname(*argv); |
| 1314 | |
| 1315 | /* If called as mkfs.ext3, create a journal inode */ |
Theodore Ts'o | 1a71bd4 | 2009-01-20 12:02:40 -0500 | [diff] [blame] | 1316 | if (!strcmp(program_name, "mkfs.ext3") || |
| 1317 | !strcmp(program_name, "mke3fs")) |
Andreas Dilger | 0072f8d | 2002-02-25 23:11:26 -0700 | [diff] [blame] | 1318 | journal_size = -1; |
| 1319 | } |
| 1320 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 1321 | while ((c = getopt (argc, argv, |
Theodore Ts'o | 37c8db7 | 2012-03-22 16:00:49 -0400 | [diff] [blame] | 1322 | "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1323 | switch (c) { |
| 1324 | case 'b': |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1325 | blocksize = strtol(optarg, &tmp, 0); |
| 1326 | b = (blocksize > 0) ? blocksize : -blocksize; |
| 1327 | if (b < EXT2_MIN_BLOCK_SIZE || |
| 1328 | b > EXT2_MAX_BLOCK_SIZE || *tmp) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1329 | com_err(program_name, 0, |
Theodore Ts'o | f37ab68 | 2005-05-05 23:15:55 -0400 | [diff] [blame] | 1330 | _("invalid block size - %s"), optarg); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1331 | exit(1); |
| 1332 | } |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1333 | if (blocksize > 4096) |
| 1334 | fprintf(stderr, _("Warning: blocksize %d not " |
| 1335 | "usable on most systems.\n"), |
| 1336 | blocksize); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1337 | if (blocksize > 0) |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1338 | fs_param.s_log_block_size = |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1339 | int_log2(blocksize >> |
| 1340 | EXT2_MIN_BLOCK_LOG_SIZE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1341 | break; |
Theodore Ts'o | b10fd5e | 2001-04-22 03:47:23 +0000 | [diff] [blame] | 1342 | case 'c': /* Check for bad blocks */ |
Theodore Ts'o | 3ed57c2 | 2001-12-24 15:01:59 -0500 | [diff] [blame] | 1343 | cflag++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1344 | break; |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 1345 | case 'C': |
Theodore Ts'o | 2d34a25 | 2011-06-14 14:30:22 -0400 | [diff] [blame] | 1346 | cluster_size = strtoul(optarg, &tmp, 0); |
| 1347 | if (cluster_size < EXT2_MIN_CLUSTER_SIZE || |
| 1348 | cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1349 | com_err(program_name, 0, |
Eric Sandeen | 28e2cb9 | 2011-10-04 17:12:11 -0500 | [diff] [blame] | 1350 | _("invalid cluster size - %s"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1351 | optarg); |
| 1352 | exit(1); |
| 1353 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1354 | break; |
Theodore Ts'o | 37c8db7 | 2012-03-22 16:00:49 -0400 | [diff] [blame] | 1355 | case 'D': |
| 1356 | direct_io = 1; |
| 1357 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1358 | case 'g': |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1359 | fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1360 | if (*tmp) { |
| 1361 | com_err(program_name, 0, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1362 | _("Illegal number for blocks per group")); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1363 | exit(1); |
| 1364 | } |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1365 | if ((fs_param.s_blocks_per_group % 8) != 0) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1366 | com_err(program_name, 0, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1367 | _("blocks per group must be multiple of 8")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1368 | exit(1); |
| 1369 | } |
| 1370 | break; |
Theodore Ts'o | 9ba4000 | 2008-04-22 08:27:01 -0400 | [diff] [blame] | 1371 | case 'G': |
| 1372 | flex_bg_size = strtoul(optarg, &tmp, 0); |
| 1373 | if (*tmp) { |
| 1374 | com_err(program_name, 0, |
| 1375 | _("Illegal number for flex_bg size")); |
| 1376 | exit(1); |
| 1377 | } |
Theodore Ts'o | d531450 | 2010-05-10 21:49:58 -0400 | [diff] [blame] | 1378 | if (flex_bg_size < 1 || |
Theodore Ts'o | 9ba4000 | 2008-04-22 08:27:01 -0400 | [diff] [blame] | 1379 | (flex_bg_size & (flex_bg_size-1)) != 0) { |
| 1380 | com_err(program_name, 0, |
| 1381 | _("flex_bg size must be a power of 2")); |
| 1382 | exit(1); |
| 1383 | } |
| 1384 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1385 | case 'i': |
| 1386 | inode_ratio = strtoul(optarg, &tmp, 0); |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1387 | if (inode_ratio < EXT2_MIN_BLOCK_SIZE || |
| 1388 | inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 || |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1389 | *tmp) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1390 | com_err(program_name, 0, |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 1391 | _("invalid inode ratio %s (min %d/max %d)"), |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1392 | optarg, EXT2_MIN_BLOCK_SIZE, |
Tim Small | e447ba3 | 2010-07-30 20:43:11 -0400 | [diff] [blame] | 1393 | EXT2_MAX_BLOCK_SIZE * 1024); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1394 | exit(1); |
| 1395 | } |
| 1396 | break; |
Theodore Ts'o | dc2ec52 | 2001-01-18 01:51:15 +0000 | [diff] [blame] | 1397 | case 'J': |
| 1398 | parse_journal_opts(optarg); |
| 1399 | break; |
Eric Sandeen | 5827d24 | 2009-10-07 16:49:17 -0500 | [diff] [blame] | 1400 | case 'K': |
Lukas Czerner | 0bc85df | 2010-11-18 14:38:39 +0100 | [diff] [blame] | 1401 | fprintf(stderr, _("Warning: -K option is deprecated and " |
| 1402 | "should not be used anymore. Use " |
| 1403 | "\'-E nodiscard\' extended option " |
| 1404 | "instead!\n")); |
Eric Sandeen | 5827d24 | 2009-10-07 16:49:17 -0500 | [diff] [blame] | 1405 | discard = 0; |
| 1406 | break; |
Theodore Ts'o | 85ef4ae | 2000-10-24 19:33:45 +0000 | [diff] [blame] | 1407 | case 'j': |
Theodore Ts'o | dc2ec52 | 2001-01-18 01:51:15 +0000 | [diff] [blame] | 1408 | if (!journal_size) |
| 1409 | journal_size = -1; |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 1410 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1411 | case 'l': |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1412 | bad_blocks_filename = malloc(strlen(optarg)+1); |
| 1413 | if (!bad_blocks_filename) { |
| 1414 | com_err(program_name, ENOMEM, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1415 | _("in malloc for bad_blocks_filename")); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1416 | exit(1); |
| 1417 | } |
| 1418 | strcpy(bad_blocks_filename, optarg); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1419 | break; |
| 1420 | case 'm': |
Andreas Dilger | ce91114 | 2005-07-06 11:50:08 -0500 | [diff] [blame] | 1421 | reserved_ratio = strtod(optarg, &tmp); |
Theodore Ts'o | 8d82245 | 2009-03-06 02:23:59 -0500 | [diff] [blame] | 1422 | if ( *tmp || reserved_ratio > 50 || |
| 1423 | reserved_ratio < 0) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1424 | com_err(program_name, 0, |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 1425 | _("invalid reserved blocks percent - %s"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1426 | optarg); |
| 1427 | exit(1); |
| 1428 | } |
| 1429 | break; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 1430 | case 'n': |
| 1431 | noaction++; |
| 1432 | break; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 1433 | case 'o': |
| 1434 | creator_os = optarg; |
| 1435 | break; |
Andreas Dilger | 2524785 | 2005-07-06 12:58:15 -0500 | [diff] [blame] | 1436 | case 'q': |
| 1437 | quiet = 1; |
| 1438 | break; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 1439 | case 'r': |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1440 | r_opt = strtoul(optarg, &tmp, 0); |
Andreas Dilger | 2524785 | 2005-07-06 12:58:15 -0500 | [diff] [blame] | 1441 | if (*tmp) { |
| 1442 | com_err(program_name, 0, |
| 1443 | _("bad revision level - %s"), optarg); |
| 1444 | exit(1); |
| 1445 | } |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1446 | fs_param.s_rev_level = r_opt; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 1447 | break; |
Theodore Ts'o | b10fd5e | 2001-04-22 03:47:23 +0000 | [diff] [blame] | 1448 | case 's': /* deprecated */ |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1449 | s_opt = atoi(optarg); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 1450 | break; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 1451 | case 'I': |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1452 | inode_size = strtoul(optarg, &tmp, 0); |
| 1453 | if (*tmp) { |
| 1454 | com_err(program_name, 0, |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 1455 | _("invalid inode size - %s"), optarg); |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1456 | exit(1); |
| 1457 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 1458 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1459 | case 'v': |
| 1460 | verbose = 1; |
| 1461 | break; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 1462 | case 'F': |
Andreas Dilger | c16e610 | 2006-08-05 19:05:53 -0400 | [diff] [blame] | 1463 | force++; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 1464 | break; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 1465 | case 'L': |
| 1466 | volume_label = optarg; |
| 1467 | break; |
| 1468 | case 'M': |
| 1469 | mount_dir = optarg; |
| 1470 | break; |
Andreas Dilger | 2524785 | 2005-07-06 12:58:15 -0500 | [diff] [blame] | 1471 | case 'N': |
| 1472 | num_inodes = strtoul(optarg, &tmp, 0); |
| 1473 | if (*tmp) { |
| 1474 | com_err(program_name, 0, |
| 1475 | _("bad num inodes - %s"), optarg); |
| 1476 | exit(1); |
| 1477 | } |
| 1478 | break; |
Theodore Ts'o | 896938d | 1999-10-23 01:04:50 +0000 | [diff] [blame] | 1479 | case 'O': |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1480 | fs_features = optarg; |
Theodore Ts'o | 896938d | 1999-10-23 01:04:50 +0000 | [diff] [blame] | 1481 | break; |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 1482 | case 'E': |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 1483 | case 'R': |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 1484 | extended_opts = optarg; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 1485 | break; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1486 | case 'S': |
| 1487 | super_only = 1; |
| 1488 | break; |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1489 | case 't': |
Eric Sandeen | 9f7c3af | 2011-09-16 15:49:32 -0500 | [diff] [blame] | 1490 | if (fs_type) { |
| 1491 | com_err(program_name, 0, |
| 1492 | _("The -t option may only be used once")); |
| 1493 | exit(1); |
| 1494 | } |
Theodore Ts'o | 4c2b28a | 2011-06-14 14:17:56 -0400 | [diff] [blame] | 1495 | fs_type = strdup(optarg); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 1496 | break; |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1497 | case 'T': |
Eric Sandeen | 9f7c3af | 2011-09-16 15:49:32 -0500 | [diff] [blame] | 1498 | if (usage_types) { |
| 1499 | com_err(program_name, 0, |
| 1500 | _("The -T option may only be used once")); |
| 1501 | exit(1); |
| 1502 | } |
Theodore Ts'o | 4c2b28a | 2011-06-14 14:17:56 -0400 | [diff] [blame] | 1503 | usage_types = strdup(optarg); |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1504 | break; |
Theodore Ts'o | b0afdda | 2009-01-20 13:18:23 -0500 | [diff] [blame] | 1505 | case 'U': |
| 1506 | fs_uuid = optarg; |
| 1507 | break; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 1508 | case 'V': |
| 1509 | /* Print version number and exit */ |
Theodore Ts'o | 1e6e4c5 | 2003-12-07 02:28:24 -0500 | [diff] [blame] | 1510 | show_version_only++; |
| 1511 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1512 | default: |
| 1513 | usage(); |
| 1514 | } |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 1515 | } |
Theodore Ts'o | 1e6e4c5 | 2003-12-07 02:28:24 -0500 | [diff] [blame] | 1516 | if ((optind == argc) && !show_version_only) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1517 | usage(); |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 1518 | device_name = argv[optind++]; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1519 | |
Theodore Ts'o | 1e6e4c5 | 2003-12-07 02:28:24 -0500 | [diff] [blame] | 1520 | if (!quiet || show_version_only) |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1521 | fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION, |
Theodore Ts'o | 3879857 | 2003-04-16 15:29:39 -0400 | [diff] [blame] | 1522 | E2FSPROGS_DATE); |
| 1523 | |
Theodore Ts'o | 1e6e4c5 | 2003-12-07 02:28:24 -0500 | [diff] [blame] | 1524 | if (show_version_only) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1525 | fprintf(stderr, _("\tUsing %s\n"), |
Theodore Ts'o | 1e6e4c5 | 2003-12-07 02:28:24 -0500 | [diff] [blame] | 1526 | error_message(EXT2_ET_BASE)); |
| 1527 | exit(0); |
| 1528 | } |
| 1529 | |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1530 | /* |
| 1531 | * If there's no blocksize specified and there is a journal |
| 1532 | * device, use it to figure out the blocksize |
| 1533 | */ |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1534 | if (blocksize <= 0 && journal_device) { |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1535 | ext2_filsys jfs; |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 1536 | io_manager io_ptr; |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1537 | |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 1538 | #ifdef CONFIG_TESTIO_DEBUG |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 1539 | if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { |
| 1540 | io_ptr = test_io_manager; |
| 1541 | test_io_backing_manager = unix_io_manager; |
| 1542 | } else |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 1543 | #endif |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 1544 | io_ptr = unix_io_manager; |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1545 | retval = ext2fs_open(journal_device, |
| 1546 | EXT2_FLAG_JOURNAL_DEV_OK, 0, |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 1547 | 0, io_ptr, &jfs); |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1548 | if (retval) { |
| 1549 | com_err(program_name, retval, |
| 1550 | _("while trying to open journal device %s\n"), |
| 1551 | journal_device); |
| 1552 | exit(1); |
| 1553 | } |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 1554 | if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1555 | com_err(program_name, 0, |
Theodore Ts'o | ddc32a0 | 2003-05-03 18:45:55 -0400 | [diff] [blame] | 1556 | _("Journal dev blocksize (%d) smaller than " |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1557 | "minimum blocksize %d\n"), jfs->blocksize, |
| 1558 | -blocksize); |
| 1559 | exit(1); |
| 1560 | } |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1561 | blocksize = jfs->blocksize; |
Theodore Ts'o | f8df04b | 2008-07-06 20:57:17 -0400 | [diff] [blame] | 1562 | printf(_("Using journal device's blocksize: %d\n"), blocksize); |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1563 | fs_param.s_log_block_size = |
Theodore Ts'o | 77dc4eb | 2001-07-27 22:00:18 -0400 | [diff] [blame] | 1564 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 1565 | ext2fs_close(jfs); |
| 1566 | } |
Theodore Ts'o | dc2ec52 | 2001-01-18 01:51:15 +0000 | [diff] [blame] | 1567 | |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 1568 | if (optind < argc) { |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1569 | fs_blocks_count = parse_num_blocks2(argv[optind++], |
| 1570 | fs_param.s_log_block_size); |
| 1571 | if (!fs_blocks_count) { |
Eric Sandeen | 792cce3 | 2010-08-12 14:40:44 -0400 | [diff] [blame] | 1572 | com_err(program_name, 0, |
Theodore Ts'o | 9d92a20 | 2010-09-24 22:40:21 -0400 | [diff] [blame] | 1573 | _("invalid blocks '%s' on device '%s'"), |
Eric Sandeen | 792cce3 | 2010-08-12 14:40:44 -0400 | [diff] [blame] | 1574 | argv[optind - 1], device_name); |
Theodore Ts'o | 55f4cbd | 2005-01-05 03:01:06 -0500 | [diff] [blame] | 1575 | exit(1); |
| 1576 | } |
| 1577 | } |
| 1578 | if (optind < argc) |
| 1579 | usage(); |
| 1580 | |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 1581 | if (!force) |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 1582 | check_plausibility(device_name); |
Theodore Ts'o | 6398532 | 2001-01-03 17:02:13 +0000 | [diff] [blame] | 1583 | check_mount(device_name, force, _("filesystem")); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1584 | |
Theodore Ts'o | 9b27e9c | 2010-12-22 18:22:40 -0500 | [diff] [blame] | 1585 | /* Determine the size of the device (if possible) */ |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1586 | if (noaction && fs_blocks_count) { |
| 1587 | dev_size = fs_blocks_count; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 1588 | retval = 0; |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1589 | } else |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 1590 | retval = ext2fs_get_device_size2(device_name, |
| 1591 | EXT2_BLOCK_SIZE(&fs_param), |
| 1592 | &dev_size); |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 1593 | |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 1594 | if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { |
| 1595 | com_err(program_name, retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1596 | _("while trying to determine filesystem size")); |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 1597 | exit(1); |
| 1598 | } |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1599 | if (!fs_blocks_count) { |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 1600 | if (retval == EXT2_ET_UNIMPLEMENTED) { |
| 1601 | com_err(program_name, 0, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1602 | _("Couldn't determine device size; you " |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 1603 | "must specify\nthe size of the " |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1604 | "filesystem\n")); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1605 | exit(1); |
Theodore Ts'o | 26ab531 | 2000-05-29 15:05:42 +0000 | [diff] [blame] | 1606 | } else { |
| 1607 | if (dev_size == 0) { |
| 1608 | com_err(program_name, 0, |
| 1609 | _("Device size reported to be zero. " |
| 1610 | "Invalid partition specified, or\n\t" |
| 1611 | "partition table wasn't reread " |
| 1612 | "after running fdisk, due to\n\t" |
| 1613 | "a modified partition being busy " |
| 1614 | "and in use. You may need to reboot\n\t" |
| 1615 | "to re-read your partition table.\n" |
| 1616 | )); |
| 1617 | exit(1); |
| 1618 | } |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1619 | fs_blocks_count = dev_size; |
| 1620 | if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param)) |
| 1621 | fs_blocks_count &= ~((blk64_t) ((sys_page_size / |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1622 | EXT2_BLOCK_SIZE(&fs_param))-1)); |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1623 | } |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1624 | } else if (!force && (fs_blocks_count > dev_size)) { |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 1625 | com_err(program_name, 0, |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1626 | _("Filesystem larger than apparent device size.")); |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 1627 | proceed_question(); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1628 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1629 | |
Theodore Ts'o | 4c2b28a | 2011-06-14 14:17:56 -0400 | [diff] [blame] | 1630 | if (!fs_type) |
| 1631 | profile_get_string(profile, "devices", device_name, |
| 1632 | "fs_type", 0, &fs_type); |
| 1633 | if (!usage_types) |
| 1634 | profile_get_string(profile, "devices", device_name, |
| 1635 | "usage_types", 0, &usage_types); |
| 1636 | |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1637 | /* |
Theodore Ts'o | 9b27e9c | 2010-12-22 18:22:40 -0500 | [diff] [blame] | 1638 | * We have the file system (or device) size, so we can now |
| 1639 | * determine the appropriate file system types so the fs can |
| 1640 | * be appropriately configured. |
| 1641 | */ |
| 1642 | fs_types = parse_fs_type(fs_type, usage_types, &fs_param, |
| 1643 | fs_blocks_count ? fs_blocks_count : dev_size, |
| 1644 | argv[0]); |
| 1645 | if (!fs_types) { |
| 1646 | fprintf(stderr, _("Failed to parse fs types list\n")); |
| 1647 | exit(1); |
| 1648 | } |
| 1649 | |
| 1650 | /* Figure out what features should be enabled */ |
| 1651 | |
| 1652 | tmp = NULL; |
| 1653 | if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) { |
| 1654 | tmp = get_string_from_profile(fs_types, "base_features", |
| 1655 | "sparse_super,filetype,resize_inode,dir_index"); |
| 1656 | edit_feature(tmp, &fs_param.s_feature_compat); |
| 1657 | free(tmp); |
| 1658 | |
Eric Sandeen | 6a426c9 | 2011-02-17 15:56:17 -0600 | [diff] [blame] | 1659 | /* And which mount options as well */ |
| 1660 | tmp = get_string_from_profile(fs_types, "default_mntopts", |
| 1661 | "acl,user_xattr"); |
| 1662 | edit_mntopts(tmp, &fs_param.s_default_mount_opts); |
| 1663 | if (tmp) |
| 1664 | free(tmp); |
| 1665 | |
Theodore Ts'o | 9b27e9c | 2010-12-22 18:22:40 -0500 | [diff] [blame] | 1666 | for (cpp = fs_types; *cpp; cpp++) { |
| 1667 | tmp = NULL; |
| 1668 | profile_get_string(profile, "fs_types", *cpp, |
| 1669 | "features", "", &tmp); |
| 1670 | if (tmp && *tmp) |
| 1671 | edit_feature(tmp, &fs_param.s_feature_compat); |
| 1672 | if (tmp) |
| 1673 | free(tmp); |
| 1674 | } |
| 1675 | tmp = get_string_from_profile(fs_types, "default_features", |
| 1676 | ""); |
| 1677 | } |
| 1678 | edit_feature(fs_features ? fs_features : tmp, |
| 1679 | &fs_param.s_feature_compat); |
| 1680 | if (tmp) |
| 1681 | free(tmp); |
| 1682 | |
| 1683 | /* |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1684 | * We now need to do a sanity check of fs_blocks_count for |
| 1685 | * 32-bit vs 64-bit block number support. |
| 1686 | */ |
| 1687 | if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) { |
| 1688 | fs_blocks_count /= 4; /* Try using a 4k blocksize */ |
| 1689 | blocksize = 4096; |
| 1690 | fs_param.s_log_block_size = 2; |
| 1691 | } |
| 1692 | if ((fs_blocks_count > MAX_32_NUM) && |
| 1693 | !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) && |
| 1694 | get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) { |
| 1695 | fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT; |
| 1696 | fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE; |
| 1697 | } |
| 1698 | if ((fs_blocks_count > MAX_32_NUM) && |
| 1699 | !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) { |
| 1700 | fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s " |
| 1701 | "too big to be expressed\n\t" |
| 1702 | "in 32 bits using a blocksize of %d.\n"), |
| 1703 | program_name, fs_blocks_count, device_name, |
| 1704 | EXT2_BLOCK_SIZE(&fs_param)); |
| 1705 | exit(1); |
| 1706 | } |
| 1707 | |
| 1708 | ext2fs_blocks_count_set(&fs_param, fs_blocks_count); |
| 1709 | |
Theodore Ts'o | b4d5105 | 2008-07-06 20:24:29 -0400 | [diff] [blame] | 1710 | if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { |
| 1711 | fs_types[0] = strdup("journal"); |
| 1712 | fs_types[1] = 0; |
| 1713 | } |
| 1714 | |
| 1715 | if (verbose) { |
| 1716 | fputs(_("fs_types for mke2fs.conf resolution: "), stdout); |
| 1717 | print_str_list(fs_types); |
| 1718 | } |
| 1719 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1720 | if (r_opt == EXT2_GOOD_OLD_REV && |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 1721 | (fs_param.s_feature_compat || fs_param.s_feature_incompat || |
Theodore Ts'o | 92fb854 | 2008-07-18 21:08:56 -0400 | [diff] [blame] | 1722 | fs_param.s_feature_ro_compat)) { |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1723 | fprintf(stderr, _("Filesystem features not supported " |
| 1724 | "with revision 0 filesystems\n")); |
| 1725 | exit(1); |
| 1726 | } |
| 1727 | |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 1728 | if (s_opt > 0) { |
| 1729 | if (r_opt == EXT2_GOOD_OLD_REV) { |
| 1730 | fprintf(stderr, _("Sparse superblocks not supported " |
| 1731 | "with revision 0 filesystems\n")); |
| 1732 | exit(1); |
| 1733 | } |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1734 | fs_param.s_feature_ro_compat |= |
| 1735 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 1736 | } else if (s_opt == 0) |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1737 | fs_param.s_feature_ro_compat &= |
| 1738 | ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 1739 | |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 1740 | if (journal_size != 0) { |
| 1741 | if (r_opt == EXT2_GOOD_OLD_REV) { |
| 1742 | fprintf(stderr, _("Journals not supported " |
| 1743 | "with revision 0 filesystems\n")); |
| 1744 | exit(1); |
| 1745 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1746 | fs_param.s_feature_compat |= |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1747 | EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
Theodore Ts'o | b290d2d | 2006-10-18 00:31:11 -0400 | [diff] [blame] | 1748 | } |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1749 | |
Aditya Kali | d3859af | 2011-05-10 14:51:31 -0700 | [diff] [blame] | 1750 | /* Get reserved_ratio from profile if not specified on cmd line. */ |
| 1751 | if (reserved_ratio < 0.0) { |
| 1752 | reserved_ratio = get_double_from_profile( |
| 1753 | fs_types, "reserved_ratio", 5.0); |
| 1754 | if (reserved_ratio > 50 || reserved_ratio < 0) { |
| 1755 | com_err(program_name, 0, |
| 1756 | _("invalid reserved blocks percent - %lf"), |
| 1757 | reserved_ratio); |
| 1758 | exit(1); |
| 1759 | } |
| 1760 | } |
| 1761 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1762 | if (fs_param.s_feature_incompat & |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1763 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1764 | reserved_ratio = 0; |
| 1765 | fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV; |
| 1766 | fs_param.s_feature_compat = 0; |
| 1767 | fs_param.s_feature_ro_compat = 0; |
| 1768 | } |
Theodore Ts'o | d94cc2e | 2008-04-27 00:08:14 -0400 | [diff] [blame] | 1769 | |
| 1770 | if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && |
| 1771 | (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) { |
| 1772 | fprintf(stderr, _("The resize_inode and meta_bg features " |
| 1773 | "are not compatible.\n" |
| 1774 | "They can not be both enabled " |
| 1775 | "simultaneously.\n")); |
| 1776 | exit(1); |
| 1777 | } |
| 1778 | |
Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 1779 | /* Set first meta blockgroup via an environment variable */ |
| 1780 | /* (this is mostly for debugging purposes) */ |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1781 | if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && |
Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 1782 | ((tmp = getenv("MKE2FS_FIRST_META_BG")))) |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1783 | fs_param.s_first_meta_bg = atoi(tmp); |
Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 1784 | |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1785 | /* Get the hardware sector sizes, if available */ |
| 1786 | retval = ext2fs_get_device_sectsize(device_name, &lsector_size); |
Theodore Ts'o | 93d5c38 | 2003-05-21 17:28:29 -0400 | [diff] [blame] | 1787 | if (retval) { |
| 1788 | com_err(program_name, retval, |
| 1789 | _("while trying to determine hardware sector size")); |
| 1790 | exit(1); |
| 1791 | } |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1792 | retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size); |
| 1793 | if (retval) { |
| 1794 | com_err(program_name, retval, |
| 1795 | _("while trying to determine physical sector size")); |
| 1796 | exit(1); |
| 1797 | } |
Theodore Ts'o | 92eb5a3 | 2010-11-22 11:09:00 -0500 | [diff] [blame] | 1798 | |
| 1799 | if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) |
| 1800 | lsector_size = atoi(tmp); |
| 1801 | if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL) |
| 1802 | psector_size = atoi(tmp); |
| 1803 | |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1804 | /* Older kernels may not have physical/logical distinction */ |
| 1805 | if (!psector_size) |
| 1806 | psector_size = lsector_size; |
Theodore Ts'o | 1cca86f | 2003-09-01 09:28:18 -0400 | [diff] [blame] | 1807 | |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1808 | if (blocksize <= 0) { |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1809 | use_bsize = get_int_from_profile(fs_types, "blocksize", 4096); |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1810 | |
| 1811 | if (use_bsize == -1) { |
| 1812 | use_bsize = sys_page_size; |
| 1813 | if ((linux_version_code < (2*65536 + 6*256)) && |
| 1814 | (use_bsize > 4096)) |
| 1815 | use_bsize = 4096; |
| 1816 | } |
Theodore Ts'o | 2b21a0d | 2010-11-22 11:14:35 -0500 | [diff] [blame] | 1817 | if (lsector_size && use_bsize < lsector_size) |
| 1818 | use_bsize = lsector_size; |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1819 | if ((blocksize < 0) && (use_bsize < (-blocksize))) |
| 1820 | use_bsize = -blocksize; |
| 1821 | blocksize = use_bsize; |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1822 | ext2fs_blocks_count_set(&fs_param, |
| 1823 | ext2fs_blocks_count(&fs_param) / |
| 1824 | (blocksize / 1024)); |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1825 | } else { |
Theodore Ts'o | f89f54a | 2010-11-21 09:56:53 -0500 | [diff] [blame] | 1826 | if (blocksize < lsector_size) { /* Impossible */ |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1827 | com_err(program_name, EINVAL, |
| 1828 | _("while setting blocksize; too small " |
| 1829 | "for device\n")); |
| 1830 | exit(1); |
Theodore Ts'o | f89f54a | 2010-11-21 09:56:53 -0500 | [diff] [blame] | 1831 | } else if ((blocksize < psector_size) && |
| 1832 | (psector_size <= sys_page_size)) { /* Suboptimal */ |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1833 | fprintf(stderr, _("Warning: specified blocksize %d is " |
Theodore Ts'o | f89f54a | 2010-11-21 09:56:53 -0500 | [diff] [blame] | 1834 | "less than device physical sectorsize %d\n"), |
| 1835 | blocksize, psector_size); |
Theodore Ts'o | bb1158b | 2010-05-17 22:31:45 -0400 | [diff] [blame] | 1836 | } |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1837 | } |
| 1838 | |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 1839 | fs_param.s_log_block_size = |
| 1840 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 1841 | if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) { |
Theodore Ts'o | 2d34a25 | 2011-06-14 14:30:22 -0400 | [diff] [blame] | 1842 | if (!cluster_size) |
| 1843 | cluster_size = get_int_from_profile(fs_types, |
| 1844 | "cluster_size", |
Theodore Ts'o | b12a0bc | 2011-06-16 10:11:06 -0400 | [diff] [blame] | 1845 | blocksize*16); |
Theodore Ts'o | 2d34a25 | 2011-06-14 14:30:22 -0400 | [diff] [blame] | 1846 | fs_param.s_log_cluster_size = |
| 1847 | int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE); |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 1848 | } else |
| 1849 | fs_param.s_log_cluster_size = fs_param.s_log_block_size; |
| 1850 | |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1851 | if (inode_ratio == 0) { |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1852 | inode_ratio = get_int_from_profile(fs_types, "inode_ratio", |
| 1853 | 8192); |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1854 | if (inode_ratio < blocksize) |
| 1855 | inode_ratio = blocksize; |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 1856 | if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param)) |
| 1857 | inode_ratio = EXT2_CLUSTER_SIZE(&fs_param); |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 1858 | } |
| 1859 | |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1860 | #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY |
Theodore Ts'o | 1599b47 | 2010-11-22 10:50:42 -0500 | [diff] [blame] | 1861 | retval = get_device_geometry(device_name, &fs_param, psector_size); |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1862 | if (retval < 0) { |
| 1863 | fprintf(stderr, |
Eric Sandeen | 13b0b12 | 2010-01-23 21:50:45 -0600 | [diff] [blame] | 1864 | _("warning: Unable to get device geometry for %s\n"), |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1865 | device_name); |
| 1866 | } else if (retval) { |
| 1867 | printf(_("%s alignment is offset by %lu bytes.\n"), |
| 1868 | device_name, retval); |
| 1869 | printf(_("This may result in very poor performance, " |
| 1870 | "(re)-partitioning suggested.\n")); |
Eric Sandeen | 9ed8e5f | 2009-10-02 11:32:42 -0500 | [diff] [blame] | 1871 | } |
| 1872 | #endif |
| 1873 | |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1874 | blocksize = EXT2_BLOCK_SIZE(&fs_param); |
Theodore Ts'o | a4396e9 | 2008-04-18 10:19:27 -0400 | [diff] [blame] | 1875 | |
Yury V. Zaytsev | 45792c1 | 2011-09-15 23:08:52 -0400 | [diff] [blame] | 1876 | /* This check should happen beyond the last assignment to blocksize */ |
| 1877 | if (blocksize > sys_page_size) { |
| 1878 | if (!force) { |
| 1879 | com_err(program_name, 0, |
| 1880 | _("%d-byte blocks too big for system (max %d)"), |
| 1881 | blocksize, sys_page_size); |
| 1882 | proceed_question(); |
| 1883 | } |
| 1884 | fprintf(stderr, _("Warning: %d-byte blocks too big for system " |
| 1885 | "(max %d), forced to continue\n"), |
| 1886 | blocksize, sys_page_size); |
| 1887 | } |
| 1888 | |
Theodore Ts'o | 210fd2c | 2010-10-01 10:47:38 -0400 | [diff] [blame] | 1889 | lazy_itable_init = 0; |
| 1890 | if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0) |
| 1891 | lazy_itable_init = 1; |
| 1892 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1893 | lazy_itable_init = get_bool_from_profile(fs_types, |
Theodore Ts'o | 210fd2c | 2010-10-01 10:47:38 -0400 | [diff] [blame] | 1894 | "lazy_itable_init", |
| 1895 | lazy_itable_init); |
Lukas Czerner | 7fe5ff3 | 2010-11-18 14:38:41 +0100 | [diff] [blame] | 1896 | discard = get_bool_from_profile(fs_types, "discard" , discard); |
Andreas Dilger | 6c54689 | 2011-06-11 12:17:29 -0400 | [diff] [blame] | 1897 | journal_flags |= get_bool_from_profile(fs_types, |
| 1898 | "lazy_journal_init", 0) ? |
| 1899 | EXT2_MKJOURNAL_LAZYINIT : 0; |
Theodore Ts'o | 304e11c | 2012-04-05 12:30:02 -0700 | [diff] [blame] | 1900 | journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1901 | |
Theodore Ts'o | 2d36358 | 2008-05-14 18:09:37 -0400 | [diff] [blame] | 1902 | /* Get options from profile */ |
| 1903 | for (cpp = fs_types; *cpp; cpp++) { |
| 1904 | tmp = NULL; |
| 1905 | profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp); |
| 1906 | if (tmp && *tmp) |
| 1907 | parse_extended_opts(&fs_param, tmp); |
Jim Meyering | 45e338f | 2009-02-23 18:07:50 +0100 | [diff] [blame] | 1908 | free(tmp); |
Theodore Ts'o | 2d36358 | 2008-05-14 18:09:37 -0400 | [diff] [blame] | 1909 | } |
| 1910 | |
Theodore Ts'o | c6a4413 | 2005-01-05 11:12:20 -0500 | [diff] [blame] | 1911 | if (extended_opts) |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1912 | parse_extended_opts(&fs_param, extended_opts); |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 1913 | |
| 1914 | /* Since sparse_super is the default, we would only have a problem |
| 1915 | * here if it was explicitly disabled. |
| 1916 | */ |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1917 | if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) && |
| 1918 | !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) { |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 1919 | com_err(program_name, 0, |
| 1920 | _("reserved online resize blocks not supported " |
| 1921 | "on non-sparse filesystem")); |
| 1922 | exit(1); |
| 1923 | } |
| 1924 | |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1925 | if (fs_param.s_blocks_per_group) { |
| 1926 | if (fs_param.s_blocks_per_group < 256 || |
| 1927 | fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) { |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 1928 | com_err(program_name, 0, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1929 | _("blocks per group count out of range")); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 1930 | exit(1); |
| 1931 | } |
| 1932 | } |
| 1933 | |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 1934 | if (inode_size == 0) |
| 1935 | inode_size = get_int_from_profile(fs_types, "inode_size", 0); |
Theodore Ts'o | 9ba4000 | 2008-04-22 08:27:01 -0400 | [diff] [blame] | 1936 | if (!flex_bg_size && (fs_param.s_feature_incompat & |
| 1937 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) |
| 1938 | flex_bg_size = get_int_from_profile(fs_types, |
| 1939 | "flex_bg_size", 16); |
| 1940 | if (flex_bg_size) { |
| 1941 | if (!(fs_param.s_feature_incompat & |
| 1942 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) { |
| 1943 | com_err(program_name, 0, |
| 1944 | _("Flex_bg feature not enabled, so " |
| 1945 | "flex_bg size may not be specified")); |
| 1946 | exit(1); |
| 1947 | } |
| 1948 | fs_param.s_log_groups_per_flex = int_log2(flex_bg_size); |
| 1949 | } |
Andreas Dilger | 067911a | 2006-07-15 22:08:20 -0400 | [diff] [blame] | 1950 | |
| 1951 | if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) { |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1952 | if (inode_size < EXT2_GOOD_OLD_INODE_SIZE || |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1953 | inode_size > EXT2_BLOCK_SIZE(&fs_param) || |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1954 | inode_size & (inode_size - 1)) { |
| 1955 | com_err(program_name, 0, |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 1956 | _("invalid inode size %d (min %d/max %d)"), |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1957 | inode_size, EXT2_GOOD_OLD_INODE_SIZE, |
Theodore Ts'o | c5290fa | 2003-04-11 22:10:50 -0400 | [diff] [blame] | 1958 | blocksize); |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1959 | exit(1); |
| 1960 | } |
Theodore Ts'o | 9b9a780 | 2005-12-10 21:50:30 -0500 | [diff] [blame] | 1961 | fs_param.s_inode_size = inode_size; |
Andreas Dilger | 932a489 | 2002-05-16 03:20:07 -0600 | [diff] [blame] | 1962 | } |
| 1963 | |
Eric Sandeen | f335864 | 2006-09-12 14:56:17 -0400 | [diff] [blame] | 1964 | /* Make sure number of inodes specified will fit in 32 bits */ |
| 1965 | if (num_inodes == 0) { |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 1966 | unsigned long long n; |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1967 | n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio; |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1968 | if (n > MAX_32_NUM) { |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 1969 | if (fs_param.s_feature_incompat & |
| 1970 | EXT4_FEATURE_INCOMPAT_64BIT) |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1971 | num_inodes = MAX_32_NUM; |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 1972 | else { |
| 1973 | com_err(program_name, 0, |
Theodore Ts'o | 84888e5 | 2011-10-08 13:32:00 -0400 | [diff] [blame] | 1974 | _("too many inodes (%llu), raise " |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 1975 | "inode ratio?"), n); |
| 1976 | exit(1); |
| 1977 | } |
Eric Sandeen | f335864 | 2006-09-12 14:56:17 -0400 | [diff] [blame] | 1978 | } |
Theodore Ts'o | 493024e | 2010-06-13 14:00:00 -0400 | [diff] [blame] | 1979 | } else if (num_inodes > MAX_32_NUM) { |
Eric Sandeen | f335864 | 2006-09-12 14:56:17 -0400 | [diff] [blame] | 1980 | com_err(program_name, 0, |
| 1981 | _("too many inodes (%llu), specify < 2^32 inodes"), |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 1982 | num_inodes); |
Eric Sandeen | f335864 | 2006-09-12 14:56:17 -0400 | [diff] [blame] | 1983 | exit(1); |
| 1984 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1985 | /* |
| 1986 | * Calculate number of inodes based on the inode ratio |
| 1987 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 1988 | fs_param.s_inodes_count = num_inodes ? num_inodes : |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1989 | (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1990 | |
Andreas Dilger | dcf7b09 | 2007-05-22 16:04:51 -0400 | [diff] [blame] | 1991 | if ((((long long)fs_param.s_inodes_count) * |
| 1992 | (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >= |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1993 | ((ext2fs_blocks_count(&fs_param)) * |
Andreas Dilger | dcf7b09 | 2007-05-22 16:04:51 -0400 | [diff] [blame] | 1994 | EXT2_BLOCK_SIZE(&fs_param))) { |
| 1995 | com_err(program_name, 0, _("inode_size (%u) * inodes_count " |
| 1996 | "(%u) too big for a\n\t" |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 1997 | "filesystem with %llu blocks, " |
Andreas Dilger | dcf7b09 | 2007-05-22 16:04:51 -0400 | [diff] [blame] | 1998 | "specify higher inode_ratio (-i)\n\t" |
| 1999 | "or lower inode count (-N).\n"), |
| 2000 | inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2001 | fs_param.s_inodes_count, |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 2002 | (unsigned long long) ext2fs_blocks_count(&fs_param)); |
Andreas Dilger | dcf7b09 | 2007-05-22 16:04:51 -0400 | [diff] [blame] | 2003 | exit(1); |
| 2004 | } |
| 2005 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2006 | /* |
| 2007 | * Calculate number of blocks to reserve |
| 2008 | */ |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 2009 | ext2fs_r_blocks_count_set(&fs_param, reserved_ratio * |
| 2010 | ext2fs_blocks_count(&fs_param) / 100.0); |
Theodore Ts'o | 4c2b28a | 2011-06-14 14:17:56 -0400 | [diff] [blame] | 2011 | |
| 2012 | free(fs_type); |
| 2013 | free(usage_types); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2014 | } |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 2015 | |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2016 | static int should_do_undo(const char *name) |
| 2017 | { |
| 2018 | errcode_t retval; |
| 2019 | io_channel channel; |
| 2020 | __u16 s_magic; |
| 2021 | struct ext2_super_block super; |
| 2022 | io_manager manager = unix_io_manager; |
| 2023 | int csum_flag, force_undo; |
| 2024 | |
| 2025 | csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param, |
| 2026 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM); |
| 2027 | force_undo = get_int_from_profile(fs_types, "force_undo", 0); |
| 2028 | if (!force_undo && (!csum_flag || !lazy_itable_init)) |
| 2029 | return 0; |
| 2030 | |
| 2031 | retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel); |
| 2032 | if (retval) { |
| 2033 | /* |
| 2034 | * We don't handle error cases instead we |
| 2035 | * declare that the file system doesn't exist |
| 2036 | * and let the rest of mke2fs take care of |
| 2037 | * error |
| 2038 | */ |
| 2039 | retval = 0; |
| 2040 | goto open_err_out; |
| 2041 | } |
| 2042 | |
| 2043 | io_channel_set_blksize(channel, SUPERBLOCK_OFFSET); |
Valerie Aurora Henson | 24a117a | 2009-09-07 21:14:24 -0400 | [diff] [blame] | 2044 | retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super); |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2045 | if (retval) { |
| 2046 | retval = 0; |
| 2047 | goto err_out; |
| 2048 | } |
| 2049 | |
| 2050 | #if defined(WORDS_BIGENDIAN) |
| 2051 | s_magic = ext2fs_swab16(super.s_magic); |
| 2052 | #else |
| 2053 | s_magic = super.s_magic; |
| 2054 | #endif |
| 2055 | |
| 2056 | if (s_magic == EXT2_SUPER_MAGIC) |
| 2057 | retval = 1; |
| 2058 | |
| 2059 | err_out: |
| 2060 | io_channel_close(channel); |
| 2061 | |
| 2062 | open_err_out: |
| 2063 | |
| 2064 | return retval; |
| 2065 | } |
| 2066 | |
| 2067 | static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr) |
| 2068 | { |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2069 | errcode_t retval = ENOMEM; |
Eric Sandeen | 30295f1 | 2011-09-16 15:49:40 -0500 | [diff] [blame] | 2070 | char *tdb_dir = NULL, *tdb_file = NULL; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2071 | char *device_name, *tmp_name; |
Eric Sandeen | 30295f1 | 2011-09-16 15:49:40 -0500 | [diff] [blame] | 2072 | int free_tdb_dir = 0; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2073 | |
| 2074 | /* |
| 2075 | * Configuration via a conf file would be |
| 2076 | * nice |
| 2077 | */ |
| 2078 | tdb_dir = getenv("E2FSPROGS_UNDO_DIR"); |
Eric Sandeen | 30295f1 | 2011-09-16 15:49:40 -0500 | [diff] [blame] | 2079 | if (!tdb_dir) { |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2080 | profile_get_string(profile, "defaults", |
| 2081 | "undo_dir", 0, "/var/lib/e2fsprogs", |
| 2082 | &tdb_dir); |
Eric Sandeen | 30295f1 | 2011-09-16 15:49:40 -0500 | [diff] [blame] | 2083 | free_tdb_dir = 1; |
| 2084 | } |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2085 | |
| 2086 | if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) || |
| 2087 | access(tdb_dir, W_OK)) |
| 2088 | return 0; |
| 2089 | |
| 2090 | tmp_name = strdup(name); |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2091 | if (!tmp_name) |
| 2092 | goto errout; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2093 | device_name = basename(tmp_name); |
Theodore Ts'o | f203bbd | 2009-04-18 10:53:32 -0400 | [diff] [blame] | 2094 | tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1); |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2095 | if (!tdb_file) { |
| 2096 | free(tmp_name); |
| 2097 | goto errout; |
| 2098 | } |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2099 | sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name); |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2100 | free(tmp_name); |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2101 | |
| 2102 | if (!access(tdb_file, F_OK)) { |
| 2103 | if (unlink(tdb_file) < 0) { |
| 2104 | retval = errno; |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2105 | goto errout; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | set_undo_io_backing_manager(*io_ptr); |
| 2110 | *io_ptr = undo_io_manager; |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2111 | retval = set_undo_io_backup_file(tdb_file); |
| 2112 | if (retval) |
| 2113 | goto errout; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2114 | printf(_("Overwriting existing filesystem; this can be undone " |
| 2115 | "using the command:\n" |
| 2116 | " e2undo %s %s\n\n"), tdb_file, name); |
Eric Sandeen | 79e6240 | 2008-07-06 18:36:56 -0400 | [diff] [blame] | 2117 | |
Eric Sandeen | 30295f1 | 2011-09-16 15:49:40 -0500 | [diff] [blame] | 2118 | if (free_tdb_dir) |
| 2119 | free(tdb_dir); |
Theodore Ts'o | f203bbd | 2009-04-18 10:53:32 -0400 | [diff] [blame] | 2120 | free(tdb_file); |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2121 | return 0; |
| 2122 | |
| 2123 | errout: |
Eric Sandeen | 30295f1 | 2011-09-16 15:49:40 -0500 | [diff] [blame] | 2124 | if (free_tdb_dir) |
| 2125 | free(tdb_dir); |
Theodore Ts'o | 8a1cf3c | 2010-12-21 21:57:02 -0500 | [diff] [blame] | 2126 | free(tdb_file); |
| 2127 | com_err(program_name, retval, |
| 2128 | _("while trying to setup undo file\n")); |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2129 | return retval; |
| 2130 | } |
| 2131 | |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 2132 | static int mke2fs_discard_device(ext2_filsys fs) |
| 2133 | { |
| 2134 | struct ext2fs_numeric_progress_struct progress; |
| 2135 | blk64_t blocks = ext2fs_blocks_count(fs->super); |
| 2136 | blk64_t count = DISCARD_STEP_MB; |
Lukas Czerner | d2bfdc7 | 2011-09-15 23:44:59 -0400 | [diff] [blame] | 2137 | blk64_t cur; |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 2138 | int retval = 0; |
| 2139 | |
Lukas Czerner | d2bfdc7 | 2011-09-15 23:44:59 -0400 | [diff] [blame] | 2140 | /* |
| 2141 | * Let's try if discard really works on the device, so |
| 2142 | * we do not print numeric progress resulting in failure |
| 2143 | * afterwards. |
| 2144 | */ |
| 2145 | retval = io_channel_discard(fs->io, 0, fs->blocksize); |
Theodore Ts'o | aa07cb7 | 2011-02-27 20:09:54 -0500 | [diff] [blame] | 2146 | if (retval) |
| 2147 | return retval; |
Lukas Czerner | d2bfdc7 | 2011-09-15 23:44:59 -0400 | [diff] [blame] | 2148 | cur = fs->blocksize; |
Theodore Ts'o | aa07cb7 | 2011-02-27 20:09:54 -0500 | [diff] [blame] | 2149 | |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 2150 | count *= (1024 * 1024); |
| 2151 | count /= fs->blocksize; |
| 2152 | |
| 2153 | ext2fs_numeric_progress_init(fs, &progress, |
| 2154 | _("Discarding device blocks: "), |
| 2155 | blocks); |
| 2156 | while (cur < blocks) { |
Theodore Ts'o | c498cb1 | 2012-09-22 21:26:48 -0400 | [diff] [blame^] | 2157 | ext2fs_numeric_progress_update(fs, &progress, cur); |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 2158 | |
| 2159 | if (cur + count > blocks) |
| 2160 | count = blocks - cur; |
| 2161 | |
Theodore Ts'o | aa07cb7 | 2011-02-27 20:09:54 -0500 | [diff] [blame] | 2162 | retval = io_channel_discard(fs->io, cur, count); |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 2163 | if (retval) |
| 2164 | break; |
| 2165 | cur += count; |
| 2166 | } |
| 2167 | |
| 2168 | if (retval) { |
| 2169 | ext2fs_numeric_progress_close(fs, &progress, |
| 2170 | _("failed - ")); |
| 2171 | if (!quiet) |
| 2172 | printf("%s\n",error_message(retval)); |
| 2173 | } else |
| 2174 | ext2fs_numeric_progress_close(fs, &progress, |
| 2175 | _("done \n")); |
| 2176 | |
| 2177 | return retval; |
| 2178 | } |
| 2179 | |
Andreas Dilger | 96367ad | 2011-06-15 22:17:38 -0400 | [diff] [blame] | 2180 | static void fix_cluster_bg_counts(ext2_filsys fs) |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 2181 | { |
| 2182 | blk64_t cluster, num_clusters, tot_free; |
| 2183 | int grp_free, num_free, group, num; |
| 2184 | |
| 2185 | num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)); |
| 2186 | tot_free = num_free = num = group = grp_free = 0; |
| 2187 | for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block); |
| 2188 | cluster < num_clusters; cluster++) { |
| 2189 | if (!ext2fs_test_block_bitmap2(fs->block_map, |
| 2190 | EXT2FS_C2B(fs, cluster))) { |
| 2191 | grp_free++; |
| 2192 | tot_free++; |
| 2193 | } |
| 2194 | num++; |
| 2195 | if ((num == fs->super->s_clusters_per_group) || |
| 2196 | (cluster == num_clusters-1)) { |
| 2197 | ext2fs_bg_free_blocks_count_set(fs, group, grp_free); |
| 2198 | ext2fs_group_desc_csum_set(fs, group); |
| 2199 | num = 0; |
| 2200 | grp_free = 0; |
| 2201 | group++; |
| 2202 | } |
| 2203 | } |
Theodore Ts'o | fe75afb | 2011-06-16 01:38:43 -0400 | [diff] [blame] | 2204 | ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free)); |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 2205 | } |
| 2206 | |
Aditya Kali | 1f5d7a8 | 2011-07-20 11:40:04 -0700 | [diff] [blame] | 2207 | static int create_quota_inodes(ext2_filsys fs) |
| 2208 | { |
| 2209 | quota_ctx_t qctx; |
| 2210 | |
Aditya Kali | a86d55d | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 2211 | quota_init_context(&qctx, fs, -1); |
| 2212 | quota_compute_usage(qctx); |
Aditya Kali | d678fef | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 2213 | quota_write_inode(qctx, quotatype); |
Aditya Kali | a86d55d | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 2214 | quota_release_context(&qctx); |
Aditya Kali | 1f5d7a8 | 2011-07-20 11:40:04 -0700 | [diff] [blame] | 2215 | |
Aditya Kali | a86d55d | 2011-11-14 10:55:54 -0500 | [diff] [blame] | 2216 | return 0; |
Aditya Kali | 1f5d7a8 | 2011-07-20 11:40:04 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2219 | int main (int argc, char *argv[]) |
| 2220 | { |
| 2221 | errcode_t retval = 0; |
| 2222 | ext2_filsys fs; |
| 2223 | badblocks_list bb_list = 0; |
Theodore Ts'o | d4e0b1c | 2007-08-03 18:56:01 -0400 | [diff] [blame] | 2224 | unsigned int journal_blocks; |
Theodore Ts'o | 14b283a | 2011-09-28 22:45:12 -0400 | [diff] [blame] | 2225 | unsigned int i, checkinterval; |
| 2226 | int max_mnt_count; |
Theodore Ts'o | d5f57d9 | 2008-08-29 21:39:36 -0400 | [diff] [blame] | 2227 | int val, hash_alg; |
Valerie Aurora Henson | 463e732 | 2009-08-05 00:17:56 -0400 | [diff] [blame] | 2228 | int flags; |
| 2229 | int old_bitmaps; |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 2230 | io_manager io_ptr; |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2231 | char tdb_string[40]; |
Theodore Ts'o | d5f57d9 | 2008-08-29 21:39:36 -0400 | [diff] [blame] | 2232 | char *hash_alg_str; |
Eric Sandeen | 6fcd6f8 | 2010-08-20 16:41:14 -0500 | [diff] [blame] | 2233 | int itable_zeroed = 0; |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2234 | |
| 2235 | #ifdef ENABLE_NLS |
| 2236 | setlocale(LC_MESSAGES, ""); |
Theodore Ts'o | 14308a5 | 2002-03-05 03:26:52 -0500 | [diff] [blame] | 2237 | setlocale(LC_CTYPE, ""); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2238 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 2239 | textdomain(NLS_CAT_NAME); |
Theodore Ts'o | 9d4507c | 2011-10-05 01:00:30 -0400 | [diff] [blame] | 2240 | set_com_err_gettext(gettext); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2241 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2242 | PRS(argc, argv); |
| 2243 | |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 2244 | #ifdef CONFIG_TESTIO_DEBUG |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 2245 | if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { |
| 2246 | io_ptr = test_io_manager; |
| 2247 | test_io_backing_manager = unix_io_manager; |
| 2248 | } else |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 2249 | #endif |
Theodore Ts'o | f38cf3c | 2008-09-01 11:17:29 -0400 | [diff] [blame] | 2250 | io_ptr = unix_io_manager; |
Theodore Ts'o | a7ccdff | 2003-07-08 18:03:48 -0400 | [diff] [blame] | 2251 | |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2252 | if (should_do_undo(device_name)) { |
| 2253 | retval = mke2fs_setup_tdb(device_name, &io_ptr); |
| 2254 | if (retval) |
| 2255 | exit(1); |
| 2256 | } |
| 2257 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2258 | /* |
| 2259 | * Initialize the superblock.... |
| 2260 | */ |
Valerie Aurora Henson | 463e732 | 2009-08-05 00:17:56 -0400 | [diff] [blame] | 2261 | flags = EXT2_FLAG_EXCLUSIVE; |
Theodore Ts'o | 37c8db7 | 2012-03-22 16:00:49 -0400 | [diff] [blame] | 2262 | if (direct_io) |
| 2263 | flags |= EXT2_FLAG_DIRECT_IO; |
Valerie Aurora Henson | 463e732 | 2009-08-05 00:17:56 -0400 | [diff] [blame] | 2264 | profile_get_boolean(profile, "options", "old_bitmaps", 0, 0, |
| 2265 | &old_bitmaps); |
| 2266 | if (!old_bitmaps) |
| 2267 | flags |= EXT2_FLAG_64BITS; |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 2268 | /* |
| 2269 | * By default, we print how many inode tables or block groups |
| 2270 | * or whatever we've written so far. The quiet flag disables |
| 2271 | * this, along with a lot of other output. |
| 2272 | */ |
| 2273 | if (!quiet) |
| 2274 | flags |= EXT2_FLAG_PRINT_PROGRESS; |
Valerie Aurora Henson | 463e732 | 2009-08-05 00:17:56 -0400 | [diff] [blame] | 2275 | retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2276 | if (retval) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2277 | com_err(device_name, retval, _("while setting up superblock")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2278 | exit(1); |
| 2279 | } |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 2280 | |
Eric Sandeen | 5827d24 | 2009-10-07 16:49:17 -0500 | [diff] [blame] | 2281 | /* Can't undo discard ... */ |
Andreas Dilger | 8185ab9 | 2011-06-07 10:22:29 -0600 | [diff] [blame] | 2282 | if (!noaction && discard && (io_ptr != undo_io_manager)) { |
Lukas Czerner | 7d9e316 | 2011-01-24 20:52:00 +0100 | [diff] [blame] | 2283 | retval = mke2fs_discard_device(fs); |
Lukas Czerner | d866599 | 2010-11-18 03:38:37 +0000 | [diff] [blame] | 2284 | if (!retval && io_channel_discard_zeroes_data(fs->io)) { |
Eric Sandeen | 6fcd6f8 | 2010-08-20 16:41:14 -0500 | [diff] [blame] | 2285 | if (verbose) |
| 2286 | printf(_("Discard succeeded and will return 0s " |
| 2287 | " - skipping inode table wipe\n")); |
| 2288 | lazy_itable_init = 1; |
| 2289 | itable_zeroed = 1; |
| 2290 | } |
| 2291 | } |
Eric Sandeen | 5827d24 | 2009-10-07 16:49:17 -0500 | [diff] [blame] | 2292 | |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2293 | sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ? |
| 2294 | 32768 : fs->blocksize * 8); |
| 2295 | io_channel_set_options(fs->io, tdb_string); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2296 | |
Theodore Ts'o | 6cb2740 | 2008-01-26 19:06:35 -0500 | [diff] [blame] | 2297 | if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS) |
| 2298 | fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS; |
| 2299 | |
Theodore Ts'o | b7c5b40 | 2009-03-05 19:40:20 -0500 | [diff] [blame] | 2300 | if ((fs_param.s_feature_incompat & |
| 2301 | (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) || |
| 2302 | (fs_param.s_feature_ro_compat & |
| 2303 | (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM| |
| 2304 | EXT4_FEATURE_RO_COMPAT_DIR_NLINK| |
| 2305 | EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE))) |
| 2306 | fs->super->s_kbytes_written = 1; |
| 2307 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2308 | /* |
Theodore Ts'o | e41784e | 2000-08-14 14:44:15 +0000 | [diff] [blame] | 2309 | * Wipe out the old on-disk superblock |
| 2310 | */ |
Theodore Ts'o | 04a9685 | 2001-08-30 21:55:26 -0400 | [diff] [blame] | 2311 | if (!noaction) |
| 2312 | zap_sector(fs, 2, 6); |
Theodore Ts'o | e41784e | 2000-08-14 14:44:15 +0000 | [diff] [blame] | 2313 | |
| 2314 | /* |
Theodore Ts'o | b0afdda | 2009-01-20 13:18:23 -0500 | [diff] [blame] | 2315 | * Parse or generate a UUID for the filesystem |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2316 | */ |
Theodore Ts'o | b0afdda | 2009-01-20 13:18:23 -0500 | [diff] [blame] | 2317 | if (fs_uuid) { |
| 2318 | if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) { |
| 2319 | com_err(device_name, 0, "could not parse UUID: %s\n", |
| 2320 | fs_uuid); |
| 2321 | exit(1); |
| 2322 | } |
| 2323 | } else |
| 2324 | uuid_generate(fs->super->s_uuid); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2325 | |
| 2326 | /* |
Theodore Ts'o | 843049c | 2002-09-22 15:37:40 -0400 | [diff] [blame] | 2327 | * Initialize the directory index variables |
| 2328 | */ |
Theodore Ts'o | d5f57d9 | 2008-08-29 21:39:36 -0400 | [diff] [blame] | 2329 | hash_alg_str = get_string_from_profile(fs_types, "hash_alg", |
| 2330 | "half_md4"); |
| 2331 | hash_alg = e2p_string2hash(hash_alg_str); |
Theodore Ts'o | 5a2db04 | 2010-12-01 18:49:26 -0500 | [diff] [blame] | 2332 | free(hash_alg_str); |
Theodore Ts'o | d5f57d9 | 2008-08-29 21:39:36 -0400 | [diff] [blame] | 2333 | fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg : |
| 2334 | EXT2_HASH_HALF_MD4; |
Theodore Ts'o | 843049c | 2002-09-22 15:37:40 -0400 | [diff] [blame] | 2335 | uuid_generate((unsigned char *) fs->super->s_hash_seed); |
| 2336 | |
| 2337 | /* |
Eric Sandeen | 3daf592 | 2011-02-17 15:55:15 -0600 | [diff] [blame] | 2338 | * Periodic checks can be enabled/disabled via config file. |
| 2339 | * Note we override the kernel include file's idea of what the default |
| 2340 | * check interval (never) should be. It's a good idea to check at |
| 2341 | * least *occasionally*, specially since servers will never rarely get |
| 2342 | * to reboot, since Linux is so robust these days. :-) |
| 2343 | * |
| 2344 | * 180 days (six months) seems like a good value. |
Theodore Ts'o | 44c09c0 | 2001-01-14 17:02:09 +0000 | [diff] [blame] | 2345 | */ |
Eric Sandeen | 3daf592 | 2011-02-17 15:55:15 -0600 | [diff] [blame] | 2346 | #ifdef EXT2_DFL_CHECKINTERVAL |
| 2347 | #undef EXT2_DFL_CHECKINTERVAL |
| 2348 | #endif |
| 2349 | #define EXT2_DFL_CHECKINTERVAL (86400L * 180L) |
| 2350 | |
| 2351 | if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) { |
| 2352 | fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL; |
| 2353 | fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT; |
| 2354 | /* |
| 2355 | * Add "jitter" to the superblock's check interval so that we |
| 2356 | * don't check all the filesystems at the same time. We use a |
| 2357 | * kludgy hack of using the UUID to derive a random jitter value |
| 2358 | */ |
| 2359 | for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++) |
| 2360 | val += fs->super->s_uuid[i]; |
| 2361 | fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT; |
Theodore Ts'o | 14b283a | 2011-09-28 22:45:12 -0400 | [diff] [blame] | 2362 | } else |
| 2363 | fs->super->s_max_mnt_count = -1; |
Theodore Ts'o | 44c09c0 | 2001-01-14 17:02:09 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2366 | * Override the creator OS, if applicable |
| 2367 | */ |
| 2368 | if (creator_os && !set_os(fs->super, creator_os)) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2369 | com_err (program_name, 0, _("unknown os - %s"), creator_os); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2370 | exit(1); |
| 2371 | } |
| 2372 | |
| 2373 | /* |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 2374 | * For the Hurd, we will turn off filetype since it doesn't |
| 2375 | * support it. |
| 2376 | */ |
| 2377 | if (fs->super->s_creator_os == EXT2_OS_HURD) |
| 2378 | fs->super->s_feature_incompat &= |
| 2379 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 2380 | |
| 2381 | /* |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2382 | * Set the volume label... |
| 2383 | */ |
| 2384 | if (volume_label) { |
Theodore Ts'o | ef9abe5 | 2001-01-01 15:31:53 +0000 | [diff] [blame] | 2385 | memset(fs->super->s_volume_name, 0, |
| 2386 | sizeof(fs->super->s_volume_name)); |
| 2387 | strncpy(fs->super->s_volume_name, volume_label, |
| 2388 | sizeof(fs->super->s_volume_name)); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Set the last mount directory |
| 2393 | */ |
| 2394 | if (mount_dir) { |
Theodore Ts'o | ef9abe5 | 2001-01-01 15:31:53 +0000 | [diff] [blame] | 2395 | memset(fs->super->s_last_mounted, 0, |
| 2396 | sizeof(fs->super->s_last_mounted)); |
| 2397 | strncpy(fs->super->s_last_mounted, mount_dir, |
| 2398 | sizeof(fs->super->s_last_mounted)); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 2399 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2400 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 2401 | if (!quiet || noaction) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2402 | show_stats(fs); |
| 2403 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 2404 | if (noaction) |
| 2405 | exit(0); |
| 2406 | |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 2407 | if (fs->super->s_feature_incompat & |
| 2408 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { |
| 2409 | create_journal_dev(fs); |
Andreas Dilger | 568101f | 2001-10-13 01:22:25 -0600 | [diff] [blame] | 2410 | exit(ext2fs_close(fs) ? 1 : 0); |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2413 | if (bad_blocks_filename) |
| 2414 | read_bb_file(fs, &bb_list, bad_blocks_filename); |
| 2415 | if (cflag) |
| 2416 | test_disk(fs, &bb_list); |
| 2417 | |
| 2418 | handle_bad_blocks(fs, bb_list); |
Theodore Ts'o | 0c17cb2 | 2008-02-18 22:56:25 -0500 | [diff] [blame] | 2419 | fs->stride = fs_stride = fs->super->s_raid_stride; |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 2420 | if (!quiet) |
| 2421 | printf(_("Allocating group tables: ")); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 2422 | retval = ext2fs_allocate_tables(fs); |
| 2423 | if (retval) { |
| 2424 | com_err(program_name, retval, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2425 | _("while trying to allocate filesystem tables")); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 2426 | exit(1); |
| 2427 | } |
Valerie Aurora Henson | 95fd65b | 2009-08-23 19:20:03 -0400 | [diff] [blame] | 2428 | if (!quiet) |
| 2429 | printf(_("done \n")); |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 2430 | |
| 2431 | retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map); |
| 2432 | if (retval) { |
| 2433 | com_err(program_name, retval, |
| 2434 | _("\n\twhile converting subcluster bitmap")); |
| 2435 | exit(1); |
| 2436 | } |
| 2437 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 2438 | if (super_only) { |
| 2439 | fs->super->s_state |= EXT2_ERROR_FS; |
| 2440 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); |
Theodore Ts'o | ba9e0af | 2012-02-16 23:16:34 -0500 | [diff] [blame] | 2441 | /* |
| 2442 | * The command "mke2fs -S" is used to recover |
| 2443 | * corrupted file systems, so do not mark any of the |
| 2444 | * inodes as unused; we want e2fsck to consider all |
| 2445 | * inodes as potentially containing recoverable data. |
| 2446 | */ |
| 2447 | if (fs->super->s_feature_ro_compat & |
| 2448 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { |
Theodore Ts'o | 30ac1ce | 2012-02-27 00:51:39 -0500 | [diff] [blame] | 2449 | for (i = 0; i < fs->group_desc_count; i++) |
Theodore Ts'o | ba9e0af | 2012-02-16 23:16:34 -0500 | [diff] [blame] | 2450 | ext2fs_bg_itable_unused_set(fs, i, 0); |
| 2451 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 2452 | } else { |
Andreas Dilger | 59f2724 | 2001-08-30 15:39:04 -0600 | [diff] [blame] | 2453 | /* rsv must be a power of two (64kB is MD RAID sb alignment) */ |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 2454 | blk64_t rsv = 65536 / fs->blocksize; |
| 2455 | blk64_t blocks = ext2fs_blocks_count(fs->super); |
| 2456 | blk64_t start; |
| 2457 | blk64_t ret_blk; |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2458 | |
| 2459 | #ifdef ZAP_BOOTBLOCK |
Theodore Ts'o | 04a9685 | 2001-08-30 21:55:26 -0400 | [diff] [blame] | 2460 | zap_sector(fs, 0, 2); |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2461 | #endif |
Andreas Dilger | 59f2724 | 2001-08-30 15:39:04 -0600 | [diff] [blame] | 2462 | |
| 2463 | /* |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2464 | * Wipe out any old MD RAID (or other) metadata at the end |
| 2465 | * of the device. This will also verify that the device is |
Andreas Dilger | 59f2724 | 2001-08-30 15:39:04 -0600 | [diff] [blame] | 2466 | * as large as we think. Be careful with very small devices. |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2467 | */ |
Andreas Dilger | 59f2724 | 2001-08-30 15:39:04 -0600 | [diff] [blame] | 2468 | start = (blocks & ~(rsv - 1)); |
| 2469 | if (start > rsv) |
| 2470 | start -= rsv; |
| 2471 | if (start > 0) |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 2472 | retval = ext2fs_zero_blocks2(fs, start, blocks - start, |
Aneesh Kumar K.V | b626b39 | 2007-08-13 15:56:26 +0530 | [diff] [blame] | 2473 | &ret_blk, NULL); |
Andreas Dilger | 59f2724 | 2001-08-30 15:39:04 -0600 | [diff] [blame] | 2474 | |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2475 | if (retval) { |
| 2476 | com_err(program_name, retval, |
Jose R. Santos | 02d6f47 | 2010-06-13 13:00:00 -0400 | [diff] [blame] | 2477 | _("while zeroing block %llu at end of filesystem"), |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2478 | ret_blk); |
Theodore Ts'o | 1400bbb | 2001-05-14 04:19:25 +0000 | [diff] [blame] | 2479 | } |
Eric Sandeen | 6fcd6f8 | 2010-08-20 16:41:14 -0500 | [diff] [blame] | 2480 | write_inode_tables(fs, lazy_itable_init, itable_zeroed); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 2481 | create_root_dir(fs); |
| 2482 | create_lost_and_found(fs); |
| 2483 | reserve_inodes(fs); |
| 2484 | create_bad_block_inode(fs, bb_list); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2485 | if (fs->super->s_feature_compat & |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 2486 | EXT2_FEATURE_COMPAT_RESIZE_INODE) { |
| 2487 | retval = ext2fs_create_resize_inode(fs); |
| 2488 | if (retval) { |
| 2489 | com_err("ext2fs_create_resize_inode", retval, |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 2490 | _("while reserving blocks for online resize")); |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 2491 | exit(1); |
| 2492 | } |
Theodore Ts'o | d323f8f | 2004-12-15 14:39:16 -0500 | [diff] [blame] | 2493 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 2494 | } |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 2495 | |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 2496 | if (journal_device) { |
| 2497 | ext2_filsys jfs; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2498 | |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 2499 | if (!force) |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2500 | check_plausibility(journal_device); |
Theodore Ts'o | 6398532 | 2001-01-03 17:02:13 +0000 | [diff] [blame] | 2501 | check_mount(journal_device, force, _("journal")); |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 2502 | |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 2503 | retval = ext2fs_open(journal_device, EXT2_FLAG_RW| |
| 2504 | EXT2_FLAG_JOURNAL_DEV_OK, 0, |
| 2505 | fs->blocksize, unix_io_manager, &jfs); |
| 2506 | if (retval) { |
| 2507 | com_err(program_name, retval, |
| 2508 | _("while trying to open journal device %s\n"), |
| 2509 | journal_device); |
| 2510 | exit(1); |
| 2511 | } |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2512 | if (!quiet) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2513 | printf(_("Adding journal to device %s: "), |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 2514 | journal_device); |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2515 | fflush(stdout); |
| 2516 | } |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 2517 | retval = ext2fs_add_journal_device(fs, jfs); |
| 2518 | if(retval) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2519 | com_err (program_name, retval, |
| 2520 | _("\n\twhile trying to add journal to device %s"), |
Theodore Ts'o | 8ddaa66 | 2000-11-17 04:55:24 +0000 | [diff] [blame] | 2521 | journal_device); |
| 2522 | exit(1); |
| 2523 | } |
| 2524 | if (!quiet) |
| 2525 | printf(_("done\n")); |
Theodore Ts'o | 16ed5b3 | 2001-01-16 07:47:31 +0000 | [diff] [blame] | 2526 | ext2fs_close(jfs); |
Andreas Dilger | 2d15576 | 2001-08-17 03:48:11 -0600 | [diff] [blame] | 2527 | free(journal_device); |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 2528 | } else if ((journal_size) || |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 2529 | (fs_param.s_feature_compat & |
Theodore Ts'o | 9dc6ad1 | 2006-03-23 22:00:01 -0500 | [diff] [blame] | 2530 | EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { |
Theodore Ts'o | 2537b6d | 2001-03-26 20:07:13 +0000 | [diff] [blame] | 2531 | journal_blocks = figure_journal_size(journal_size, fs); |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2532 | |
Theodore Ts'o | a620bad | 2009-03-31 07:42:24 -0400 | [diff] [blame] | 2533 | if (super_only) { |
| 2534 | printf(_("Skipping journal creation in super-only mode\n")); |
| 2535 | fs->super->s_journal_inum = EXT2_JOURNAL_INO; |
| 2536 | goto no_journal; |
| 2537 | } |
| 2538 | |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2539 | if (!journal_blocks) { |
| 2540 | fs->super->s_feature_compat &= |
| 2541 | ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 2542 | goto no_journal; |
| 2543 | } |
| 2544 | if (!quiet) { |
Theodore Ts'o | d4e0b1c | 2007-08-03 18:56:01 -0400 | [diff] [blame] | 2545 | printf(_("Creating journal (%u blocks): "), |
Theodore Ts'o | 16ad333 | 2000-12-31 03:21:56 +0000 | [diff] [blame] | 2546 | journal_blocks); |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2547 | fflush(stdout); |
| 2548 | } |
Theodore Ts'o | 6398532 | 2001-01-03 17:02:13 +0000 | [diff] [blame] | 2549 | retval = ext2fs_add_journal_inode(fs, journal_blocks, |
| 2550 | journal_flags); |
Theodore Ts'o | 85ef4ae | 2000-10-24 19:33:45 +0000 | [diff] [blame] | 2551 | if (retval) { |
| 2552 | com_err (program_name, retval, |
Theodore Ts'o | 1d08d9b | 2001-04-17 01:01:49 +0000 | [diff] [blame] | 2553 | _("\n\twhile trying to create journal")); |
Theodore Ts'o | 85ef4ae | 2000-10-24 19:33:45 +0000 | [diff] [blame] | 2554 | exit(1); |
| 2555 | } |
| 2556 | if (!quiet) |
| 2557 | printf(_("done\n")); |
| 2558 | } |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2559 | no_journal: |
Andreas Dilger | 0f5eba7 | 2011-09-24 13:48:55 -0400 | [diff] [blame] | 2560 | if (!super_only && |
| 2561 | fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) { |
| 2562 | retval = ext2fs_mmp_init(fs); |
| 2563 | if (retval) { |
| 2564 | fprintf(stderr, _("\nError while enabling multiple " |
| 2565 | "mount protection feature.")); |
| 2566 | exit(1); |
| 2567 | } |
| 2568 | if (!quiet) |
| 2569 | printf(_("Multiple mount protection is enabled " |
| 2570 | "with update interval %d seconds.\n"), |
| 2571 | fs->super->s_mmp_update_interval); |
| 2572 | } |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2573 | |
Theodore Ts'o | c6ed60c | 2011-06-10 18:57:22 -0400 | [diff] [blame] | 2574 | if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param, |
| 2575 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) |
| 2576 | fix_cluster_bg_counts(fs); |
Aditya Kali | 1f5d7a8 | 2011-07-20 11:40:04 -0700 | [diff] [blame] | 2577 | if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param, |
| 2578 | EXT4_FEATURE_RO_COMPAT_QUOTA)) |
| 2579 | create_quota_inodes(fs); |
| 2580 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2581 | if (!quiet) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 2582 | printf(_("Writing superblocks and " |
| 2583 | "filesystem accounting information: ")); |
Lukas Czerner | faa2dcd | 2011-09-13 22:24:11 -0400 | [diff] [blame] | 2584 | checkinterval = fs->super->s_checkinterval; |
| 2585 | max_mnt_count = fs->super->s_max_mnt_count; |
| 2586 | retval = ext2fs_close(fs); |
Theodore Ts'o | 5d45d80 | 1999-03-16 19:35:19 +0000 | [diff] [blame] | 2587 | if (retval) { |
Theodore Ts'o | 6693837 | 2002-03-08 00:14:46 -0500 | [diff] [blame] | 2588 | fprintf(stderr, |
| 2589 | _("\nWarning, had trouble writing out superblocks.")); |
Lukas Czerner | faa2dcd | 2011-09-13 22:24:11 -0400 | [diff] [blame] | 2590 | } else if (!quiet) { |
Theodore Ts'o | 2537b6d | 2001-03-26 20:07:13 +0000 | [diff] [blame] | 2591 | printf(_("done\n\n")); |
Theodore Ts'o | 1cca86f | 2003-09-01 09:28:18 -0400 | [diff] [blame] | 2592 | if (!getenv("MKE2FS_SKIP_CHECK_MSG")) |
Lukas Czerner | faa2dcd | 2011-09-13 22:24:11 -0400 | [diff] [blame] | 2593 | print_check_message(max_mnt_count, checkinterval); |
Theodore Ts'o | 93345d1 | 2001-02-17 06:09:50 +0000 | [diff] [blame] | 2594 | } |
Lukas Czerner | faa2dcd | 2011-09-13 22:24:11 -0400 | [diff] [blame] | 2595 | |
Theodore Ts'o | a6d8302 | 2006-12-26 03:38:07 -0500 | [diff] [blame] | 2596 | remove_error_table(&et_ext2_error_table); |
| 2597 | remove_error_table(&et_prof_error_table); |
Theodore Ts'o | 3d43836 | 2008-02-19 08:32:58 -0500 | [diff] [blame] | 2598 | profile_release(profile); |
Theodore Ts'o | 5a2db04 | 2010-12-01 18:49:26 -0500 | [diff] [blame] | 2599 | for (i=0; fs_types[i]; i++) |
| 2600 | free(fs_types[i]); |
| 2601 | free(fs_types); |
Lukas Czerner | faa2dcd | 2011-09-13 22:24:11 -0400 | [diff] [blame] | 2602 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 2603 | } |