| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mke2fs.c - Make a ext2fs filesystem. |
| 3 | * |
| 4 | * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | /* Usage: mke2fs [options] device |
| 9 | * |
| 10 | * The device may be a block device or a image of one, but this isn't |
| 11 | * enforced (but it's not much fun on a character device :-). |
| 12 | */ |
| 13 | |
| 14 | #include <string.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <ctype.h> |
| 17 | #include <termios.h> |
| 18 | #include <time.h> |
| 19 | #include <getopt.h> |
| 20 | #include <unistd.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <mntent.h> |
| 23 | #include <malloc.h> |
| 24 | #include <sys/ioctl.h> |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | #include <linux/fs.h> |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 28 | #include <linux/ext2_fs.h> |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | |
| 30 | #include "et/com_err.h" |
| 31 | #include "ext2fs/ext2fs.h" |
| 32 | #include "../version.h" |
| 33 | |
| 34 | #define STRIDE_LENGTH 8 |
| 35 | |
| 36 | extern int isatty(int); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 37 | extern FILE *fpopen(const char *cmd, const char *mode); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | |
| 39 | const char * program_name = "mke2fs"; |
| 40 | const char * device_name = NULL; |
| 41 | |
| 42 | /* Command line options */ |
| 43 | int cflag = 0; |
| 44 | int verbose = 0; |
| 45 | int quiet = 0; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 46 | int super_only = 0; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 47 | char *bad_blocks_filename = 0; |
| 48 | |
| 49 | struct ext2_super_block param; |
| 50 | |
| 51 | static void usage(NOARGS) |
| 52 | { |
| 53 | fprintf(stderr, |
| 54 | "Usage: %s [-c|-t|-l filename] [-b block-size] " |
| 55 | "[-f fragment-size]\n\t[-i bytes-per-inode] " |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 56 | "[-m reserved-blocks-percentage] [-qvS]\n" |
| 57 | "\t[-g blocks-per-group] device [blocks-count]\n", |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 58 | program_name); |
| 59 | exit(1); |
| 60 | } |
| 61 | |
| 62 | static int log2(int arg) |
| 63 | { |
| 64 | int l = 0; |
| 65 | |
| 66 | arg >>= 1; |
| 67 | while (arg) { |
| 68 | l++; |
| 69 | arg >>= 1; |
| 70 | } |
| 71 | return l; |
| 72 | } |
| 73 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 74 | static long valid_offset (int fd, ext2_loff_t offset) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | { |
| 76 | char ch; |
| 77 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 78 | if (ext2_llseek (fd, offset, 0) < 0) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | if (read (fd, &ch, 1) < 1) |
| 81 | return 0; |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | static int count_blocks (int fd) |
| 86 | { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 87 | ext2_loff_t high, low; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 88 | |
| 89 | low = 0; |
| 90 | for (high = 1; valid_offset (fd, high); high *= 2) |
| 91 | low = high; |
| 92 | while (low < high - 1) |
| 93 | { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 94 | const ext2_loff_t mid = (low + high) / 2; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 95 | |
| 96 | if (valid_offset (fd, mid)) |
| 97 | low = mid; |
| 98 | else |
| 99 | high = mid; |
| 100 | } |
| 101 | valid_offset (fd, 0); |
| 102 | return (low + 1) / 1024; |
| 103 | } |
| 104 | |
| 105 | static int get_size(const char *file) |
| 106 | { |
| 107 | int fd; |
| 108 | int size; |
| 109 | |
| 110 | fd = open(file, O_RDWR); |
| 111 | if (fd < 0) { |
| 112 | com_err("open", errno, "while trying to determine size of %s", |
| 113 | file); |
| 114 | exit(1); |
| 115 | } |
| 116 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { |
| 117 | close(fd); |
| 118 | return size / (EXT2_BLOCK_SIZE(¶m) / 512); |
| 119 | } |
| 120 | |
| 121 | size = count_blocks(fd); |
| 122 | close(fd); |
| 123 | return size; |
| 124 | } |
| 125 | |
| 126 | static void check_mount(NOARGS) |
| 127 | { |
| 128 | FILE * f; |
| 129 | struct mntent * mnt; |
| 130 | |
| 131 | if ((f = setmntent (MOUNTED, "r")) == NULL) |
| 132 | return; |
| 133 | while ((mnt = getmntent (f)) != NULL) |
| 134 | if (strcmp (device_name, mnt->mnt_fsname) == 0) |
| 135 | break; |
| 136 | endmntent (f); |
| 137 | if (!mnt) |
| 138 | return; |
| 139 | |
| 140 | fprintf(stderr, "%s is mounted; will not make a filesystem here!\n", |
| 141 | device_name); |
| 142 | exit(1); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Helper function for read_bb_file and test_disk |
| 147 | */ |
| 148 | static void invalid_block(ext2_filsys fs, blk_t blk) |
| 149 | { |
| 150 | printf("Bad block %lu out of range; ignored.\n", blk); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Reads the bad blocks list from a file |
| 156 | */ |
| 157 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, |
| 158 | const char *bad_blocks_file) |
| 159 | { |
| 160 | FILE *f; |
| 161 | errcode_t retval; |
| 162 | |
| 163 | f = fopen(bad_blocks_file, "r"); |
| 164 | if (!f) { |
| 165 | com_err("read_bad_blocks_file", errno, |
| 166 | "while trying to open %s", bad_blocks_file); |
| 167 | exit(1); |
| 168 | } |
| 169 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
| 170 | fclose (f); |
| 171 | if (retval) { |
| 172 | com_err("ext2fs_read_bb_FILE", retval, |
| 173 | "while reading in list of bad blocks from file"); |
| 174 | exit(1); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Runs the badblocks program to test the disk |
| 180 | */ |
| 181 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) |
| 182 | { |
| 183 | FILE *f; |
| 184 | errcode_t retval; |
| 185 | char buf[1024]; |
| 186 | |
| 187 | sprintf(buf, "badblocks %s%s %ld", quiet ? "" : "-s ", |
| 188 | fs->device_name, |
| 189 | fs->super->s_blocks_count); |
| 190 | if (verbose) |
| 191 | printf("Running command: %s\n", buf); |
| 192 | f = popen(buf, "r"); |
| 193 | if (!f) { |
| 194 | com_err("popen", errno, |
| 195 | "while trying run '%s'", buf); |
| 196 | exit(1); |
| 197 | } |
| 198 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 199 | pclose(f); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 200 | if (retval) { |
| 201 | com_err("ext2fs_read_bb_FILE", retval, |
| 202 | "while processing list of bad blocks from program"); |
| 203 | exit(1); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) |
| 208 | { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 209 | int i, j; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 210 | int must_be_good; |
| 211 | blk_t blk; |
| 212 | badblocks_iterate bb_iter; |
| 213 | errcode_t retval; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 214 | blk_t group_block; |
| 215 | int group; |
| 216 | int group_bad; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 217 | |
| 218 | if (!bb_list) |
| 219 | return; |
| 220 | |
| 221 | /* |
| 222 | * The primary superblock and group descriptors *must* be |
| 223 | * good; if not, abort. |
| 224 | */ |
| 225 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; |
| 226 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { |
| 227 | if (badblocks_list_test(bb_list, i)) { |
| 228 | fprintf(stderr, "Block %d in primary superblock/group " |
| 229 | "descriptor area bad.\n", i); |
| 230 | fprintf(stderr, "Blocks %ld through %d must be good " |
| 231 | "in order to build a filesystem.\n", |
| 232 | fs->super->s_first_data_block, must_be_good); |
| 233 | fprintf(stderr, "Aborting....\n"); |
| 234 | exit(1); |
| 235 | } |
| 236 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * See if any of the bad blocks are showing up in the backup |
| 240 | * superblocks and/or group descriptors. If so, issue a |
| 241 | * warning and adjust the block counts appropriately. |
| 242 | */ |
| 243 | group_block = fs->super->s_first_data_block + |
| 244 | fs->super->s_blocks_per_group; |
| 245 | group_bad = 0; |
| 246 | |
| 247 | for (i = 1; i < fs->group_desc_count; i++) { |
| 248 | for (j=0; j < fs->desc_blocks+1; j++) { |
| 249 | if (badblocks_list_test(bb_list, group_block + |
| 250 | j)) { |
| 251 | if (!group_bad) |
| 252 | fprintf(stderr, |
| 253 | "Warning: the backup superblock/group descriptors at block %ld contain\n" |
| 254 | " bad blocks.\n\n", |
| 255 | group_block); |
| 256 | group_bad++; |
| 257 | group = ext2fs_group_of_blk(fs, group_block+j); |
| 258 | fs->group_desc[group].bg_free_blocks_count++; |
| 259 | fs->super->s_free_blocks_count++; |
| 260 | } |
| 261 | } |
| 262 | group_block += fs->super->s_blocks_per_group; |
| 263 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Mark all the bad blocks as used... |
| 267 | */ |
| 268 | retval = badblocks_list_iterate_begin(bb_list, &bb_iter); |
| 269 | if (retval) { |
| 270 | com_err("badblocks_list_iterate_begin", retval, |
| 271 | "while marking bad blocks as used"); |
| 272 | exit(1); |
| 273 | } |
| 274 | while (badblocks_list_iterate(bb_iter, &blk)) |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 275 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 276 | badblocks_list_iterate_end(bb_iter); |
| 277 | } |
| 278 | |
| 279 | static void new_table_block(ext2_filsys fs, blk_t first_block, |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 280 | const char *name, int num, int initialize, |
| 281 | const char *buf, blk_t *new_block) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 282 | { |
| 283 | errcode_t retval; |
| 284 | blk_t blk; |
| 285 | int i; |
| 286 | int count; |
| 287 | |
| 288 | retval = ext2fs_get_free_blocks(fs, first_block, |
| 289 | first_block + fs->super->s_blocks_per_group, |
| 290 | num, fs->block_map, new_block); |
| 291 | if (retval) { |
| 292 | printf("Could not allocate %d block(s) for %s: %s\n", |
| 293 | num, name, error_message(retval)); |
| 294 | ext2fs_unmark_valid(fs); |
| 295 | return; |
| 296 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 297 | if (initialize) { |
| 298 | blk = *new_block; |
| 299 | for (i=0; i < num; i += STRIDE_LENGTH, blk += STRIDE_LENGTH) { |
| 300 | if (num-i > STRIDE_LENGTH) |
| 301 | count = STRIDE_LENGTH; |
| 302 | else |
| 303 | count = num - i; |
| 304 | retval = io_channel_write_blk(fs->io, blk, count, buf); |
| 305 | if (retval) |
| 306 | printf("Warning: could not write %d blocks " |
| 307 | "starting at %ld for %s: %s\n", |
| 308 | count, blk, name, |
| 309 | error_message(retval)); |
| 310 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 311 | } |
| 312 | blk = *new_block; |
| 313 | for (i = 0; i < num; i++, blk++) |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 314 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void alloc_tables(ext2_filsys fs) |
| 318 | { |
| 319 | blk_t group_blk; |
| 320 | int i; |
| 321 | char *buf; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 322 | |
| 323 | buf = malloc(fs->blocksize * STRIDE_LENGTH); |
| 324 | if (!buf) { |
| 325 | com_err("malloc", ENOMEM, "while allocating zeroizing buffer"); |
| 326 | exit(1); |
| 327 | } |
| 328 | memset(buf, 0, fs->blocksize * STRIDE_LENGTH); |
| 329 | |
| 330 | group_blk = fs->super->s_first_data_block; |
| 331 | if (!quiet) |
| 332 | printf("Writing inode tables: "); |
| 333 | for (i = 0; i < fs->group_desc_count; i++) { |
| 334 | if (!quiet) |
| 335 | printf("%4d/%4ld", i, fs->group_desc_count); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 336 | new_table_block(fs, group_blk, "block bitmap", 1, 0, buf, |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 337 | &fs->group_desc[i].bg_block_bitmap); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 338 | new_table_block(fs, group_blk, "inode bitmap", 1, 0, buf, |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 339 | &fs->group_desc[i].bg_inode_bitmap); |
| 340 | new_table_block(fs, group_blk, "inode table", |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 341 | fs->inode_blocks_per_group, |
| 342 | !super_only, buf, |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 343 | &fs->group_desc[i].bg_inode_table); |
| 344 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 345 | group_blk += fs->super->s_blocks_per_group; |
| 346 | if (!quiet) |
| 347 | printf("\b\b\b\b\b\b\b\b\b"); |
| 348 | } |
| 349 | if (!quiet) |
| 350 | printf("done \n"); |
| 351 | } |
| 352 | |
| 353 | static void create_root_dir(ext2_filsys fs) |
| 354 | { |
| 355 | errcode_t retval; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 356 | struct ext2_inode inode; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 357 | |
| 358 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); |
| 359 | if (retval) { |
| 360 | com_err("ext2fs_mkdir", retval, "while creating root dir"); |
| 361 | exit(1); |
| 362 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 363 | if (geteuid()) { |
| 364 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); |
| 365 | if (retval) { |
| 366 | com_err("ext2fs_read_inode", retval, |
| 367 | "while reading root inode"); |
| 368 | exit(1); |
| 369 | } |
| 370 | inode.i_uid = geteuid(); |
| 371 | retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); |
| 372 | if (retval) { |
| 373 | com_err("ext2fs_write_inode", retval, |
| 374 | "while setting root inode ownership"); |
| 375 | exit(1); |
| 376 | } |
| 377 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void create_lost_and_found(ext2_filsys fs) |
| 381 | { |
| 382 | errcode_t retval; |
| 383 | ino_t ino; |
| 384 | const char *name = "lost+found"; |
| 385 | int i; |
| 386 | |
| 387 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); |
| 388 | if (retval) { |
| 389 | com_err("ext2fs_mkdir", retval, "while creating /lost+found"); |
| 390 | exit(1); |
| 391 | } |
| 392 | |
| 393 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); |
| 394 | if (retval) { |
| 395 | com_err("ext2_lookup", retval, "while looking up /lost+found"); |
| 396 | exit(1); |
| 397 | } |
| 398 | |
| 399 | for (i=1; i < EXT2_NDIR_BLOCKS; i++) { |
| 400 | retval = ext2fs_expand_dir(fs, ino); |
| 401 | if (retval) { |
| 402 | com_err("ext2fs_expand_dir", retval, |
| 403 | "while expanding /lost+found"); |
| 404 | exit(1); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) |
| 410 | { |
| 411 | errcode_t retval; |
| 412 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 413 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 414 | fs->group_desc[0].bg_free_inodes_count--; |
| 415 | fs->super->s_free_inodes_count--; |
| 416 | retval = ext2fs_update_bb_inode(fs, bb_list); |
| 417 | if (retval) { |
| 418 | com_err("ext2fs_update_bb_inode", retval, |
| 419 | "while setting bad block inode"); |
| 420 | exit(1); |
| 421 | } |
| 422 | |
| 423 | } |
| 424 | |
| 425 | static void reserve_inodes(ext2_filsys fs) |
| 426 | { |
| 427 | ino_t i; |
| 428 | int group; |
| 429 | |
| 430 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INO; i++) { |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 431 | ext2fs_mark_inode_bitmap(fs->inode_map, i); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 432 | group = ext2fs_group_of_ino(fs, i); |
| 433 | fs->group_desc[group].bg_free_inodes_count--; |
| 434 | fs->super->s_free_inodes_count--; |
| 435 | } |
| 436 | ext2fs_mark_ib_dirty(fs); |
| 437 | } |
| 438 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 439 | static void zap_bootblock(ext2_filsys fs) |
| 440 | { |
| 441 | char buf[512]; |
| 442 | int retval; |
| 443 | |
| 444 | memset(buf, 0, 512); |
| 445 | |
| 446 | retval = io_channel_write_blk(fs->io, 0, -512, buf); |
| 447 | if (retval) |
| 448 | printf("Warning: could not erase block 0: %s\n", |
| 449 | error_message(retval)); |
| 450 | } |
| 451 | |
| 452 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 453 | static void show_stats(ext2_filsys fs) |
| 454 | { |
| 455 | struct ext2_super_block *s = fs->super; |
| 456 | blk_t group_block; |
| 457 | int i, col_left; |
| 458 | |
| 459 | if (param.s_blocks_count != s->s_blocks_count) |
| 460 | printf("warning: %ld blocks unused.\n\n", |
| 461 | param.s_blocks_count - s->s_blocks_count); |
| 462 | |
| 463 | printf("%lu inodes, %lu blocks\n", s->s_inodes_count, |
| 464 | s->s_blocks_count); |
| 465 | printf("%lu blocks (%2.2f%%) reserved for the super user\n", |
| 466 | s->s_r_blocks_count, |
| 467 | 100.0 * s->s_r_blocks_count / s->s_blocks_count); |
| 468 | printf("First data block=%lu\n", s->s_first_data_block); |
| 469 | printf("Block size=%u (log=%lu)\n", fs->blocksize, |
| 470 | s->s_log_block_size); |
| 471 | printf("Fragment size=%u (log=%lu)\n", fs->fragsize, |
| 472 | s->s_log_frag_size); |
| 473 | printf("%lu block group%s\n", fs->group_desc_count, |
| 474 | (fs->group_desc_count > 1) ? "s" : ""); |
| 475 | printf("%lu blocks per group, %lu fragments per group\n", |
| 476 | s->s_blocks_per_group, s->s_frags_per_group); |
| 477 | printf("%lu inodes per group\n", s->s_inodes_per_group); |
| 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; |
| 489 | if (!col_left--) { |
| 490 | printf("\n\t"); |
| 491 | col_left = 8; |
| 492 | } |
| 493 | printf("%lu", group_block); |
| 494 | if (i != fs->group_desc_count - 1) |
| 495 | printf(", "); |
| 496 | } |
| 497 | printf("\n\n"); |
| 498 | } |
| 499 | |
| 500 | static void PRS(int argc, char *argv[]) |
| 501 | { |
| 502 | char c; |
| 503 | int size; |
| 504 | char * tmp; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 505 | char *oldpath; |
| 506 | static char newpath[PATH_MAX]; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 507 | int inode_ratio = 4096; |
| 508 | int reserved_ratio = 5; |
| 509 | |
| 510 | /* Update our PATH to include /sbin */ |
| 511 | strcpy(newpath, "PATH=/sbin:"); |
| 512 | if ((oldpath = getenv("PATH")) != NULL) |
| 513 | strcat(newpath, oldpath); |
| 514 | putenv(newpath); |
| 515 | |
| 516 | setbuf(stdout, NULL); |
| 517 | setbuf(stderr, NULL); |
| 518 | initialize_ext2_error_table(); |
| 519 | memset(¶m, 0, sizeof(struct ext2_super_block)); |
| 520 | |
| 521 | fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n", |
| 522 | E2FSPROGS_VERSION, E2FSPROGS_DATE, |
| 523 | EXT2FS_VERSION, EXT2FS_DATE); |
| 524 | if (argc && *argv) |
| 525 | program_name = *argv; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 526 | while ((c = getopt (argc, argv, "b:cf:g:i:l:m:qtvS")) != EOF) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 527 | switch (c) { |
| 528 | case 'b': |
| 529 | size = strtoul(optarg, &tmp, 0); |
| 530 | if (size < 1024 || size > 4096 || *tmp) { |
| 531 | com_err(program_name, 0, "bad block size - %s", |
| 532 | optarg); |
| 533 | exit(1); |
| 534 | } |
| 535 | param.s_log_block_size = |
| 536 | log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 537 | break; |
| 538 | case 'c': |
| 539 | case 't': /* Check for bad blocks */ |
| 540 | cflag = 1; |
| 541 | break; |
| 542 | case 'f': |
| 543 | size = strtoul(optarg, &tmp, 0); |
| 544 | if (size < 1024 || size > 4096 || *tmp) { |
| 545 | com_err(program_name, 0, "bad fragment size - %s", |
| 546 | optarg); |
| 547 | exit(1); |
| 548 | } |
| 549 | param.s_log_frag_size = |
| 550 | log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 551 | printf("Warning: fragments not supported. " |
| 552 | "Ignoring -f option\n"); |
| 553 | break; |
| 554 | case 'g': |
| 555 | param.s_blocks_per_group = strtoul(optarg, &tmp, 0); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 556 | if (*tmp) { |
| 557 | com_err(program_name, 0, |
| 558 | "Illegal number for blocks per group"); |
| 559 | exit(1); |
| 560 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 561 | if (param.s_blocks_per_group < 256 || |
| 562 | param.s_blocks_per_group > 8192 || *tmp) { |
| 563 | com_err(program_name, 0, |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 564 | "blocks per group count out of range"); |
| 565 | exit(1); |
| 566 | } |
| 567 | if ((param.s_blocks_per_group % 8) != 0) { |
| 568 | com_err(program_name, 0, |
| 569 | "blocks per group must be multiple of 8"); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 570 | exit(1); |
| 571 | } |
| 572 | break; |
| 573 | case 'i': |
| 574 | inode_ratio = strtoul(optarg, &tmp, 0); |
| 575 | if (inode_ratio < 1024 || inode_ratio > 256 * 1024 || |
| 576 | *tmp) { |
| 577 | com_err(program_name, 0, "bad inode ratio - %s", |
| 578 | optarg); |
| 579 | exit(1); |
| 580 | } |
| 581 | break; |
| 582 | case 'l': |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 583 | bad_blocks_filename = malloc(strlen(optarg)+1); |
| 584 | if (!bad_blocks_filename) { |
| 585 | com_err(program_name, ENOMEM, |
| 586 | "in malloc for bad_blocks_filename"); |
| 587 | exit(1); |
| 588 | } |
| 589 | strcpy(bad_blocks_filename, optarg); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 590 | break; |
| 591 | case 'm': |
| 592 | reserved_ratio = strtoul(optarg, &tmp, 0); |
| 593 | if (reserved_ratio > 50 || *tmp) { |
| 594 | com_err(program_name, 0, |
| 595 | "bad reserved blocks percent - %s", |
| 596 | optarg); |
| 597 | exit(1); |
| 598 | } |
| 599 | break; |
| 600 | case 'v': |
| 601 | verbose = 1; |
| 602 | break; |
| 603 | case 'q': |
| 604 | quiet = 1; |
| 605 | break; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 606 | case 'S': |
| 607 | super_only = 1; |
| 608 | break; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 609 | default: |
| 610 | usage(); |
| 611 | } |
| 612 | if (optind == argc) |
| 613 | usage(); |
| 614 | device_name = argv[optind]; |
| 615 | optind++; |
| 616 | if (optind < argc) { |
| 617 | param.s_blocks_count = strtoul(argv[optind++], &tmp, 0); |
| 618 | if (*tmp) { |
| 619 | com_err(program_name, 0, "bad blocks count - %s", |
| 620 | argv[optind - 1]); |
| 621 | exit(1); |
| 622 | } |
| 623 | } |
| 624 | if (optind < argc) |
| 625 | usage(); |
| 626 | param.s_log_frag_size = param.s_log_block_size; |
| 627 | |
| 628 | if (!param.s_blocks_count) |
| 629 | param.s_blocks_count = get_size(device_name); |
| 630 | |
| 631 | /* |
| 632 | * Calculate number of inodes based on the inode ratio |
| 633 | */ |
| 634 | param.s_inodes_count = |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 635 | ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m)) |
| 636 | / inode_ratio; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * Calculate number of blocks to reserve |
| 640 | */ |
| 641 | param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; |
| 642 | } |
| 643 | |
| 644 | int main (int argc, char *argv[]) |
| 645 | { |
| 646 | errcode_t retval = 0; |
| 647 | ext2_filsys fs; |
| 648 | badblocks_list bb_list = 0; |
| 649 | |
| 650 | PRS(argc, argv); |
| 651 | |
| 652 | check_mount(); |
| 653 | |
| 654 | /* |
| 655 | * Initialize the superblock.... |
| 656 | */ |
| 657 | retval = ext2fs_initialize(device_name, 0, ¶m, |
| 658 | unix_io_manager, &fs); |
| 659 | if (retval) { |
| 660 | com_err(device_name, retval, "while setting up superblock"); |
| 661 | exit(1); |
| 662 | } |
| 663 | |
| 664 | if (!quiet) |
| 665 | show_stats(fs); |
| 666 | |
| 667 | if (bad_blocks_filename) |
| 668 | read_bb_file(fs, &bb_list, bad_blocks_filename); |
| 669 | if (cflag) |
| 670 | test_disk(fs, &bb_list); |
| 671 | |
| 672 | handle_bad_blocks(fs, bb_list); |
| 673 | alloc_tables(fs); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 674 | if (super_only) { |
| 675 | fs->super->s_state |= EXT2_ERROR_FS; |
| 676 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); |
| 677 | } else { |
| 678 | create_root_dir(fs); |
| 679 | create_lost_and_found(fs); |
| 680 | reserve_inodes(fs); |
| 681 | create_bad_block_inode(fs, bb_list); |
| 682 | zap_bootblock(fs); |
| 683 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 684 | |
| 685 | if (!quiet) |
| 686 | printf("Writing superblocks and " |
| 687 | "filesystem accounting information: "); |
| 688 | ext2fs_close(fs); |
| 689 | if (!quiet) |
| 690 | printf("done\n"); |
| 691 | return 0; |
| 692 | } |