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