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