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