Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mke2fs.c - Make a ext2fs filesystem. |
| 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 4 | * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* Usage: mke2fs [options] device |
| 13 | * |
| 14 | * The device may be a block device or a image of one, but this isn't |
| 15 | * enforced (but it's not much fun on a character device :-). |
| 16 | */ |
| 17 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <ctype.h> |
| 22 | #include <termios.h> |
| 23 | #include <time.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 24 | #ifdef HAVE_GETOPT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 26 | #endif |
| 27 | #ifdef HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 29 | #endif |
| 30 | #ifdef HAVE_STDLIB_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 32 | #endif |
| 33 | #ifdef HAVE_ERRNO_H |
| 34 | #include <errno.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_MNTENT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 37 | #include <mntent.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 38 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 39 | #include <sys/ioctl.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 41 | #include <sys/stat.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 42 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 43 | #ifdef HAVE_LINUX_FS_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 44 | #include <linux/fs.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 45 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 46 | #include <linux/ext2_fs.h> |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 47 | #ifdef HAVE_LINUX_MAJOR_H |
| 48 | #include <linux/major.h> |
| 49 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 50 | |
| 51 | #include "et/com_err.h" |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 52 | #include "uuid/uuid.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | #include "ext2fs/ext2fs.h" |
| 54 | #include "../version.h" |
| 55 | |
| 56 | #define STRIDE_LENGTH 8 |
| 57 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 58 | #ifndef sparc |
| 59 | #define ZAP_BOOTBLOCK |
| 60 | #endif |
| 61 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | extern int isatty(int); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 63 | extern FILE *fpopen(const char *cmd, const char *mode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | |
| 65 | const char * program_name = "mke2fs"; |
| 66 | const char * device_name = NULL; |
| 67 | |
| 68 | /* Command line options */ |
| 69 | int cflag = 0; |
| 70 | int verbose = 0; |
| 71 | int quiet = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 72 | int super_only = 0; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 73 | int force = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 74 | char *bad_blocks_filename = 0; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 75 | __u32 fs_stride = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 76 | |
| 77 | struct ext2_super_block param; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 78 | char *creator_os = NULL; |
| 79 | char *volume_label = NULL; |
| 80 | char *mount_dir = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 81 | |
| 82 | static void usage(NOARGS) |
| 83 | { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 84 | fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] " |
| 85 | "[-f fragment-size]\n\t[-i bytes-per-inode] " |
| 86 | "[-m reserved-blocks-percentage] [-qvS]\n\t" |
| 87 | "[-o creator-os] [-g blocks-per-group] [-L volume-label]\n\t" |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 88 | "[-M last-mounted-directory] [-r fs-revision] [-R raid_opts]\n\t" |
| 89 | "device [blocks-count]\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 90 | program_name); |
| 91 | exit(1); |
| 92 | } |
| 93 | |
| 94 | static int log2(int arg) |
| 95 | { |
| 96 | int l = 0; |
| 97 | |
| 98 | arg >>= 1; |
| 99 | while (arg) { |
| 100 | l++; |
| 101 | arg >>= 1; |
| 102 | } |
| 103 | return l; |
| 104 | } |
| 105 | |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 106 | static void check_plausibility(NOARGS) |
| 107 | { |
| 108 | #ifdef HAVE_LINUX_MAJOR_H |
| 109 | int val; |
| 110 | struct stat s; |
| 111 | |
| 112 | val = stat(device_name, &s); |
| 113 | |
| 114 | if(val == -1) { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 115 | printf("Could not stat %s --- %s\n", device_name, |
| 116 | error_message(errno)); |
| 117 | if (errno == ENOENT) |
| 118 | printf("\nThe device apparently does not exist; " |
| 119 | "did yo specify it correctly?\n"); |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 120 | exit(1); |
| 121 | } |
| 122 | if(!S_ISBLK(s.st_mode)) { |
| 123 | printf("%s is not a block special device.\n", device_name); |
| 124 | printf("Proceed anyway? (y,n) "); |
| 125 | if (getchar() != 'y') |
| 126 | exit(1); |
| 127 | return; |
| 128 | } |
| 129 | if ((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) || |
| 130 | (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR && |
| 131 | MINOR(s.st_rdev)%16 == 0)) { |
| 132 | printf("%s is entire device, not just one partition!\n", |
| 133 | device_name); |
| 134 | printf("Proceed anyway? (y,n) "); |
| 135 | if (getchar() != 'y') |
| 136 | exit(1); |
| 137 | return; |
| 138 | } |
| 139 | #endif |
| 140 | } |
| 141 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 142 | static void check_mount(NOARGS) |
| 143 | { |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 144 | errcode_t retval; |
| 145 | int mount_flags; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 146 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 147 | retval = ext2fs_check_if_mounted(device_name, &mount_flags); |
| 148 | if (retval) { |
| 149 | com_err("ext2fs_check_if_mount", retval, |
| 150 | "while determining whether %s is mounted.", |
| 151 | device_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 152 | return; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 153 | } |
| 154 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 155 | return; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 156 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 157 | fprintf(stderr, "%s is mounted; will not make a filesystem here!\n", |
| 158 | device_name); |
| 159 | exit(1); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Helper function for read_bb_file and test_disk |
| 164 | */ |
| 165 | static void invalid_block(ext2_filsys fs, blk_t blk) |
| 166 | { |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 167 | printf("Bad block %u out of range; ignored.\n", blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Reads the bad blocks list from a file |
| 173 | */ |
| 174 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, |
| 175 | const char *bad_blocks_file) |
| 176 | { |
| 177 | FILE *f; |
| 178 | errcode_t retval; |
| 179 | |
| 180 | f = fopen(bad_blocks_file, "r"); |
| 181 | if (!f) { |
| 182 | com_err("read_bad_blocks_file", errno, |
| 183 | "while trying to open %s", bad_blocks_file); |
| 184 | exit(1); |
| 185 | } |
| 186 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
| 187 | fclose (f); |
| 188 | if (retval) { |
| 189 | com_err("ext2fs_read_bb_FILE", retval, |
| 190 | "while reading in list of bad blocks from file"); |
| 191 | exit(1); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Runs the badblocks program to test the disk |
| 197 | */ |
| 198 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) |
| 199 | { |
| 200 | FILE *f; |
| 201 | errcode_t retval; |
| 202 | char buf[1024]; |
| 203 | |
Theodore Ts'o | f635d7f | 1997-05-09 02:50:16 +0000 | [diff] [blame] | 204 | sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize, |
| 205 | quiet ? "" : "-s ", fs->device_name, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 206 | fs->super->s_blocks_count); |
| 207 | if (verbose) |
| 208 | printf("Running command: %s\n", buf); |
| 209 | f = popen(buf, "r"); |
| 210 | if (!f) { |
| 211 | com_err("popen", errno, |
| 212 | "while trying run '%s'", buf); |
| 213 | exit(1); |
| 214 | } |
| 215 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 216 | pclose(f); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 217 | if (retval) { |
| 218 | com_err("ext2fs_read_bb_FILE", retval, |
| 219 | "while processing list of bad blocks from program"); |
| 220 | exit(1); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) |
| 225 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 226 | int i, j; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 227 | int must_be_good; |
| 228 | blk_t blk; |
| 229 | badblocks_iterate bb_iter; |
| 230 | errcode_t retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 231 | blk_t group_block; |
| 232 | int group; |
| 233 | int group_bad; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 234 | |
| 235 | if (!bb_list) |
| 236 | return; |
| 237 | |
| 238 | /* |
| 239 | * The primary superblock and group descriptors *must* be |
| 240 | * good; if not, abort. |
| 241 | */ |
| 242 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; |
| 243 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { |
| 244 | if (badblocks_list_test(bb_list, i)) { |
| 245 | fprintf(stderr, "Block %d in primary superblock/group " |
| 246 | "descriptor area bad.\n", i); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 247 | fprintf(stderr, "Blocks %d through %d must be good " |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 248 | "in order to build a filesystem.\n", |
| 249 | fs->super->s_first_data_block, must_be_good); |
| 250 | fprintf(stderr, "Aborting....\n"); |
| 251 | exit(1); |
| 252 | } |
| 253 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * See if any of the bad blocks are showing up in the backup |
| 257 | * superblocks and/or group descriptors. If so, issue a |
| 258 | * warning and adjust the block counts appropriately. |
| 259 | */ |
| 260 | group_block = fs->super->s_first_data_block + |
| 261 | fs->super->s_blocks_per_group; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 262 | |
| 263 | for (i = 1; i < fs->group_desc_count; i++) { |
Theodore Ts'o | 92bcc59 | 1998-02-16 22:29:34 +0000 | [diff] [blame^] | 264 | group_bad = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 265 | for (j=0; j < fs->desc_blocks+1; j++) { |
| 266 | if (badblocks_list_test(bb_list, group_block + |
| 267 | j)) { |
| 268 | if (!group_bad) |
| 269 | fprintf(stderr, |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 270 | "Warning: the backup superblock/group descriptors at block %d contain\n" |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 271 | " bad blocks.\n\n", |
| 272 | group_block); |
| 273 | group_bad++; |
| 274 | group = ext2fs_group_of_blk(fs, group_block+j); |
| 275 | fs->group_desc[group].bg_free_blocks_count++; |
| 276 | fs->super->s_free_blocks_count++; |
| 277 | } |
| 278 | } |
| 279 | group_block += fs->super->s_blocks_per_group; |
| 280 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * Mark all the bad blocks as used... |
| 284 | */ |
| 285 | retval = badblocks_list_iterate_begin(bb_list, &bb_iter); |
| 286 | if (retval) { |
| 287 | com_err("badblocks_list_iterate_begin", retval, |
| 288 | "while marking bad blocks as used"); |
| 289 | exit(1); |
| 290 | } |
| 291 | while (badblocks_list_iterate(bb_iter, &blk)) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 292 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 293 | badblocks_list_iterate_end(bb_iter); |
| 294 | } |
| 295 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 296 | static void write_inode_tables(ext2_filsys fs) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 297 | { |
| 298 | errcode_t retval; |
| 299 | blk_t blk; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 300 | int i, j, num, count; |
| 301 | char *buf; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 302 | |
| 303 | buf = malloc(fs->blocksize * STRIDE_LENGTH); |
| 304 | if (!buf) { |
| 305 | com_err("malloc", ENOMEM, "while allocating zeroizing buffer"); |
| 306 | exit(1); |
| 307 | } |
| 308 | memset(buf, 0, fs->blocksize * STRIDE_LENGTH); |
| 309 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 310 | if (!quiet) |
| 311 | printf("Writing inode tables: "); |
| 312 | for (i = 0; i < fs->group_desc_count; i++) { |
| 313 | if (!quiet) |
| 314 | printf("%4d/%4ld", i, fs->group_desc_count); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 315 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 316 | blk = fs->group_desc[i].bg_inode_table; |
| 317 | num = fs->inode_blocks_per_group; |
| 318 | |
| 319 | for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { |
| 320 | if (num-j > STRIDE_LENGTH) |
| 321 | count = STRIDE_LENGTH; |
| 322 | else |
| 323 | count = num - j; |
| 324 | retval = io_channel_write_blk(fs->io, blk, count, buf); |
| 325 | if (retval) |
| 326 | printf("Warning: could not write %d blocks " |
| 327 | "in inode table starting at %d: %s\n", |
| 328 | count, blk, error_message(retval)); |
| 329 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 330 | if (!quiet) |
| 331 | printf("\b\b\b\b\b\b\b\b\b"); |
| 332 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 333 | free(buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 334 | if (!quiet) |
| 335 | printf("done \n"); |
| 336 | } |
| 337 | |
| 338 | static void create_root_dir(ext2_filsys fs) |
| 339 | { |
| 340 | errcode_t retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 341 | struct ext2_inode inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 342 | |
| 343 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); |
| 344 | if (retval) { |
| 345 | com_err("ext2fs_mkdir", retval, "while creating root dir"); |
| 346 | exit(1); |
| 347 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 348 | if (geteuid()) { |
| 349 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); |
| 350 | if (retval) { |
| 351 | com_err("ext2fs_read_inode", retval, |
| 352 | "while reading root inode"); |
| 353 | exit(1); |
| 354 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 355 | inode.i_uid = getuid(); |
| 356 | if (inode.i_uid) |
| 357 | inode.i_gid = getgid(); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 358 | retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); |
| 359 | if (retval) { |
| 360 | com_err("ext2fs_write_inode", retval, |
| 361 | "while setting root inode ownership"); |
| 362 | exit(1); |
| 363 | } |
| 364 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static void create_lost_and_found(ext2_filsys fs) |
| 368 | { |
| 369 | errcode_t retval; |
| 370 | ino_t ino; |
| 371 | const char *name = "lost+found"; |
| 372 | int i; |
| 373 | |
| 374 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); |
| 375 | if (retval) { |
| 376 | com_err("ext2fs_mkdir", retval, "while creating /lost+found"); |
| 377 | exit(1); |
| 378 | } |
| 379 | |
| 380 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); |
| 381 | if (retval) { |
| 382 | com_err("ext2_lookup", retval, "while looking up /lost+found"); |
| 383 | exit(1); |
| 384 | } |
| 385 | |
| 386 | for (i=1; i < EXT2_NDIR_BLOCKS; i++) { |
| 387 | retval = ext2fs_expand_dir(fs, ino); |
| 388 | if (retval) { |
| 389 | com_err("ext2fs_expand_dir", retval, |
| 390 | "while expanding /lost+found"); |
| 391 | exit(1); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) |
| 397 | { |
| 398 | errcode_t retval; |
| 399 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 400 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 401 | fs->group_desc[0].bg_free_inodes_count--; |
| 402 | fs->super->s_free_inodes_count--; |
| 403 | retval = ext2fs_update_bb_inode(fs, bb_list); |
| 404 | if (retval) { |
| 405 | com_err("ext2fs_update_bb_inode", retval, |
| 406 | "while setting bad block inode"); |
| 407 | exit(1); |
| 408 | } |
| 409 | |
| 410 | } |
| 411 | |
| 412 | static void reserve_inodes(ext2_filsys fs) |
| 413 | { |
| 414 | ino_t i; |
| 415 | int group; |
| 416 | |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 417 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 418 | ext2fs_mark_inode_bitmap(fs->inode_map, i); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 419 | group = ext2fs_group_of_ino(fs, i); |
| 420 | fs->group_desc[group].bg_free_inodes_count--; |
| 421 | fs->super->s_free_inodes_count--; |
| 422 | } |
| 423 | ext2fs_mark_ib_dirty(fs); |
| 424 | } |
| 425 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 426 | #ifdef ZAP_BOOTBLOCK |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 427 | static void zap_bootblock(ext2_filsys fs) |
| 428 | { |
| 429 | char buf[512]; |
| 430 | int retval; |
| 431 | |
| 432 | memset(buf, 0, 512); |
| 433 | |
| 434 | retval = io_channel_write_blk(fs->io, 0, -512, buf); |
| 435 | if (retval) |
| 436 | printf("Warning: could not erase block 0: %s\n", |
| 437 | error_message(retval)); |
| 438 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 439 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 440 | |
| 441 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 442 | static void show_stats(ext2_filsys fs) |
| 443 | { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 444 | struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super; |
| 445 | char buf[80]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 446 | blk_t group_block; |
| 447 | int i, col_left; |
| 448 | |
| 449 | if (param.s_blocks_count != s->s_blocks_count) |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 450 | printf("warning: %d blocks unused.\n\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 451 | param.s_blocks_count - s->s_blocks_count); |
| 452 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 453 | switch (fs->super->s_creator_os) { |
| 454 | case EXT2_OS_LINUX: printf ("Linux"); break; |
| 455 | case EXT2_OS_HURD: printf ("GNU/hurd"); break; |
| 456 | case EXT2_OS_MASIX: printf ("Masix"); break; |
| 457 | default: printf ("(unknown os)"); |
| 458 | } |
| 459 | printf (" ext2 filesystem format\n"); |
| 460 | memset(buf, 0, sizeof(buf)); |
| 461 | strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); |
| 462 | printf("Filesystem label=%s\n", buf); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 463 | printf("%u inodes, %u blocks\n", s->s_inodes_count, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 464 | s->s_blocks_count); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 465 | printf("%u blocks (%2.2f%%) reserved for the super user\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 466 | s->s_r_blocks_count, |
| 467 | 100.0 * s->s_r_blocks_count / s->s_blocks_count); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 468 | printf("First data block=%u\n", s->s_first_data_block); |
| 469 | printf("Block size=%u (log=%u)\n", fs->blocksize, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 470 | s->s_log_block_size); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 471 | printf("Fragment size=%u (log=%u)\n", fs->fragsize, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 472 | s->s_log_frag_size); |
| 473 | printf("%lu block group%s\n", fs->group_desc_count, |
| 474 | (fs->group_desc_count > 1) ? "s" : ""); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 475 | printf("%u blocks per group, %u fragments per group\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 476 | s->s_blocks_per_group, s->s_frags_per_group); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 477 | printf("%u inodes per group\n", s->s_inodes_per_group); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 478 | |
| 479 | if (fs->group_desc_count == 1) { |
| 480 | printf("\n"); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | printf("Superblock backups stored on blocks: "); |
| 485 | group_block = s->s_first_data_block; |
| 486 | col_left = 0; |
| 487 | for (i = 1; i < fs->group_desc_count; i++) { |
| 488 | group_block += s->s_blocks_per_group; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 489 | if (!ext2fs_bg_has_super(fs, i)) |
| 490 | continue; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 491 | if (!col_left--) { |
| 492 | printf("\n\t"); |
Theodore Ts'o | e22d461 | 1997-06-17 03:57:27 +0000 | [diff] [blame] | 493 | col_left = 6; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 494 | } |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 495 | printf("%u", group_block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 496 | if (i != fs->group_desc_count - 1) |
| 497 | printf(", "); |
| 498 | } |
| 499 | printf("\n\n"); |
| 500 | } |
| 501 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 502 | #ifndef HAVE_STRCASECMP |
| 503 | static int strcasecmp (char *s1, char *s2) |
| 504 | { |
| 505 | while (*s1 && *s2) { |
| 506 | int ch1 = *s1++, ch2 = *s2++; |
| 507 | if (isupper (ch1)) |
| 508 | ch1 = tolower (ch1); |
| 509 | if (isupper (ch2)) |
| 510 | ch2 = tolower (ch2); |
| 511 | if (ch1 != ch2) |
| 512 | return ch1 - ch2; |
| 513 | } |
| 514 | return *s1 ? 1 : *s2 ? -1 : 0; |
| 515 | } |
| 516 | #endif |
| 517 | |
| 518 | /* |
| 519 | * Set the S_CREATOR_OS field. Return true if OS is known, |
| 520 | * otherwise, 0. |
| 521 | */ |
| 522 | static int set_os(struct ext2_super_block *sb, char *os) |
| 523 | { |
| 524 | if (isdigit (*os)) |
| 525 | sb->s_creator_os = atoi (os); |
| 526 | else if (strcasecmp(os, "linux") == 0) |
| 527 | sb->s_creator_os = EXT2_OS_LINUX; |
| 528 | else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) |
| 529 | sb->s_creator_os = EXT2_OS_HURD; |
| 530 | else if (strcasecmp(os, "masix") == 0) |
| 531 | sb->s_creator_os = EXT2_OS_MASIX; |
| 532 | else |
| 533 | return 0; |
| 534 | return 1; |
| 535 | } |
| 536 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 537 | #define PATH_SET "PATH=/sbin" |
| 538 | |
Theodore Ts'o | d163b09 | 1997-10-03 17:42:28 +0000 | [diff] [blame] | 539 | static void parse_raid_opts(const char *opts) |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 540 | { |
| 541 | char *buf, *token, *next, *p, *arg; |
| 542 | int len; |
| 543 | int raid_usage = 0; |
| 544 | |
| 545 | len = strlen(opts); |
| 546 | buf = malloc(len+1); |
| 547 | if (!buf) { |
| 548 | fprintf(stderr, "Couldn't allocate memory to parse " |
| 549 | "raid options!\n"); |
| 550 | exit(1); |
| 551 | } |
| 552 | strcpy(buf, opts); |
| 553 | for (token = buf; token && *token; token = next) { |
| 554 | p = strchr(token, ','); |
| 555 | next = 0; |
| 556 | if (p) { |
| 557 | *p = 0; |
| 558 | next = p+1; |
| 559 | } |
| 560 | arg = strchr(token, '='); |
| 561 | if (arg) { |
| 562 | *arg = 0; |
| 563 | arg++; |
| 564 | } |
| 565 | if (strcmp(token, "stride") == 0) { |
| 566 | if (!arg) { |
| 567 | raid_usage++; |
| 568 | continue; |
| 569 | } |
| 570 | fs_stride = strtoul(arg, &p, 0); |
| 571 | if (*p || (fs_stride == 0)) { |
| 572 | fprintf(stderr, "Invalid stride parameter.\n"); |
| 573 | raid_usage++; |
| 574 | continue; |
| 575 | } |
| 576 | } else |
| 577 | raid_usage++; |
| 578 | } |
| 579 | if (raid_usage) { |
| 580 | fprintf(stderr, "\nBad raid options specified.\n\n" |
| 581 | "Raid options are separated by commas, " |
| 582 | "and may take an argument which\n" |
| 583 | "\tis set off by an equals ('=') sign.\n\n" |
| 584 | "Valid raid options are:\n" |
| 585 | "\tstride=<stride length in blocks>\n\n"); |
| 586 | exit(1); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | |
| 591 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 592 | static void PRS(int argc, char *argv[]) |
| 593 | { |
Theodore Ts'o | 519149f | 1997-10-25 03:49:49 +0000 | [diff] [blame] | 594 | int c; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 595 | int size; |
| 596 | char * tmp; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 597 | blk_t max = 8192; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 598 | int inode_ratio = 4096; |
| 599 | int reserved_ratio = 5; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 600 | errcode_t retval; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 601 | int sparse_option = -1; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 602 | char *oldpath = getenv("PATH"); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 603 | struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) ¶m; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 604 | char *raid_opts = 0; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 605 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 606 | /* Update our PATH to include /sbin */ |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 607 | if (oldpath) { |
| 608 | char *newpath; |
| 609 | |
| 610 | newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath)); |
| 611 | strcpy (newpath, PATH_SET); |
| 612 | strcat (newpath, ":"); |
| 613 | strcat (newpath, oldpath); |
| 614 | putenv (newpath); |
| 615 | } else |
| 616 | putenv (PATH_SET); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 617 | |
| 618 | setbuf(stdout, NULL); |
| 619 | setbuf(stderr, NULL); |
| 620 | initialize_ext2_error_table(); |
| 621 | memset(¶m, 0, sizeof(struct ext2_super_block)); |
| 622 | |
| 623 | fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n", |
| 624 | E2FSPROGS_VERSION, E2FSPROGS_DATE, |
| 625 | EXT2FS_VERSION, EXT2FS_DATE); |
| 626 | if (argc && *argv) |
| 627 | program_name = *argv; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 628 | while ((c = getopt (argc, argv, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 629 | "b:cf:g:i:l:m:o:qr:R:s:tvI:SFL:M:")) != EOF) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 630 | switch (c) { |
| 631 | case 'b': |
| 632 | size = strtoul(optarg, &tmp, 0); |
| 633 | if (size < 1024 || size > 4096 || *tmp) { |
| 634 | com_err(program_name, 0, "bad block size - %s", |
| 635 | optarg); |
| 636 | exit(1); |
| 637 | } |
| 638 | param.s_log_block_size = |
| 639 | log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 640 | max = size * 8; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 641 | break; |
| 642 | case 'c': |
| 643 | case 't': /* Check for bad blocks */ |
| 644 | cflag = 1; |
| 645 | break; |
| 646 | case 'f': |
| 647 | size = strtoul(optarg, &tmp, 0); |
| 648 | if (size < 1024 || size > 4096 || *tmp) { |
| 649 | com_err(program_name, 0, "bad fragment size - %s", |
| 650 | optarg); |
| 651 | exit(1); |
| 652 | } |
| 653 | param.s_log_frag_size = |
| 654 | log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 655 | printf("Warning: fragments not supported. " |
| 656 | "Ignoring -f option\n"); |
| 657 | break; |
| 658 | case 'g': |
| 659 | param.s_blocks_per_group = strtoul(optarg, &tmp, 0); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 660 | if (*tmp) { |
| 661 | com_err(program_name, 0, |
| 662 | "Illegal number for blocks per group"); |
| 663 | exit(1); |
| 664 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 665 | if ((param.s_blocks_per_group % 8) != 0) { |
| 666 | com_err(program_name, 0, |
| 667 | "blocks per group must be multiple of 8"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 668 | exit(1); |
| 669 | } |
| 670 | break; |
| 671 | case 'i': |
| 672 | inode_ratio = strtoul(optarg, &tmp, 0); |
| 673 | if (inode_ratio < 1024 || inode_ratio > 256 * 1024 || |
| 674 | *tmp) { |
| 675 | com_err(program_name, 0, "bad inode ratio - %s", |
| 676 | optarg); |
| 677 | exit(1); |
| 678 | } |
| 679 | break; |
| 680 | case 'l': |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 681 | bad_blocks_filename = malloc(strlen(optarg)+1); |
| 682 | if (!bad_blocks_filename) { |
| 683 | com_err(program_name, ENOMEM, |
| 684 | "in malloc for bad_blocks_filename"); |
| 685 | exit(1); |
| 686 | } |
| 687 | strcpy(bad_blocks_filename, optarg); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 688 | break; |
| 689 | case 'm': |
| 690 | reserved_ratio = strtoul(optarg, &tmp, 0); |
| 691 | if (reserved_ratio > 50 || *tmp) { |
| 692 | com_err(program_name, 0, |
| 693 | "bad reserved blocks percent - %s", |
| 694 | optarg); |
| 695 | exit(1); |
| 696 | } |
| 697 | break; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 698 | case 'o': |
| 699 | creator_os = optarg; |
| 700 | break; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 701 | case 'r': |
| 702 | param.s_rev_level = atoi(optarg); |
| 703 | break; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 704 | case 's': |
| 705 | sparse_option = atoi(optarg); |
| 706 | break; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 707 | #ifdef EXT2_DYNAMIC_REV |
| 708 | case 'I': |
| 709 | param.s_inode_size = atoi(optarg); |
| 710 | break; |
| 711 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 712 | case 'v': |
| 713 | verbose = 1; |
| 714 | break; |
| 715 | case 'q': |
| 716 | quiet = 1; |
| 717 | break; |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 718 | case 'F': |
| 719 | force = 1; |
| 720 | break; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 721 | case 'L': |
| 722 | volume_label = optarg; |
| 723 | break; |
| 724 | case 'M': |
| 725 | mount_dir = optarg; |
| 726 | break; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 727 | case 'R': |
| 728 | raid_opts = optarg; |
| 729 | break; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 730 | case 'S': |
| 731 | super_only = 1; |
| 732 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 733 | default: |
| 734 | usage(); |
| 735 | } |
| 736 | if (optind == argc) |
| 737 | usage(); |
| 738 | device_name = argv[optind]; |
| 739 | optind++; |
| 740 | if (optind < argc) { |
| 741 | param.s_blocks_count = strtoul(argv[optind++], &tmp, 0); |
| 742 | if (*tmp) { |
| 743 | com_err(program_name, 0, "bad blocks count - %s", |
| 744 | argv[optind - 1]); |
| 745 | exit(1); |
| 746 | } |
| 747 | } |
| 748 | if (optind < argc) |
| 749 | usage(); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 750 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 751 | if (raid_opts) |
| 752 | parse_raid_opts(raid_opts); |
| 753 | |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 754 | if (!force) |
| 755 | check_plausibility(); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 756 | check_mount(); |
| 757 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 758 | param.s_log_frag_size = param.s_log_block_size; |
| 759 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 760 | if (!param.s_blocks_count) { |
| 761 | retval = ext2fs_get_device_size(device_name, |
| 762 | EXT2_BLOCK_SIZE(¶m), |
| 763 | ¶m.s_blocks_count); |
| 764 | if (retval) { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 765 | com_err(program_name, retval, |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 766 | "while trying to determine filesystem size"); |
| 767 | exit(1); |
| 768 | } |
| 769 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 770 | |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 771 | if (param.s_blocks_per_group) { |
| 772 | if (param.s_blocks_per_group < 256 || |
| 773 | param.s_blocks_per_group > max || *tmp) { |
| 774 | com_err(program_name, 0, |
| 775 | "blocks per group count out of range"); |
| 776 | exit(1); |
| 777 | } |
| 778 | } |
| 779 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 780 | /* |
| 781 | * Calculate number of inodes based on the inode ratio |
| 782 | */ |
| 783 | param.s_inodes_count = |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 784 | ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m)) |
| 785 | / inode_ratio; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * Calculate number of blocks to reserve |
| 789 | */ |
| 790 | param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * If we are using revision #1, use the sparse super feature |
| 794 | * by default |
| 795 | */ |
| 796 | #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
| 797 | if ((sparse_option == 1) |
| 798 | #ifdef EXT2_DYNAMIC_REV |
| 799 | || (param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option) |
| 800 | #endif |
| 801 | ) |
| 802 | param_ext2->s_feature_ro_compat |= |
| 803 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 804 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | int main (int argc, char *argv[]) |
| 808 | { |
| 809 | errcode_t retval = 0; |
| 810 | ext2_filsys fs; |
| 811 | badblocks_list bb_list = 0; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 812 | struct ext2fs_sb *s; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 813 | |
| 814 | PRS(argc, argv); |
| 815 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 816 | /* |
| 817 | * Initialize the superblock.... |
| 818 | */ |
| 819 | retval = ext2fs_initialize(device_name, 0, ¶m, |
| 820 | unix_io_manager, &fs); |
| 821 | if (retval) { |
| 822 | com_err(device_name, retval, "while setting up superblock"); |
| 823 | exit(1); |
| 824 | } |
| 825 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 826 | /* |
| 827 | * Generate a UUID for it... |
| 828 | */ |
| 829 | s = (struct ext2fs_sb *) fs->super; |
| 830 | uuid_generate(s->s_uuid); |
| 831 | |
| 832 | /* |
| 833 | * Override the creator OS, if applicable |
| 834 | */ |
| 835 | if (creator_os && !set_os(fs->super, creator_os)) { |
| 836 | com_err (program_name, 0, "unknown os - %s", creator_os); |
| 837 | exit(1); |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Set the volume label... |
| 842 | */ |
| 843 | if (volume_label) { |
| 844 | memset(s->s_volume_name, 0, sizeof(s->s_volume_name)); |
| 845 | strncpy(s->s_volume_name, volume_label, |
| 846 | sizeof(s->s_volume_name)); |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Set the last mount directory |
| 851 | */ |
| 852 | if (mount_dir) { |
| 853 | memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted)); |
| 854 | strncpy(s->s_last_mounted, mount_dir, |
| 855 | sizeof(s->s_last_mounted)); |
| 856 | } |
| 857 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 858 | if (!quiet) |
| 859 | show_stats(fs); |
| 860 | |
| 861 | if (bad_blocks_filename) |
| 862 | read_bb_file(fs, &bb_list, bad_blocks_filename); |
| 863 | if (cflag) |
| 864 | test_disk(fs, &bb_list); |
| 865 | |
| 866 | handle_bad_blocks(fs, bb_list); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 867 | fs->stride = fs_stride; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 868 | retval = ext2fs_allocate_tables(fs); |
| 869 | if (retval) { |
| 870 | com_err(program_name, retval, |
| 871 | "while trying to allocate filesystem tables"); |
| 872 | exit(1); |
| 873 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 874 | if (super_only) { |
| 875 | fs->super->s_state |= EXT2_ERROR_FS; |
| 876 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); |
| 877 | } else { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 878 | write_inode_tables(fs); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 879 | create_root_dir(fs); |
| 880 | create_lost_and_found(fs); |
| 881 | reserve_inodes(fs); |
| 882 | create_bad_block_inode(fs, bb_list); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 883 | #ifdef ZAP_BOOTBLOCK |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 884 | zap_bootblock(fs); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 885 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 886 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 887 | |
| 888 | if (!quiet) |
| 889 | printf("Writing superblocks and " |
| 890 | "filesystem accounting information: "); |
| 891 | ext2fs_close(fs); |
| 892 | if (!quiet) |
| 893 | printf("done\n"); |
| 894 | return 0; |
| 895 | } |