| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * unix.c - The unix-specific code for e2fsck |
| 3 | * |
| 4 | * Copyright (C) 1993, 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% |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #ifdef HAVE_STDLIB_H |
| 14 | #include <stdlib.h> |
| 15 | #endif |
| 16 | #include <string.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <ctype.h> |
| 19 | #include <termios.h> |
| 20 | #include <time.h> |
| 21 | #ifdef HAVE_GETOPT_H |
| 22 | #include <getopt.h> |
| 23 | #endif |
| 24 | #include <unistd.h> |
| 25 | #ifdef HAVE_ERRNO_H |
| 26 | #include <errno.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_MNTENT_H |
| 29 | #include <mntent.h> |
| 30 | #endif |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <malloc.h> |
| 33 | |
| 34 | #include "et/com_err.h" |
| 35 | #include "e2fsck.h" |
| 36 | #include "problem.h" |
| 37 | #include "../version.h" |
| 38 | |
| 39 | extern int isatty(int); |
| 40 | |
| 41 | /* Command line options */ |
| 42 | static int blocksize = 0; |
| 43 | static int swapfs = 0; |
| 44 | static int normalize_swapfs = 0; |
| 45 | static int cflag = 0; /* check disk */ |
| 46 | static int show_version_only = 0; |
| 47 | static int force = 0; |
| 48 | static int verbose = 0; |
| 49 | |
| 50 | static int replace_bad_blocks = 0; |
| 51 | static char *bad_blocks_file = 0; |
| 52 | |
| 53 | static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0}; |
| 54 | |
| 55 | static int root_filesystem = 0; |
| 56 | static int read_only_root = 0; |
| 57 | |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 58 | static void usage(e2fsck_t ctx) |
| 59 | { |
| 60 | fprintf(stderr, |
| 61 | "Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n" |
| 62 | "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n" |
| 63 | "\t\t[-l|-L bad_blocks_file] device\n", ctx->program_name); |
| 64 | exit(FSCK_USAGE); |
| 65 | } |
| 66 | |
| 67 | static void show_stats(e2fsck_t ctx) |
| 68 | { |
| 69 | ext2_filsys fs = ctx->fs; |
| 70 | int inodes, inodes_used, blocks, blocks_used; |
| 71 | int dir_links; |
| 72 | int num_files, num_links; |
| 73 | int frag_percent; |
| 74 | |
| 75 | dir_links = 2 * ctx->fs_directory_count - 1; |
| 76 | num_files = ctx->fs_total_count - dir_links; |
| 77 | num_links = ctx->fs_links_count - dir_links; |
| 78 | inodes = fs->super->s_inodes_count; |
| 79 | inodes_used = (fs->super->s_inodes_count - |
| 80 | fs->super->s_free_inodes_count); |
| 81 | blocks = fs->super->s_blocks_count; |
| 82 | blocks_used = (fs->super->s_blocks_count - |
| 83 | fs->super->s_free_blocks_count); |
| 84 | |
| 85 | frag_percent = (10000 * ctx->fs_fragmented) / inodes_used; |
| 86 | frag_percent = (frag_percent + 5) / 10; |
| 87 | |
| 88 | if (!verbose) { |
| 89 | printf("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n", |
| 90 | ctx->device_name, inodes_used, inodes, |
| 91 | frag_percent / 10, frag_percent % 10, |
| 92 | blocks_used, blocks); |
| 93 | return; |
| 94 | } |
| 95 | printf ("\n%8d inode%s used (%d%%)\n", inodes_used, |
| 96 | (inodes_used != 1) ? "s" : "", |
| 97 | 100 * inodes_used / inodes); |
| 98 | printf ("%8d non-contiguous inodes (%0d.%d%%)\n", |
| 99 | ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); |
| 100 | printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n", |
| 101 | ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); |
| 102 | printf ("%8d block%s used (%d%%)\n" |
| 103 | "%8d bad block%s\n", blocks_used, |
| 104 | (blocks_used != 1) ? "s" : "", |
| 105 | 100 * blocks_used / blocks, ctx->fs_badblocks_count, |
| 106 | ctx->fs_badblocks_count != 1 ? "s" : ""); |
| 107 | printf ("\n%8d regular file%s\n" |
| 108 | "%8d director%s\n" |
| 109 | "%8d character device file%s\n" |
| 110 | "%8d block device file%s\n" |
| 111 | "%8d fifo%s\n" |
| 112 | "%8d link%s\n" |
| 113 | "%8d symbolic link%s (%d fast symbolic link%s)\n" |
| 114 | "%8d socket%s\n" |
| 115 | "--------\n" |
| 116 | "%8d file%s\n", |
| 117 | ctx->fs_regular_count, |
| 118 | (ctx->fs_regular_count != 1) ? "s" : "", |
| 119 | ctx->fs_directory_count, |
| 120 | (ctx->fs_directory_count != 1) ? "ies" : "y", |
| 121 | ctx->fs_chardev_count, |
| 122 | (ctx->fs_chardev_count != 1) ? "s" : "", |
| 123 | ctx->fs_blockdev_count, |
| 124 | (ctx->fs_blockdev_count != 1) ? "s" : "", |
| 125 | ctx->fs_fifo_count, |
| 126 | (ctx->fs_fifo_count != 1) ? "s" : "", |
| 127 | ctx->fs_links_count - dir_links, |
| 128 | ((ctx->fs_links_count - dir_links) != 1) ? "s" : "", |
| 129 | ctx->fs_symlinks_count, |
| 130 | (ctx->fs_symlinks_count != 1) ? "s" : "", |
| 131 | ctx->fs_fast_symlinks_count, |
| 132 | (ctx->fs_fast_symlinks_count != 1) ? "s" : "", |
| 133 | ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "", |
| 134 | ctx->fs_total_count - dir_links, |
| 135 | ((ctx->fs_total_count - dir_links) != 1) ? "s" : ""); |
| 136 | } |
| 137 | |
| 138 | static void check_mount(e2fsck_t ctx) |
| 139 | { |
| 140 | errcode_t retval; |
| 141 | int mount_flags, cont, fd; |
| 142 | |
| 143 | retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); |
| 144 | if (retval) { |
| 145 | com_err("ext2fs_check_if_mount", retval, |
| 146 | "while determining whether %s is mounted.", |
| 147 | ctx->filesystem_name); |
| 148 | return; |
| 149 | } |
| 150 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
| 151 | return; |
| 152 | |
| 153 | #if (defined(__linux__) && defined(HAVE_MNTENT_H)) |
| 154 | /* |
| 155 | * If the root is mounted read-only, then /etc/mtab is |
| 156 | * probably not correct; so we won't issue a warning based on |
| 157 | * it. |
| 158 | */ |
| 159 | fd = open(MOUNTED, O_RDWR); |
| 160 | if (fd < 0) { |
| 161 | if (errno == EROFS) |
| 162 | return; |
| 163 | } else |
| 164 | close(fd); |
| 165 | #endif |
| 166 | |
| 167 | if (ctx->options & E2F_OPT_READONLY) { |
| 168 | printf("Warning! %s is mounted.\n", ctx->device_name); |
| 169 | return; |
| 170 | } |
| 171 | |
| Theodore Ts'o | 7403335 | 1999-07-01 03:00:47 +0000 | [diff] [blame^] | 172 | printf("%s is mounted. ", ctx->device_name); |
| 173 | if (!isatty(0) || !isatty(1)) { |
| 174 | printf("Cannot continue, aborting.\n\n"); |
| 175 | exit(FSCK_ERROR); |
| 176 | } |
| 177 | printf("\n\n\007\007\007\007WARNING!!! " |
| 178 | "Running e2fsck on a mounted filesystem may cause\n" |
| 179 | "SEVERE filesystem damage.\007\007\007\n\n"); |
| 180 | cont = ask_yn("Do you really want to continue", -1); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 181 | if (!cont) { |
| 182 | printf ("check aborted.\n"); |
| 183 | exit (0); |
| 184 | } |
| 185 | return; |
| 186 | } |
| 187 | |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 188 | /* |
| 189 | * This routine checks to see if a filesystem can be skipped; if so, |
| 190 | * it will exit with E2FSCK_OK. Under some conditions it will print a |
| 191 | * message explaining why a check is being forced. |
| 192 | */ |
| 193 | static void check_if_skip(e2fsck_t ctx) |
| 194 | { |
| 195 | ext2_filsys fs = ctx->fs; |
| 196 | const char *reason = NULL; |
| 197 | |
| 198 | if (force || bad_blocks_file || cflag || swapfs) |
| 199 | return; |
| 200 | |
| 201 | if (fs->super->s_state & EXT2_ERROR_FS) |
| 202 | reason = "contains a file system with errors"; |
| Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 203 | else if ((fs->super->s_state & EXT2_VALID_FS) == 0) |
| 204 | reason = "was not cleanly unmounted"; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 205 | else if (fs->super->s_mnt_count >= |
| 206 | (unsigned) fs->super->s_max_mnt_count) |
| 207 | reason = "has reached maximal mount count"; |
| 208 | else if (fs->super->s_checkinterval && |
| 209 | time(0) >= (fs->super->s_lastcheck + |
| 210 | fs->super->s_checkinterval)) |
| 211 | reason = "has gone too long without being checked"; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 212 | if (reason) { |
| 213 | printf("%s %s, check forced.\n", ctx->device_name, reason); |
| 214 | return; |
| 215 | } |
| 216 | printf("%s: clean, %d/%d files, %d/%d blocks\n", ctx->device_name, |
| 217 | fs->super->s_inodes_count - fs->super->s_free_inodes_count, |
| 218 | fs->super->s_inodes_count, |
| 219 | fs->super->s_blocks_count - fs->super->s_free_blocks_count, |
| 220 | fs->super->s_blocks_count); |
| 221 | ext2fs_close(fs); |
| 222 | exit(FSCK_OK); |
| 223 | } |
| 224 | |
| Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 225 | /* |
| 226 | * For completion notice |
| 227 | */ |
| 228 | static int e2fsck_update_progress(e2fsck_t ctx, int pass, |
| 229 | unsigned long cur, unsigned long max) |
| 230 | { |
| 231 | const char spinner[] = "\\|/-"; |
| Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 232 | char buf[80]; |
| Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 233 | |
| 234 | if (pass == 0) |
| 235 | return 0; |
| Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 236 | |
| 237 | if (ctx->progress_fd) { |
| 238 | sprintf(buf, "%d %lu %lu\n", pass, cur, max); |
| 239 | write(ctx->progress_fd, buf, strlen(buf)); |
| 240 | } else { |
| 241 | ctx->progress_pos = (ctx->progress_pos+1) & 3; |
| 242 | fputc(spinner[ctx->progress_pos], stdout); |
| 243 | fputc('\b', stdout); |
| 244 | fflush(stdout); |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 248 | |
| 249 | #define PATH_SET "PATH=/sbin" |
| 250 | |
| Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 251 | static void reserve_stdio_fds(NOARGS) |
| 252 | { |
| 253 | int fd; |
| 254 | |
| 255 | while (1) { |
| 256 | fd = open("/dev/null", O_RDWR); |
| 257 | if (fd > 2) |
| 258 | break; |
| Theodore Ts'o | 75d83be | 1999-05-18 03:16:36 +0000 | [diff] [blame] | 259 | if (fd < 0) { |
| Theodore Ts'o | 813bbb2 | 1999-06-22 03:17:45 +0000 | [diff] [blame] | 260 | fprintf(stderr, "ERROR: Couldn't open " |
| 261 | "/dev/null (%s)\n", |
| Theodore Ts'o | 75d83be | 1999-05-18 03:16:36 +0000 | [diff] [blame] | 262 | strerror(errno)); |
| 263 | break; |
| 264 | } |
| Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 265 | } |
| 266 | close(fd); |
| 267 | } |
| 268 | |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 269 | static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx) |
| 270 | { |
| 271 | int flush = 0; |
| Theodore Ts'o | 519149f | 1997-10-25 03:49:49 +0000 | [diff] [blame] | 272 | int c; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 273 | #ifdef MTRACE |
| 274 | extern void *mallwatch; |
| 275 | #endif |
| 276 | char *oldpath = getenv("PATH"); |
| 277 | e2fsck_t ctx; |
| 278 | errcode_t retval; |
| 279 | |
| 280 | retval = e2fsck_allocate_context(&ctx); |
| 281 | if (retval) |
| 282 | return retval; |
| 283 | |
| 284 | *ret_ctx = ctx; |
| 285 | |
| 286 | /* Update our PATH to include /sbin */ |
| 287 | if (oldpath) { |
| 288 | char *newpath; |
| 289 | |
| Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 290 | newpath = (char *) malloc(sizeof (PATH_SET) + 1 + |
| 291 | strlen (oldpath)); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 292 | if (!newpath) |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 293 | fatal_error(ctx, "Couldn't malloc() newpath"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 294 | strcpy (newpath, PATH_SET); |
| 295 | strcat (newpath, ":"); |
| 296 | strcat (newpath, oldpath); |
| 297 | putenv (newpath); |
| 298 | } else |
| 299 | putenv (PATH_SET); |
| 300 | |
| 301 | setbuf(stdout, NULL); |
| 302 | setbuf(stderr, NULL); |
| 303 | initialize_ext2_error_table(); |
| 304 | |
| 305 | if (argc && *argv) |
| 306 | ctx->program_name = *argv; |
| 307 | else |
| 308 | ctx->program_name = "e2fsck"; |
| Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 309 | while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF) |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 310 | switch (c) { |
| Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 311 | case 'C': |
| 312 | ctx->progress = e2fsck_update_progress; |
| 313 | ctx->progress_fd = atoi(optarg); |
| 314 | break; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 315 | case 'p': |
| 316 | case 'a': |
| 317 | ctx->options |= E2F_OPT_PREEN; |
| 318 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO); |
| 319 | break; |
| 320 | case 'n': |
| 321 | ctx->options |= E2F_OPT_NO; |
| 322 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN); |
| 323 | break; |
| 324 | case 'y': |
| 325 | ctx->options |= E2F_OPT_YES; |
| 326 | ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO); |
| 327 | break; |
| 328 | case 't': |
| Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 329 | #ifdef RESOURCE_TRACK |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 330 | if (ctx->options & E2F_OPT_TIME) |
| 331 | ctx->options |= E2F_OPT_TIME2; |
| 332 | else |
| 333 | ctx->options |= E2F_OPT_TIME; |
| Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 334 | #else |
| 335 | fprintf(stderr, "The -t option is not " |
| 336 | "supported on this version of e2fsck.\n"); |
| 337 | #endif |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 338 | break; |
| 339 | case 'c': |
| 340 | cflag++; |
| 341 | ctx->options |= E2F_OPT_CHECKBLOCKS; |
| 342 | break; |
| 343 | case 'r': |
| 344 | /* What we do by default, anyway! */ |
| 345 | break; |
| 346 | case 'b': |
| 347 | ctx->use_superblock = atoi(optarg); |
| 348 | break; |
| 349 | case 'B': |
| 350 | blocksize = atoi(optarg); |
| 351 | break; |
| 352 | case 'I': |
| 353 | ctx->inode_buffer_blocks = atoi(optarg); |
| 354 | break; |
| 355 | case 'P': |
| 356 | ctx->process_inode_size = atoi(optarg); |
| 357 | break; |
| 358 | case 'L': |
| 359 | replace_bad_blocks++; |
| 360 | case 'l': |
| Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 361 | bad_blocks_file = (char *) malloc(strlen(optarg)+1); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 362 | if (!bad_blocks_file) |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 363 | fatal_error(ctx, |
| 364 | "Couldn't malloc bad_blocks_file"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 365 | strcpy(bad_blocks_file, optarg); |
| 366 | break; |
| 367 | case 'd': |
| 368 | ctx->options |= E2F_OPT_DEBUG; |
| 369 | break; |
| 370 | case 'f': |
| 371 | force = 1; |
| 372 | break; |
| 373 | case 'F': |
| 374 | #ifdef BLKFLSBUF |
| 375 | flush = 1; |
| 376 | #else |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 377 | fatal_error(ctx, "-F not supported"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 378 | #endif |
| 379 | break; |
| 380 | case 'v': |
| 381 | verbose = 1; |
| 382 | break; |
| 383 | case 'V': |
| 384 | show_version_only = 1; |
| 385 | break; |
| 386 | #ifdef MTRACE |
| 387 | case 'M': |
| 388 | mallwatch = (void *) strtol(optarg, NULL, 0); |
| 389 | break; |
| 390 | #endif |
| 391 | case 'N': |
| 392 | ctx->device_name = optarg; |
| 393 | break; |
| 394 | case 's': |
| 395 | normalize_swapfs = 1; |
| 396 | case 'S': |
| 397 | swapfs = 1; |
| 398 | break; |
| 399 | default: |
| 400 | usage(ctx); |
| 401 | } |
| 402 | if (show_version_only) |
| 403 | return 0; |
| 404 | if (optind != argc - 1) |
| 405 | usage(ctx); |
| 406 | if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file && |
| 407 | !cflag && !swapfs) |
| 408 | ctx->options |= E2F_OPT_READONLY; |
| 409 | ctx->filesystem_name = argv[optind]; |
| 410 | if (ctx->device_name == 0) |
| 411 | ctx->device_name = ctx->filesystem_name; |
| 412 | if (flush) { |
| 413 | #ifdef BLKFLSBUF |
| 414 | int fd = open(ctx->filesystem_name, O_RDONLY, 0); |
| 415 | |
| 416 | if (fd < 0) { |
| 417 | com_err("open", errno, "while opening %s for flushing", |
| 418 | ctx->filesystem_name); |
| 419 | exit(FSCK_ERROR); |
| 420 | } |
| 421 | if (ioctl(fd, BLKFLSBUF, 0) < 0) { |
| 422 | com_err("BLKFLSBUF", errno, "while trying to flush %s", |
| 423 | ctx->filesystem_name); |
| 424 | exit(FSCK_ERROR); |
| 425 | } |
| 426 | close(fd); |
| 427 | #else |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 428 | fatal_error(ctx, "BLKFLSBUF not supported"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 429 | #endif /* BLKFLSBUF */ |
| 430 | } |
| 431 | if (swapfs) { |
| 432 | if (cflag || bad_blocks_file) { |
| 433 | fprintf(stderr, "Incompatible options not " |
| 434 | "allowed when byte-swapping.\n"); |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 435 | exit(FSCK_ERROR); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static const char *my_ver_string = E2FSPROGS_VERSION; |
| 442 | static const char *my_ver_date = E2FSPROGS_DATE; |
| 443 | |
| 444 | int main (int argc, char *argv[]) |
| 445 | { |
| 446 | errcode_t retval = 0; |
| 447 | int exit_value = FSCK_OK; |
| 448 | int i; |
| 449 | ext2_filsys fs = 0; |
| 450 | io_manager io_ptr; |
| 451 | struct ext2fs_sb *s; |
| 452 | const char *lib_ver_date; |
| 453 | int my_ver, lib_ver; |
| 454 | e2fsck_t ctx; |
| 455 | struct problem_context pctx; |
| Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 456 | int flags, run_result; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 457 | |
| 458 | clear_problem_context(&pctx); |
| 459 | #ifdef MTRACE |
| 460 | mtrace(); |
| 461 | #endif |
| 462 | #ifdef MCHECK |
| 463 | mcheck(0); |
| 464 | #endif |
| 465 | my_ver = ext2fs_parse_version_string(my_ver_string); |
| 466 | lib_ver = ext2fs_get_library_version(0, &lib_ver_date); |
| 467 | if (my_ver > lib_ver) { |
| 468 | fprintf( stderr, "Error: ext2fs library version " |
| 469 | "out of date!\n"); |
| 470 | show_version_only++; |
| 471 | } |
| 472 | |
| 473 | retval = PRS(argc, argv, &ctx); |
| 474 | if (retval) { |
| 475 | com_err("e2fsck", retval, |
| 476 | "while trying to initialize program"); |
| 477 | exit(1); |
| 478 | } |
| Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 479 | reserve_stdio_fds(); |
| 480 | |
| Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 481 | #ifdef RESOURCE_TRACK |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 482 | init_resource_track(&ctx->global_rtrack); |
| Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 483 | #endif |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 484 | |
| 485 | if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) |
| 486 | fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n", |
| 487 | my_ver_string, my_ver_date, EXT2FS_VERSION, |
| 488 | EXT2FS_DATE); |
| 489 | |
| 490 | if (show_version_only) { |
| 491 | fprintf(stderr, "\tUsing %s, %s\n", |
| 492 | error_message(EXT2_ET_BASE), lib_ver_date); |
| 493 | exit(0); |
| 494 | } |
| 495 | |
| 496 | check_mount(ctx); |
| 497 | |
| 498 | if (!(ctx->options & E2F_OPT_PREEN) && |
| 499 | !(ctx->options & E2F_OPT_NO) && |
| 500 | !(ctx->options & E2F_OPT_YES)) { |
| 501 | if (!isatty (0) || !isatty (1)) |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 502 | fatal_error(ctx, |
| 503 | "need terminal for interactive repairs"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 504 | } |
| 505 | ctx->superblock = ctx->use_superblock; |
| 506 | restart: |
| 507 | #if 1 |
| 508 | io_ptr = unix_io_manager; |
| 509 | #else |
| 510 | io_ptr = test_io_manager; |
| 511 | test_io_backing_manager = unix_io_manager; |
| 512 | #endif |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 513 | flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW; |
| 514 | if (ctx->superblock && blocksize) { |
| 515 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 516 | ctx->superblock, blocksize, io_ptr, &fs); |
| 517 | } else if (ctx->superblock) { |
| 518 | for (i=0; possible_block_sizes[i]; i++) { |
| 519 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 520 | ctx->superblock, |
| 521 | possible_block_sizes[i], |
| 522 | io_ptr, &fs); |
| 523 | if (!retval) |
| 524 | break; |
| 525 | } |
| 526 | } else |
| 527 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 528 | 0, 0, io_ptr, &fs); |
| 529 | if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && |
| 530 | ((retval == EXT2_ET_BAD_MAGIC) || |
| 531 | ((retval == 0) && ext2fs_check_desc(fs)))) { |
| 532 | if (!fs || (fs->group_desc_count > 1)) { |
| 533 | printf("%s trying backup blocks...\n", |
| 534 | retval ? "Couldn't find ext2 superblock," : |
| 535 | "Group descriptors look bad..."); |
| 536 | ctx->superblock = get_backup_sb(fs); |
| 537 | if (fs) |
| 538 | ext2fs_close(fs); |
| 539 | goto restart; |
| 540 | } |
| 541 | } |
| 542 | if (retval) { |
| 543 | com_err(ctx->program_name, retval, "while trying to open %s", |
| 544 | ctx->filesystem_name); |
| Theodore Ts'o | 24dd402 | 1998-02-01 00:16:40 +0000 | [diff] [blame] | 545 | if (retval == EXT2_ET_REV_TOO_HIGH) { |
| 546 | printf("The filesystem revision is apparently " |
| 547 | "too high for this version of e2fsck.\n" |
| 548 | "(Or the filesystem superblock " |
| 549 | "is corrupt)\n\n"); |
| 550 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); |
| 551 | } else if (retval == EXT2_ET_SHORT_READ) |
| 552 | printf("Could this be a zero-length partition?\n"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 553 | else if ((retval == EPERM) || (retval == EACCES)) |
| 554 | printf("You must have %s access to the " |
| 555 | "filesystem or be root\n", |
| 556 | (ctx->options & E2F_OPT_READONLY) ? |
| 557 | "r/o" : "r/w"); |
| 558 | else if (retval == ENXIO) |
| 559 | printf("Possibly non-existent or swap device?\n"); |
| Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame] | 560 | #ifdef EROFS |
| 561 | else if (retval == EROFS) |
| Theodore Ts'o | 813bbb2 | 1999-06-22 03:17:45 +0000 | [diff] [blame] | 562 | printf("Disk write-protected; use the -n option " |
| Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame] | 563 | "to do a read-only\n" |
| 564 | "check of the device.\n"); |
| Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame] | 565 | #endif |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 566 | else |
| 567 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 568 | exit(FSCK_ERROR); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 569 | } |
| 570 | ctx->fs = fs; |
| Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 571 | fs->priv_data = ctx; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 572 | #ifdef EXT2_CURRENT_REV |
| 573 | if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) { |
| 574 | com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, |
| 575 | "while trying to open %s", |
| 576 | ctx->filesystem_name); |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 577 | get_newer: |
| 578 | fatal_error(ctx, "Get a newer version of e2fsck!"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 579 | } |
| 580 | #endif |
| 581 | /* |
| 582 | * Check for compatibility with the feature sets. We need to |
| 583 | * be more stringent than ext2fs_open(). |
| 584 | */ |
| 585 | s = (struct ext2fs_sb *) fs->super; |
| 586 | if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || |
| Theodore Ts'o | 8144d67 | 1998-07-09 05:33:18 +0000 | [diff] [blame] | 587 | (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 588 | com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE, |
| 589 | "(%s)", ctx->filesystem_name); |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 590 | goto get_newer; |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 591 | } |
| 592 | if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { |
| 593 | com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE, |
| 594 | "(%s)", ctx->filesystem_name); |
| 595 | goto get_newer; |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * If the user specified a specific superblock, presumably the |
| 600 | * master superblock has been trashed. So we mark the |
| 601 | * superblock as dirty, so it can be written out. |
| 602 | */ |
| 603 | if (ctx->superblock && |
| 604 | !(ctx->options & E2F_OPT_READONLY)) |
| 605 | ext2fs_mark_super_dirty(fs); |
| 606 | |
| 607 | /* |
| 608 | * Don't overwrite the backup superblock and block |
| 609 | * descriptors, until we're sure the filesystem is OK.... |
| 610 | */ |
| 611 | fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
| 612 | |
| 613 | ehandler_init(fs->io); |
| 614 | |
| 615 | if (ctx->superblock) |
| 616 | set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); |
| 617 | check_super_block(ctx); |
| Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 618 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 619 | exit(FSCK_ERROR); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 620 | check_if_skip(ctx); |
| 621 | if (bad_blocks_file) |
| 622 | read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); |
| 623 | else if (cflag) |
| 624 | test_disk(ctx); |
| Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 625 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 626 | exit(FSCK_ERROR); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 627 | |
| 628 | if (normalize_swapfs) { |
| 629 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == |
| 630 | ext2fs_native_flag()) { |
| 631 | fprintf(stderr, "%s: Filesystem byte order " |
| 632 | "already normalized.\n", ctx->device_name); |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 633 | exit(FSCK_ERROR); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 636 | if (swapfs) { |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 637 | swap_filesys(ctx); |
| Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 638 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 639 | exit(FSCK_ERROR); |
| 640 | } |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 641 | |
| 642 | /* |
| 643 | * Mark the system as valid, 'til proven otherwise |
| 644 | */ |
| 645 | ext2fs_mark_valid(fs); |
| 646 | |
| 647 | retval = ext2fs_read_bb_inode(fs, &fs->badblocks); |
| 648 | if (retval) { |
| 649 | com_err(ctx->program_name, retval, |
| 650 | "while reading bad blocks inode"); |
| 651 | preenhalt(ctx); |
| 652 | printf("This doesn't bode well, but we'll try to go on...\n"); |
| 653 | } |
| Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 654 | |
| 655 | run_result = e2fsck_run(ctx); |
| 656 | if (run_result == E2F_FLAG_RESTART) { |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 657 | printf("Restarting e2fsck from the beginning...\n"); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 658 | retval = e2fsck_reset_context(ctx); |
| 659 | if (retval) { |
| 660 | com_err(ctx->program_name, retval, |
| 661 | "while resetting context"); |
| 662 | exit(1); |
| 663 | } |
| Theodore Ts'o | 73f17cf | 1999-01-04 07:35:45 +0000 | [diff] [blame] | 664 | ext2fs_close(fs); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 665 | goto restart; |
| 666 | } |
| Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 667 | if (run_result & E2F_FLAG_SIGNAL_MASK) |
| Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 668 | exit(FSCK_ERROR); |
| 669 | if (run_result & E2F_FLAG_CANCEL) |
| 670 | ext2fs_unmark_valid(fs); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 671 | |
| 672 | #ifdef MTRACE |
| 673 | mtrace_print("Cleanup"); |
| 674 | #endif |
| 675 | if (ext2fs_test_changed(fs)) { |
| 676 | exit_value = FSCK_NONDESTRUCT; |
| 677 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 678 | printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n", |
| 679 | ctx->device_name); |
| 680 | if (root_filesystem && !read_only_root) { |
| 681 | printf("%s: ***** REBOOT LINUX *****\n", |
| 682 | ctx->device_name); |
| 683 | exit_value = FSCK_REBOOT; |
| 684 | } |
| 685 | } |
| 686 | if (ext2fs_test_valid(fs)) |
| 687 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
| 688 | else |
| 689 | exit_value = FSCK_UNCORRECTED; |
| 690 | if (!(ctx->options & E2F_OPT_READONLY)) { |
| 691 | if (ext2fs_test_valid(fs)) { |
| 692 | if (!(fs->super->s_state & EXT2_VALID_FS)) |
| 693 | exit_value = FSCK_NONDESTRUCT; |
| 694 | fs->super->s_state = EXT2_VALID_FS; |
| 695 | } else |
| 696 | fs->super->s_state &= ~EXT2_VALID_FS; |
| 697 | fs->super->s_mnt_count = 0; |
| 698 | fs->super->s_lastcheck = time(NULL); |
| 699 | ext2fs_mark_super_dirty(fs); |
| 700 | } |
| 701 | show_stats(ctx); |
| 702 | |
| Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 703 | e2fsck_write_bitmaps(ctx); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 704 | |
| Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 705 | #ifdef RESOURCE_TRACK |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 706 | if (ctx->options & E2F_OPT_TIME) |
| 707 | print_resource_track(NULL, &ctx->global_rtrack); |
| Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 708 | #endif |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 709 | |
| 710 | e2fsck_free_context(ctx); |
| Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 711 | ext2fs_close(fs); |
| Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 712 | |
| 713 | return exit_value; |
| 714 | } |