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