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