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