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> |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 19 | #include <time.h> |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 20 | #ifdef HAVE_SIGNAL_H |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 21 | #include <signal.h> |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 22 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 23 | #ifdef HAVE_GETOPT_H |
| 24 | #include <getopt.h> |
| 25 | #endif |
| 26 | #include <unistd.h> |
| 27 | #ifdef HAVE_ERRNO_H |
| 28 | #include <errno.h> |
| 29 | #endif |
| 30 | #ifdef HAVE_MNTENT_H |
| 31 | #include <mntent.h> |
| 32 | #endif |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <malloc.h> |
| 35 | |
| 36 | #include "et/com_err.h" |
| 37 | #include "e2fsck.h" |
| 38 | #include "problem.h" |
| 39 | #include "../version.h" |
| 40 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 61 | _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n" |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 62 | "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n" |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 63 | "\t\t[-l|-L bad_blocks_file] [-C fd] device\n"), |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 64 | ctx->program_name); |
Theodore Ts'o | b55199e | 1999-07-19 15:37:46 +0000 | [diff] [blame] | 65 | |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 66 | fprintf(stderr, _("\nEmergency help:\n" |
Theodore Ts'o | b55199e | 1999-07-19 15:37:46 +0000 | [diff] [blame] | 67 | " -p Automatic repair (no questions)\n" |
| 68 | " -n Make no changes to the filesystem\n" |
| 69 | " -y Assume \"yes\" to all questions\n" |
| 70 | " -c Check for bad blocks\n" |
| 71 | " -f Force checking even if filesystem is marked clean\n" |
| 72 | " -v Be verbose\n" |
| 73 | " -b superblock Use alternative superblock\n" |
| 74 | " -B blocksize Force blocksize when looking for superblock\n" |
| 75 | " -l bad_blocks_file Add to badblocks list\n" |
| 76 | " -L bad_blocks_file Set badblocks list\n" |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 77 | )); |
Theodore Ts'o | b55199e | 1999-07-19 15:37:46 +0000 | [diff] [blame] | 78 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 79 | exit(FSCK_USAGE); |
| 80 | } |
| 81 | |
| 82 | static void show_stats(e2fsck_t ctx) |
| 83 | { |
| 84 | ext2_filsys fs = ctx->fs; |
| 85 | int inodes, inodes_used, blocks, blocks_used; |
| 86 | int dir_links; |
| 87 | int num_files, num_links; |
| 88 | int frag_percent; |
| 89 | |
| 90 | dir_links = 2 * ctx->fs_directory_count - 1; |
| 91 | num_files = ctx->fs_total_count - dir_links; |
| 92 | num_links = ctx->fs_links_count - dir_links; |
| 93 | inodes = fs->super->s_inodes_count; |
| 94 | inodes_used = (fs->super->s_inodes_count - |
| 95 | fs->super->s_free_inodes_count); |
| 96 | blocks = fs->super->s_blocks_count; |
| 97 | blocks_used = (fs->super->s_blocks_count - |
| 98 | fs->super->s_free_blocks_count); |
| 99 | |
| 100 | frag_percent = (10000 * ctx->fs_fragmented) / inodes_used; |
| 101 | frag_percent = (frag_percent + 5) / 10; |
| 102 | |
| 103 | if (!verbose) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 104 | printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 105 | ctx->device_name, inodes_used, inodes, |
| 106 | frag_percent / 10, frag_percent % 10, |
| 107 | blocks_used, blocks); |
| 108 | return; |
| 109 | } |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 110 | /* |
| 111 | * This is a bit ugly. But I think there will nearly always be more |
| 112 | * than one "thing" to report about, so I won't try writing complex |
| 113 | * code to handle one/two/many forms of all words. |
| 114 | * Some languages (Italian, at least) never uses the plural form |
| 115 | * of foreign words, so in real life this could not be a problem. |
| 116 | * md@linux.it - 2000-1-15 |
| 117 | */ |
| 118 | #ifdef ENABLE_NLS |
| 119 | printf (_("\n%8d inodes used (%d%%)\n"), inodes_used, |
| 120 | (inodes_used != 1), 100 * inodes_used / inodes); |
| 121 | printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"), |
| 122 | ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); |
| 123 | printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"), |
| 124 | ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); |
| 125 | printf (_("%8d blocks used (%d%%)\n" |
| 126 | "%8d bad blocks\n"), blocks_used, |
| 127 | 100 * blocks_used / blocks, ctx->fs_badblocks_count); |
| 128 | printf (_("\n%8d regular files\n" |
| 129 | "%8d directories\n" |
| 130 | "%8d character device files\n" |
| 131 | "%8d block device files\n" |
| 132 | "%8d fifos\n" |
| 133 | "%8d links\n" |
| 134 | "%8d symbolic links (%d fast symbolic links)\n" |
| 135 | "%8d sockets\n" |
| 136 | "--------\n" |
| 137 | "%8d files\n"), |
| 138 | ctx->fs_regular_count, |
| 139 | ctx->fs_directory_count, |
| 140 | ctx->fs_chardev_count, |
| 141 | ctx->fs_blockdev_count, |
| 142 | ctx->fs_fifo_count, |
| 143 | ctx->fs_links_count - dir_links, |
| 144 | ctx->fs_symlinks_count, |
| 145 | ctx->fs_fast_symlinks_count, |
| 146 | ctx->fs_sockets_count, |
| 147 | ctx->fs_total_count - dir_links); |
| 148 | #else |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 149 | printf ("\n%8d inode%s used (%d%%)\n", inodes_used, |
| 150 | (inodes_used != 1) ? "s" : "", |
| 151 | 100 * inodes_used / inodes); |
| 152 | printf ("%8d non-contiguous inodes (%0d.%d%%)\n", |
| 153 | ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); |
| 154 | printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n", |
| 155 | ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); |
| 156 | printf ("%8d block%s used (%d%%)\n" |
| 157 | "%8d bad block%s\n", blocks_used, |
| 158 | (blocks_used != 1) ? "s" : "", |
| 159 | 100 * blocks_used / blocks, ctx->fs_badblocks_count, |
| 160 | ctx->fs_badblocks_count != 1 ? "s" : ""); |
| 161 | printf ("\n%8d regular file%s\n" |
| 162 | "%8d director%s\n" |
| 163 | "%8d character device file%s\n" |
| 164 | "%8d block device file%s\n" |
| 165 | "%8d fifo%s\n" |
| 166 | "%8d link%s\n" |
| 167 | "%8d symbolic link%s (%d fast symbolic link%s)\n" |
| 168 | "%8d socket%s\n" |
| 169 | "--------\n" |
| 170 | "%8d file%s\n", |
| 171 | ctx->fs_regular_count, |
| 172 | (ctx->fs_regular_count != 1) ? "s" : "", |
| 173 | ctx->fs_directory_count, |
| 174 | (ctx->fs_directory_count != 1) ? "ies" : "y", |
| 175 | ctx->fs_chardev_count, |
| 176 | (ctx->fs_chardev_count != 1) ? "s" : "", |
| 177 | ctx->fs_blockdev_count, |
| 178 | (ctx->fs_blockdev_count != 1) ? "s" : "", |
| 179 | ctx->fs_fifo_count, |
| 180 | (ctx->fs_fifo_count != 1) ? "s" : "", |
| 181 | ctx->fs_links_count - dir_links, |
| 182 | ((ctx->fs_links_count - dir_links) != 1) ? "s" : "", |
| 183 | ctx->fs_symlinks_count, |
| 184 | (ctx->fs_symlinks_count != 1) ? "s" : "", |
| 185 | ctx->fs_fast_symlinks_count, |
| 186 | (ctx->fs_fast_symlinks_count != 1) ? "s" : "", |
| 187 | ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "", |
| 188 | ctx->fs_total_count - dir_links, |
| 189 | ((ctx->fs_total_count - dir_links) != 1) ? "s" : ""); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 190 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void check_mount(e2fsck_t ctx) |
| 194 | { |
| 195 | errcode_t retval; |
| 196 | int mount_flags, cont, fd; |
| 197 | |
| 198 | retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); |
| 199 | if (retval) { |
| 200 | com_err("ext2fs_check_if_mount", retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 201 | _("while determining whether %s is mounted."), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 202 | ctx->filesystem_name); |
| 203 | return; |
| 204 | } |
| 205 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
| 206 | return; |
| 207 | |
| 208 | #if (defined(__linux__) && defined(HAVE_MNTENT_H)) |
| 209 | /* |
| 210 | * If the root is mounted read-only, then /etc/mtab is |
| 211 | * probably not correct; so we won't issue a warning based on |
| 212 | * it. |
| 213 | */ |
| 214 | fd = open(MOUNTED, O_RDWR); |
| 215 | if (fd < 0) { |
| 216 | if (errno == EROFS) |
| 217 | return; |
| 218 | } else |
| 219 | close(fd); |
| 220 | #endif |
| 221 | |
| 222 | if (ctx->options & E2F_OPT_READONLY) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 223 | printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 227 | printf(_("%s is mounted. "), ctx->filesystem_name); |
Theodore Ts'o | 7403335 | 1999-07-01 03:00:47 +0000 | [diff] [blame] | 228 | if (!isatty(0) || !isatty(1)) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 229 | printf(_("Cannot continue, aborting.\n\n")); |
Theodore Ts'o | 7403335 | 1999-07-01 03:00:47 +0000 | [diff] [blame] | 230 | exit(FSCK_ERROR); |
| 231 | } |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 232 | printf(_("\n\n\007\007\007\007WARNING!!! " |
Theodore Ts'o | 7403335 | 1999-07-01 03:00:47 +0000 | [diff] [blame] | 233 | "Running e2fsck on a mounted filesystem may cause\n" |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 234 | "SEVERE filesystem damage.\007\007\007\n\n")); |
| 235 | cont = ask_yn(_("Do you really want to continue"), -1); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 236 | if (!cont) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 237 | printf (_("check aborted.\n")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 238 | exit (0); |
| 239 | } |
| 240 | return; |
| 241 | } |
| 242 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 243 | /* |
| 244 | * This routine checks to see if a filesystem can be skipped; if so, |
| 245 | * it will exit with E2FSCK_OK. Under some conditions it will print a |
| 246 | * message explaining why a check is being forced. |
| 247 | */ |
| 248 | static void check_if_skip(e2fsck_t ctx) |
| 249 | { |
| 250 | ext2_filsys fs = ctx->fs; |
| 251 | const char *reason = NULL; |
| 252 | |
| 253 | if (force || bad_blocks_file || cflag || swapfs) |
| 254 | return; |
| 255 | |
| 256 | if (fs->super->s_state & EXT2_ERROR_FS) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 257 | reason = _("contains a file system with errors"); |
Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 258 | else if ((fs->super->s_state & EXT2_VALID_FS) == 0) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 259 | reason = _("was not cleanly unmounted"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 260 | else if (fs->super->s_mnt_count >= |
| 261 | (unsigned) fs->super->s_max_mnt_count) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 262 | reason = _("has reached maximal mount count"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 263 | else if (fs->super->s_checkinterval && |
| 264 | time(0) >= (fs->super->s_lastcheck + |
| 265 | fs->super->s_checkinterval)) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 266 | reason = _("has gone too long without being checked"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 267 | if (reason) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 268 | printf(_("%s %s, check forced.\n"), ctx->device_name, reason); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 271 | printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 272 | fs->super->s_inodes_count - fs->super->s_free_inodes_count, |
| 273 | fs->super->s_inodes_count, |
| 274 | fs->super->s_blocks_count - fs->super->s_free_blocks_count, |
| 275 | fs->super->s_blocks_count); |
| 276 | ext2fs_close(fs); |
| 277 | exit(FSCK_OK); |
| 278 | } |
| 279 | |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 280 | /* |
| 281 | * For completion notice |
| 282 | */ |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 283 | struct percent_tbl { |
| 284 | int max_pass; |
| 285 | int table[32]; |
| 286 | }; |
| 287 | struct percent_tbl e2fsck_tbl = { |
| 288 | 5, { 0, 70, 90, 92, 95, 100 } |
| 289 | }; |
| 290 | static int dpywidth = 50; |
| 291 | static char bar[] = |
| 292 | "===============================================================" |
| 293 | "==============================================================="; |
| 294 | static char spaces[] = |
| 295 | " " |
| 296 | " "; |
| 297 | |
| 298 | static float calc_percent(struct percent_tbl *tbl, int pass, int curr, |
| 299 | int max) |
| 300 | { |
| 301 | float percent; |
| 302 | |
| 303 | if (pass <= 0) |
| 304 | return 0.0; |
| 305 | if (pass > tbl->max_pass) |
| 306 | return 100.0; |
| 307 | percent = ((float) curr) / ((float) max); |
| 308 | return ((percent * (tbl->table[pass] - tbl->table[pass-1])) |
| 309 | + tbl->table[pass-1]); |
| 310 | } |
| 311 | |
| 312 | extern void e2fsck_clear_progbar(e2fsck_t ctx) |
| 313 | { |
| 314 | if (!(ctx->flags & E2F_FLAG_PROG_BAR)) |
| 315 | return; |
| 316 | |
| 317 | printf("%s\r", spaces + (sizeof(spaces) - 80)); |
| 318 | ctx->flags &= ~E2F_FLAG_PROG_BAR; |
| 319 | } |
| 320 | |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 321 | static int e2fsck_update_progress(e2fsck_t ctx, int pass, |
| 322 | unsigned long cur, unsigned long max) |
| 323 | { |
| 324 | const char spinner[] = "\\|/-"; |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 325 | char buf[80]; |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 326 | int i; |
| 327 | float percent; |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 328 | |
| 329 | if (pass == 0) |
| 330 | return 0; |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 331 | |
| 332 | if (ctx->progress_fd) { |
| 333 | sprintf(buf, "%d %lu %lu\n", pass, cur, max); |
| 334 | write(ctx->progress_fd, buf, strlen(buf)); |
| 335 | } else { |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 336 | if (ctx->flags & E2F_FLAG_PROG_SUPPRESS) |
| 337 | return 0; |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 338 | ctx->progress_pos = (ctx->progress_pos+1) & 3; |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 339 | ctx->flags |= E2F_FLAG_PROG_BAR; |
| 340 | percent = calc_percent(&e2fsck_tbl, pass, cur, max); |
| 341 | if (ctx->progress_last_percent == (int) 1000 * percent) |
| 342 | return 0; |
| 343 | ctx->progress_last_percent = (int) 1000 * percent; |
| 344 | i = ((percent * dpywidth) + 50) / 100; |
| 345 | printf("%s: |%s%s", ctx->device_name, |
| 346 | bar + (sizeof(bar) - (i+1)), |
| 347 | spaces + (sizeof(spaces) - (dpywidth - i + 1))); |
| 348 | if (percent == 100.0) |
| 349 | fputc('|', stdout); |
| 350 | else |
| 351 | fputc(spinner[ctx->progress_pos & 3], stdout); |
| 352 | printf(" %4.1f%% \r", percent); |
| 353 | if (percent == 100.0) |
| 354 | e2fsck_clear_progbar(ctx); |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 355 | fflush(stdout); |
| 356 | } |
| 357 | return 0; |
| 358 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 359 | |
| 360 | #define PATH_SET "PATH=/sbin" |
| 361 | |
Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 362 | static void reserve_stdio_fds(NOARGS) |
| 363 | { |
| 364 | int fd; |
| 365 | |
| 366 | while (1) { |
| 367 | fd = open("/dev/null", O_RDWR); |
| 368 | if (fd > 2) |
| 369 | break; |
Theodore Ts'o | 75d83be | 1999-05-18 03:16:36 +0000 | [diff] [blame] | 370 | if (fd < 0) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 371 | fprintf(stderr, _("ERROR: Couldn't open " |
| 372 | "/dev/null (%s)\n"), |
Theodore Ts'o | 75d83be | 1999-05-18 03:16:36 +0000 | [diff] [blame] | 373 | strerror(errno)); |
| 374 | break; |
| 375 | } |
Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 376 | } |
| 377 | close(fd); |
| 378 | } |
| 379 | |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 380 | #ifdef HAVE_SIGNAL_H |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 381 | static e2fsck_t global_signal_ctx; |
| 382 | |
| 383 | static void signal_progress_on(int sig) |
| 384 | { |
| 385 | e2fsck_t ctx = global_signal_ctx; |
| 386 | |
| 387 | if (!ctx) |
| 388 | return; |
| 389 | |
| 390 | ctx->progress = e2fsck_update_progress; |
| 391 | ctx->progress_fd = 0; |
| 392 | } |
| 393 | |
| 394 | static void signal_progress_off(int sig) |
| 395 | { |
| 396 | e2fsck_t ctx = global_signal_ctx; |
| 397 | |
| 398 | if (!ctx) |
| 399 | return; |
| 400 | |
| 401 | e2fsck_clear_progbar(ctx); |
| 402 | ctx->progress = 0; |
| 403 | } |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 404 | #endif |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 405 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 406 | static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx) |
| 407 | { |
| 408 | int flush = 0; |
Theodore Ts'o | 519149f | 1997-10-25 03:49:49 +0000 | [diff] [blame] | 409 | int c; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 410 | #ifdef MTRACE |
| 411 | extern void *mallwatch; |
| 412 | #endif |
| 413 | char *oldpath = getenv("PATH"); |
| 414 | e2fsck_t ctx; |
| 415 | errcode_t retval; |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 416 | #ifdef HAVE_SIGNAL_H |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 417 | struct sigaction sa; |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 418 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 419 | |
| 420 | retval = e2fsck_allocate_context(&ctx); |
| 421 | if (retval) |
| 422 | return retval; |
| 423 | |
| 424 | *ret_ctx = ctx; |
| 425 | |
| 426 | /* Update our PATH to include /sbin */ |
| 427 | if (oldpath) { |
| 428 | char *newpath; |
| 429 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 430 | newpath = (char *) malloc(sizeof (PATH_SET) + 1 + |
| 431 | strlen (oldpath)); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 432 | if (!newpath) |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 433 | fatal_error(ctx, "Couldn't malloc() newpath"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 434 | strcpy (newpath, PATH_SET); |
| 435 | strcat (newpath, ":"); |
| 436 | strcat (newpath, oldpath); |
| 437 | putenv (newpath); |
| 438 | } else |
| 439 | putenv (PATH_SET); |
| 440 | |
| 441 | setbuf(stdout, NULL); |
| 442 | setbuf(stderr, NULL); |
| 443 | initialize_ext2_error_table(); |
| 444 | |
| 445 | if (argc && *argv) |
| 446 | ctx->program_name = *argv; |
| 447 | else |
| 448 | ctx->program_name = "e2fsck"; |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 449 | 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] | 450 | switch (c) { |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 451 | case 'C': |
| 452 | ctx->progress = e2fsck_update_progress; |
| 453 | ctx->progress_fd = atoi(optarg); |
| 454 | break; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 455 | case 'p': |
| 456 | case 'a': |
| 457 | ctx->options |= E2F_OPT_PREEN; |
| 458 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO); |
| 459 | break; |
| 460 | case 'n': |
| 461 | ctx->options |= E2F_OPT_NO; |
| 462 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN); |
| 463 | break; |
| 464 | case 'y': |
| 465 | ctx->options |= E2F_OPT_YES; |
| 466 | ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO); |
| 467 | break; |
| 468 | case 't': |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 469 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 470 | if (ctx->options & E2F_OPT_TIME) |
| 471 | ctx->options |= E2F_OPT_TIME2; |
| 472 | else |
| 473 | ctx->options |= E2F_OPT_TIME; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 474 | #else |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 475 | fprintf(stderr, _("The -t option is not " |
| 476 | "supported on this version of e2fsck.\n")); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 477 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 478 | break; |
| 479 | case 'c': |
| 480 | cflag++; |
| 481 | ctx->options |= E2F_OPT_CHECKBLOCKS; |
| 482 | break; |
| 483 | case 'r': |
| 484 | /* What we do by default, anyway! */ |
| 485 | break; |
| 486 | case 'b': |
| 487 | ctx->use_superblock = atoi(optarg); |
| 488 | break; |
| 489 | case 'B': |
| 490 | blocksize = atoi(optarg); |
| 491 | break; |
| 492 | case 'I': |
| 493 | ctx->inode_buffer_blocks = atoi(optarg); |
| 494 | break; |
| 495 | case 'P': |
| 496 | ctx->process_inode_size = atoi(optarg); |
| 497 | break; |
| 498 | case 'L': |
| 499 | replace_bad_blocks++; |
| 500 | case 'l': |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 501 | bad_blocks_file = (char *) malloc(strlen(optarg)+1); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 502 | if (!bad_blocks_file) |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 503 | fatal_error(ctx, |
| 504 | "Couldn't malloc bad_blocks_file"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 505 | strcpy(bad_blocks_file, optarg); |
| 506 | break; |
| 507 | case 'd': |
| 508 | ctx->options |= E2F_OPT_DEBUG; |
| 509 | break; |
| 510 | case 'f': |
| 511 | force = 1; |
| 512 | break; |
| 513 | case 'F': |
| 514 | #ifdef BLKFLSBUF |
| 515 | flush = 1; |
| 516 | #else |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 517 | fatal_error(ctx, _("-F not supported")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 518 | #endif |
| 519 | break; |
| 520 | case 'v': |
| 521 | verbose = 1; |
| 522 | break; |
| 523 | case 'V': |
| 524 | show_version_only = 1; |
| 525 | break; |
| 526 | #ifdef MTRACE |
| 527 | case 'M': |
| 528 | mallwatch = (void *) strtol(optarg, NULL, 0); |
| 529 | break; |
| 530 | #endif |
| 531 | case 'N': |
| 532 | ctx->device_name = optarg; |
| 533 | break; |
| 534 | case 's': |
| 535 | normalize_swapfs = 1; |
| 536 | case 'S': |
| 537 | swapfs = 1; |
| 538 | break; |
| 539 | default: |
| 540 | usage(ctx); |
| 541 | } |
| 542 | if (show_version_only) |
| 543 | return 0; |
| 544 | if (optind != argc - 1) |
| 545 | usage(ctx); |
| 546 | if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file && |
| 547 | !cflag && !swapfs) |
| 548 | ctx->options |= E2F_OPT_READONLY; |
| 549 | ctx->filesystem_name = argv[optind]; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 550 | #ifdef BLKFLSBUF |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 551 | if (flush) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 552 | int fd = open(ctx->filesystem_name, O_RDONLY, 0); |
| 553 | |
| 554 | if (fd < 0) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 555 | com_err("open", errno, |
| 556 | _("while opening %s for flushing"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 557 | ctx->filesystem_name); |
| 558 | exit(FSCK_ERROR); |
| 559 | } |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 560 | if (fsync(fd) < 0) { |
| 561 | com_err("fsync", errno, |
| 562 | _("while trying to flush %s"), |
| 563 | ctx->filesystem_name); |
| 564 | exit(FSCK_ERROR); |
| 565 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 566 | if (ioctl(fd, BLKFLSBUF, 0) < 0) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 567 | com_err("BLKFLSBUF", errno, |
| 568 | _("while trying to flush %s"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 569 | ctx->filesystem_name); |
| 570 | exit(FSCK_ERROR); |
| 571 | } |
| 572 | close(fd); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 573 | } |
Theodore Ts'o | 28ffafb | 2000-02-08 19:14:02 +0000 | [diff] [blame] | 574 | #endif /* BLKFLSBUF */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 575 | if (swapfs) { |
| 576 | if (cflag || bad_blocks_file) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 577 | fprintf(stderr, _("Incompatible options not " |
| 578 | "allowed when byte-swapping.\n")); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 579 | exit(FSCK_ERROR); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 582 | #ifdef HAVE_SIGNAL_H |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 583 | /* |
| 584 | * Set up signal action |
| 585 | */ |
| 586 | memset(&sa, 0, sizeof(struct sigaction)); |
| 587 | #ifdef SA_RESTART |
| 588 | sa.sa_flags = SA_RESTART; |
| 589 | #endif |
| 590 | global_signal_ctx = ctx; |
| 591 | sa.sa_handler = signal_progress_on; |
| 592 | sigaction(SIGUSR1, &sa, 0); |
| 593 | sa.sa_handler = signal_progress_off; |
| 594 | sigaction(SIGUSR2, &sa, 0); |
Theodore Ts'o | 9ecd8be | 1999-10-20 18:24:31 +0000 | [diff] [blame] | 595 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static const char *my_ver_string = E2FSPROGS_VERSION; |
| 600 | static const char *my_ver_date = E2FSPROGS_DATE; |
| 601 | |
| 602 | int main (int argc, char *argv[]) |
| 603 | { |
| 604 | errcode_t retval = 0; |
| 605 | int exit_value = FSCK_OK; |
| 606 | int i; |
| 607 | ext2_filsys fs = 0; |
| 608 | io_manager io_ptr; |
| 609 | struct ext2fs_sb *s; |
| 610 | const char *lib_ver_date; |
| 611 | int my_ver, lib_ver; |
| 612 | e2fsck_t ctx; |
| 613 | struct problem_context pctx; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 614 | int flags, run_result; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 615 | |
| 616 | clear_problem_context(&pctx); |
| 617 | #ifdef MTRACE |
| 618 | mtrace(); |
| 619 | #endif |
| 620 | #ifdef MCHECK |
| 621 | mcheck(0); |
| 622 | #endif |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 623 | #ifdef ENABLE_NLS |
| 624 | setlocale(LC_MESSAGES, ""); |
| 625 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 626 | textdomain(NLS_CAT_NAME); |
| 627 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 628 | my_ver = ext2fs_parse_version_string(my_ver_string); |
| 629 | lib_ver = ext2fs_get_library_version(0, &lib_ver_date); |
| 630 | if (my_ver > lib_ver) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 631 | fprintf( stderr, _("Error: ext2fs library version " |
| 632 | "out of date!\n")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 633 | show_version_only++; |
| 634 | } |
| 635 | |
| 636 | retval = PRS(argc, argv, &ctx); |
| 637 | if (retval) { |
| 638 | com_err("e2fsck", retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 639 | _("while trying to initialize program")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 640 | exit(1); |
| 641 | } |
Theodore Ts'o | 24fc503 | 1998-08-26 15:23:31 +0000 | [diff] [blame] | 642 | reserve_stdio_fds(); |
| 643 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 644 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 645 | init_resource_track(&ctx->global_rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 646 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 647 | |
| 648 | if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 649 | fprintf (stderr, _("e2fsck %s, %s for EXT2 FS %s, %s\n"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 650 | my_ver_string, my_ver_date, EXT2FS_VERSION, |
| 651 | EXT2FS_DATE); |
| 652 | |
| 653 | if (show_version_only) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 654 | fprintf(stderr, _("\tUsing %s, %s\n"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 655 | error_message(EXT2_ET_BASE), lib_ver_date); |
| 656 | exit(0); |
| 657 | } |
| 658 | |
| 659 | check_mount(ctx); |
| 660 | |
| 661 | if (!(ctx->options & E2F_OPT_PREEN) && |
| 662 | !(ctx->options & E2F_OPT_NO) && |
| 663 | !(ctx->options & E2F_OPT_YES)) { |
| 664 | if (!isatty (0) || !isatty (1)) |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 665 | fatal_error(ctx, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 666 | _("need terminal for interactive repairs")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 667 | } |
| 668 | ctx->superblock = ctx->use_superblock; |
| 669 | restart: |
| 670 | #if 1 |
| 671 | io_ptr = unix_io_manager; |
| 672 | #else |
| 673 | io_ptr = test_io_manager; |
| 674 | test_io_backing_manager = unix_io_manager; |
| 675 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 676 | flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW; |
| 677 | if (ctx->superblock && blocksize) { |
| 678 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 679 | ctx->superblock, blocksize, io_ptr, &fs); |
| 680 | } else if (ctx->superblock) { |
| 681 | for (i=0; possible_block_sizes[i]; i++) { |
| 682 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 683 | ctx->superblock, |
| 684 | possible_block_sizes[i], |
| 685 | io_ptr, &fs); |
| 686 | if (!retval) |
| 687 | break; |
| 688 | } |
| 689 | } else |
| 690 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 691 | 0, 0, io_ptr, &fs); |
| 692 | if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && |
| 693 | ((retval == EXT2_ET_BAD_MAGIC) || |
| 694 | ((retval == 0) && ext2fs_check_desc(fs)))) { |
| 695 | if (!fs || (fs->group_desc_count > 1)) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 696 | printf(_("%s trying backup blocks...\n"), |
| 697 | retval ? _("Couldn't find ext2 superblock,") : |
| 698 | _("Group descriptors look bad...")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 699 | ctx->superblock = get_backup_sb(fs); |
| 700 | if (fs) |
| 701 | ext2fs_close(fs); |
| 702 | goto restart; |
| 703 | } |
| 704 | } |
| 705 | if (retval) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 706 | com_err(ctx->program_name, retval, _("while trying to open %s"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 707 | ctx->filesystem_name); |
Theodore Ts'o | 24dd402 | 1998-02-01 00:16:40 +0000 | [diff] [blame] | 708 | if (retval == EXT2_ET_REV_TOO_HIGH) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 709 | printf(_("The filesystem revision is apparently " |
Theodore Ts'o | 24dd402 | 1998-02-01 00:16:40 +0000 | [diff] [blame] | 710 | "too high for this version of e2fsck.\n" |
| 711 | "(Or the filesystem superblock " |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 712 | "is corrupt)\n\n")); |
Theodore Ts'o | 24dd402 | 1998-02-01 00:16:40 +0000 | [diff] [blame] | 713 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); |
| 714 | } else if (retval == EXT2_ET_SHORT_READ) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 715 | printf(_("Could this be a zero-length partition?\n")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 716 | else if ((retval == EPERM) || (retval == EACCES)) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 717 | printf(_("You must have %s access to the " |
| 718 | "filesystem or be root\n"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 719 | (ctx->options & E2F_OPT_READONLY) ? |
| 720 | "r/o" : "r/w"); |
| 721 | else if (retval == ENXIO) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 722 | printf(_("Possibly non-existent or swap device?\n")); |
Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame] | 723 | #ifdef EROFS |
| 724 | else if (retval == EROFS) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 725 | printf(_("Disk write-protected; use the -n option " |
Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame] | 726 | "to do a read-only\n" |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 727 | "check of the device.\n")); |
Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame] | 728 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 729 | else |
| 730 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 731 | exit(FSCK_ERROR); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 732 | } |
| 733 | ctx->fs = fs; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 734 | fs->priv_data = ctx; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 735 | #ifdef EXT2_CURRENT_REV |
| 736 | if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) { |
| 737 | com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 738 | _("while trying to open %s"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 739 | ctx->filesystem_name); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 740 | get_newer: |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 741 | fatal_error(ctx, _("Get a newer version of e2fsck!")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 742 | } |
| 743 | #endif |
| 744 | /* |
| 745 | * Check for compatibility with the feature sets. We need to |
| 746 | * be more stringent than ext2fs_open(). |
| 747 | */ |
| 748 | s = (struct ext2fs_sb *) fs->super; |
| 749 | if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || |
Theodore Ts'o | 8144d67 | 1998-07-09 05:33:18 +0000 | [diff] [blame] | 750 | (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 751 | com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE, |
| 752 | "(%s)", ctx->filesystem_name); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 753 | goto get_newer; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 754 | } |
| 755 | if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { |
| 756 | com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE, |
| 757 | "(%s)", ctx->filesystem_name); |
| 758 | goto get_newer; |
| 759 | } |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 760 | if (ctx->device_name == 0 && |
| 761 | (s->s_volume_name[0] != 0)) { |
| 762 | char *cp = malloc(sizeof(s->s_volume_name)+1); |
| 763 | if (cp) { |
| 764 | strncpy(cp, s->s_volume_name, |
| 765 | sizeof(s->s_volume_name)); |
| 766 | cp[sizeof(s->s_volume_name)] = 0; |
| 767 | ctx->device_name = cp; |
| 768 | } |
| 769 | } |
| 770 | if (ctx->device_name == 0) |
| 771 | ctx->device_name = ctx->filesystem_name; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 772 | |
| 773 | /* |
| 774 | * If the user specified a specific superblock, presumably the |
| 775 | * master superblock has been trashed. So we mark the |
| 776 | * superblock as dirty, so it can be written out. |
| 777 | */ |
| 778 | if (ctx->superblock && |
| 779 | !(ctx->options & E2F_OPT_READONLY)) |
| 780 | ext2fs_mark_super_dirty(fs); |
| 781 | |
| 782 | /* |
| 783 | * Don't overwrite the backup superblock and block |
| 784 | * descriptors, until we're sure the filesystem is OK.... |
| 785 | */ |
| 786 | fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
| 787 | |
| 788 | ehandler_init(fs->io); |
| 789 | |
| 790 | if (ctx->superblock) |
| 791 | set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); |
| 792 | check_super_block(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 793 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 794 | exit(FSCK_ERROR); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 795 | check_if_skip(ctx); |
| 796 | if (bad_blocks_file) |
| 797 | read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); |
| 798 | else if (cflag) |
| 799 | test_disk(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 800 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 801 | exit(FSCK_ERROR); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 802 | |
| 803 | if (normalize_swapfs) { |
| 804 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == |
| 805 | ext2fs_native_flag()) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 806 | fprintf(stderr, _("%s: Filesystem byte order " |
| 807 | "already normalized.\n"), ctx->device_name); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 808 | exit(FSCK_ERROR); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 811 | if (swapfs) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 812 | swap_filesys(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 813 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 814 | exit(FSCK_ERROR); |
| 815 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 816 | |
| 817 | /* |
| 818 | * Mark the system as valid, 'til proven otherwise |
| 819 | */ |
| 820 | ext2fs_mark_valid(fs); |
| 821 | |
| 822 | retval = ext2fs_read_bb_inode(fs, &fs->badblocks); |
| 823 | if (retval) { |
| 824 | com_err(ctx->program_name, retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 825 | _("while reading bad blocks inode")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 826 | preenhalt(ctx); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 827 | printf(_("This doesn't bode well," |
| 828 | " but we'll try to go on...\n")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 829 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 830 | |
| 831 | run_result = e2fsck_run(ctx); |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 832 | e2fsck_clear_progbar(ctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 833 | if (run_result == E2F_FLAG_RESTART) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 834 | printf(_("Restarting e2fsck from the beginning...\n")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 835 | retval = e2fsck_reset_context(ctx); |
| 836 | if (retval) { |
| 837 | com_err(ctx->program_name, retval, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 838 | _("while resetting context")); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 839 | exit(1); |
| 840 | } |
Theodore Ts'o | 73f17cf | 1999-01-04 07:35:45 +0000 | [diff] [blame] | 841 | ext2fs_close(fs); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 842 | goto restart; |
| 843 | } |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 844 | if (run_result & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 845 | exit(FSCK_ERROR); |
| 846 | if (run_result & E2F_FLAG_CANCEL) |
| 847 | ext2fs_unmark_valid(fs); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 848 | |
| 849 | #ifdef MTRACE |
| 850 | mtrace_print("Cleanup"); |
| 851 | #endif |
| 852 | if (ext2fs_test_changed(fs)) { |
| 853 | exit_value = FSCK_NONDESTRUCT; |
| 854 | if (!(ctx->options & E2F_OPT_PREEN)) |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 855 | printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 856 | ctx->device_name); |
| 857 | if (root_filesystem && !read_only_root) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 858 | printf(_("%s: ***** REBOOT LINUX *****\n"), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 859 | ctx->device_name); |
| 860 | exit_value = FSCK_REBOOT; |
| 861 | } |
| 862 | } |
| 863 | if (ext2fs_test_valid(fs)) |
| 864 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
| 865 | else |
| 866 | exit_value = FSCK_UNCORRECTED; |
| 867 | if (!(ctx->options & E2F_OPT_READONLY)) { |
| 868 | if (ext2fs_test_valid(fs)) { |
| 869 | if (!(fs->super->s_state & EXT2_VALID_FS)) |
| 870 | exit_value = FSCK_NONDESTRUCT; |
| 871 | fs->super->s_state = EXT2_VALID_FS; |
| 872 | } else |
| 873 | fs->super->s_state &= ~EXT2_VALID_FS; |
| 874 | fs->super->s_mnt_count = 0; |
| 875 | fs->super->s_lastcheck = time(NULL); |
| 876 | ext2fs_mark_super_dirty(fs); |
| 877 | } |
| 878 | show_stats(ctx); |
| 879 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 880 | e2fsck_write_bitmaps(ctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 881 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 882 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 883 | if (ctx->options & E2F_OPT_TIME) |
| 884 | print_resource_track(NULL, &ctx->global_rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 885 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 886 | |
Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 887 | ext2fs_close(fs); |
Theodore Ts'o | 7142db0 | 1999-11-08 18:46:54 +0000 | [diff] [blame] | 888 | ctx->fs = NULL; |
| 889 | e2fsck_free_context(ctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 890 | |
| 891 | return exit_value; |
| 892 | } |