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