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