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