blob: 7a617cf6dc1ca7f89c3e4de9f31b299b5f55eb62 [file] [log] [blame]
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001/*
2 * unix.c - The unix-specific code for e2fsck
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#ifdef HAVE_STDLIB_H
14#include <stdlib.h>
15#endif
16#include <string.h>
17#include <fcntl.h>
18#include <ctype.h>
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000019#include <time.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000020#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +000021#include <signal.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000022#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000023#ifdef HAVE_GETOPT_H
24#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000025#else
26extern char *optarg;
27extern int optind;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000028#endif
29#include <unistd.h>
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33#ifdef HAVE_MNTENT_H
34#include <mntent.h>
35#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040036#ifdef HAVE_SYS_IOCTL_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000037#include <sys/ioctl.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040038#endif
Theodore Ts'oe71d8732003-03-14 02:13:48 -050039#ifdef HAVE_MALLOC_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000040#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050041#endif
Theodore Ts'oc07f9f22004-04-12 00:16:44 -040042#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_DIRENT_H
46#include <dirent.h>
47#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000048
49#include "et/com_err.h"
50#include "e2fsck.h"
51#include "problem.h"
52#include "../version.h"
53
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054/* Command line options */
Theodore Ts'o54a31a32003-08-19 10:08:34 -040055static int swapfs;
56static int normalize_swapfs;
57static int cflag; /* check disk */
58static int show_version_only;
59static int verbose;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060
Theodore Ts'o54a31a32003-08-19 10:08:34 -040061static int replace_bad_blocks;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -050062static int keep_bad_blocks;
Theodore Ts'o54a31a32003-08-19 10:08:34 -040063static char *bad_blocks_file;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000064
Theodore Ts'o243dc312000-08-22 21:37:47 +000065e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
66
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -050067#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
68int journal_enable_debug = -1;
69#endif
70
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071static void usage(e2fsck_t ctx)
72{
73 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050074 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000075 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -040076 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040077 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000078 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000079
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000080 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000081 " -p Automatic repair (no questions)\n"
82 " -n Make no changes to the filesystem\n"
83 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050084 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000085 " -f Force checking even if filesystem is marked clean\n"));
86 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000087 " -v Be verbose\n"
88 " -b superblock Use alternative superblock\n"
89 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -040090 " -j external_journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000091 " -l bad_blocks_file Add to badblocks list\n"
92 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000093 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000094
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000095 exit(FSCK_USAGE);
96}
97
98static void show_stats(e2fsck_t ctx)
99{
100 ext2_filsys fs = ctx->fs;
Eric Sandeenf3358642006-09-12 14:56:17 -0400101 ext2_ino_t inodes, inodes_used;
Takashi Sato8deb80a2006-03-18 21:43:46 -0500102 blk_t blocks, blocks_used;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000103 int dir_links;
104 int num_files, num_links;
105 int frag_percent;
106
107 dir_links = 2 * ctx->fs_directory_count - 1;
108 num_files = ctx->fs_total_count - dir_links;
109 num_links = ctx->fs_links_count - dir_links;
110 inodes = fs->super->s_inodes_count;
111 inodes_used = (fs->super->s_inodes_count -
112 fs->super->s_free_inodes_count);
113 blocks = fs->super->s_blocks_count;
114 blocks_used = (fs->super->s_blocks_count -
115 fs->super->s_free_blocks_count);
116
117 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
118 frag_percent = (frag_percent + 5) / 10;
119
120 if (!verbose) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400121 printf(_("%s: %u/%u files (%0d.%d%% non-contiguous), %u/%u blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000122 ctx->device_name, inodes_used, inodes,
123 frag_percent / 10, frag_percent % 10,
124 blocks_used, blocks);
125 return;
126 }
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400127 printf (P_("\n%8u inode used (%2.2f%%)\n", "\n%8u inodes used (%2.2f%%)\n",
128 inodes_used), inodes_used, 100.0 * inodes_used / inodes);
129 printf (P_("%8u non-contiguous inode (%0d.%d%%)\n",
130 "%8u non-contiguous inodes (%0d.%d%%)\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400131 ctx->fs_fragmented),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000132 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400133 printf (_(" # of inodes with ind/dind/tind blocks: %u/%u/%u\n"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000134 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400135 printf (P_("%8u block used (%2.2f%%)\n", "%8u blocks used (%2.2f%%)\n",
136 blocks_used), blocks_used, 100.0 * blocks_used / blocks);
137 printf (P_("%8u bad block\n", "%8u bad blocks\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400139 printf (P_("%8u large file\n", "%8u large files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400140 ctx->large_files), ctx->large_files);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400141 printf (P_("\n%8u regular file\n", "\n%8u regular files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400142 ctx->fs_regular_count), ctx->fs_regular_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400143 printf (P_("%8u directory\n", "%8u directories\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400144 ctx->fs_directory_count), ctx->fs_directory_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400145 printf (P_("%8u character device file\n",
146 "%8u character device files\n", ctx->fs_chardev_count),
Theodore Ts'o113e4052003-05-17 21:00:46 -0400147 ctx->fs_chardev_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400148 printf (P_("%8u block device file\n", "%8u block device files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400150 printf (P_("%8u fifo\n", "%8u fifos\n", ctx->fs_fifo_count),
Theodore Ts'o113e4052003-05-17 21:00:46 -0400151 ctx->fs_fifo_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400152 printf (P_("%8u link\n", "%8u links\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400155 printf (P_("%8u symbolic link", "%8u symbolic links",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400157 printf (P_(" (%u fast symbolic link)\n", " (%u fast symbolic links)\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400159 printf (P_("%8u socket\n", "%8u sockets\n", ctx->fs_sockets_count),
Theodore Ts'o113e4052003-05-17 21:00:46 -0400160 ctx->fs_sockets_count);
161 printf ("--------\n");
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400162 printf (P_("%8u file\n", "%8u files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400163 ctx->fs_total_count - dir_links),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000164 ctx->fs_total_count - dir_links);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000165}
166
167static void check_mount(e2fsck_t ctx)
168{
169 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500170 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000171
Theodore Ts'oee895132002-11-07 16:16:55 -0500172 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
173 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000174 if (retval) {
175 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000176 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000177 ctx->filesystem_name);
178 return;
179 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400180
181 /*
Theodore Ts'oae1182c2005-12-09 18:11:16 -0500182 * If the filesystem isn't mounted, or it's the root
183 * filesystem and it's mounted read-only, and we're not doing
184 * a read/write check, then everything's fine.
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400185 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500186 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
187 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
Theodore Ts'oae1182c2005-12-09 18:11:16 -0500188 (ctx->mount_flags & EXT2_MF_READONLY) &&
189 !(ctx->options & E2F_OPT_WRITECHECK)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000190 return;
191
Theodore Ts'oae1182c2005-12-09 18:11:16 -0500192 if ((ctx->options & E2F_OPT_READONLY) &&
193 !(ctx->options & E2F_OPT_WRITECHECK)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000194 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000195 return;
196 }
197
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000198 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400199 if (!ctx->interactive)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000200 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000201 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000202 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000203 "SEVERE filesystem damage.\007\007\007\n\n"));
204 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000205 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000206 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000207 exit (0);
208 }
209 return;
210}
211
Theodore Ts'o54434922003-12-07 01:28:50 -0500212static int is_on_batt(void)
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500213{
214 FILE *f;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400215 DIR *d;
216 char tmp[80], tmp2[80], fname[80];
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500217 unsigned int acflag;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400218 struct dirent* de;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500219
220 f = fopen("/proc/apm", "r");
221 if (f) {
222 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
223 acflag = 1;
224 fclose(f);
225 return (acflag != 1);
226 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400227 d = opendir("/proc/acpi/ac_adapter");
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500228 if (d) {
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500229 while ((de=readdir(d)) != NULL) {
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500230 if (!strncmp(".", de->d_name, 1))
231 continue;
232 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
233 de->d_name);
234 f = fopen(fname, "r");
235 if (!f)
236 continue;
237 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
238 tmp[0] = 0;
239 fclose(f);
240 if (strncmp(tmp, "off-line", 8) == 0) {
241 closedir(d);
242 return 1;
243 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400244 }
Matthias Andree4b137042005-01-13 03:35:29 +0100245 closedir(d);
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500246 }
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500247 return 0;
248}
249
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000250/*
251 * This routine checks to see if a filesystem can be skipped; if so,
252 * it will exit with E2FSCK_OK. Under some conditions it will print a
253 * message explaining why a check is being forced.
254 */
255static void check_if_skip(e2fsck_t ctx)
256{
257 ext2_filsys fs = ctx->fs;
258 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000259 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500260 long next_check;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500261 int batt = is_on_batt();
Theodore Ts'oa5f37a92006-01-29 05:15:36 -0500262 int defer_check_on_battery;
263
264 profile_get_boolean(ctx->profile, "options",
265 "defer_check_on_battery", 0, 1,
266 &defer_check_on_battery);
267 if (!defer_check_on_battery)
268 batt = 0;
269
Theodore Ts'od37066a2001-12-21 23:28:54 -0500270 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
271 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000272 return;
273
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500274 if ((fs->super->s_state & EXT2_ERROR_FS) ||
275 !ext2fs_test_valid(fs))
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000276 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000277 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000278 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000279 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000280 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000281 (unsigned) fs->super->s_max_mnt_count)) {
282 reason = _(" has been mounted %u times without being checked");
283 reason_arg = fs->super->s_mnt_count;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500284 if (batt && (fs->super->s_mnt_count <
285 (unsigned) fs->super->s_max_mnt_count*2))
286 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000287 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400288 ((ctx->now - fs->super->s_lastcheck) >=
Theodore Ts'o54434922003-12-07 01:28:50 -0500289 fs->super->s_checkinterval)) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000290 reason = _(" has gone %u days without being checked");
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400291 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
292 if (batt && ((ctx->now - fs->super->s_lastcheck) <
Theodore Ts'o54434922003-12-07 01:28:50 -0500293 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500294 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000295 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000296 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000297 fputs(ctx->device_name, stdout);
298 printf(reason, reason_arg);
299 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000300 return;
301 }
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400302 printf(_("%s: clean, %u/%u files, %u/%u blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000303 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
304 fs->super->s_inodes_count,
305 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
306 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500307 next_check = 100000;
308 if (fs->super->s_max_mnt_count > 0) {
309 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
310 if (next_check <= 0)
311 next_check = 1;
312 }
Theodore Ts'o66fbee82004-05-04 20:38:17 -0400313 if (fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400314 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500315 next_check = 1;
316 if (next_check <= 5) {
Theodore Ts'obc3392c2006-01-29 05:05:31 -0500317 if (next_check == 1) {
318 if (batt)
319 fputs(_(" (check deferred; on battery)"),
320 stdout);
321 else
322 fputs(_(" (check after next mount)"), stdout);
323 } else
Theodore Ts'o54434922003-12-07 01:28:50 -0500324 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500325 }
326 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000327 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400328 ctx->fs = NULL;
329 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000330 exit(FSCK_OK);
331}
332
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000333/*
334 * For completion notice
335 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000336struct percent_tbl {
337 int max_pass;
338 int table[32];
339};
340struct percent_tbl e2fsck_tbl = {
341 5, { 0, 70, 90, 92, 95, 100 }
342};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400343static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000344
345static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
346 int max)
347{
348 float percent;
349
350 if (pass <= 0)
351 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000352 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000353 return 100.0;
354 percent = ((float) curr) / ((float) max);
355 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
356 + tbl->table[pass-1]);
357}
358
359extern void e2fsck_clear_progbar(e2fsck_t ctx)
360{
361 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
362 return;
363
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400364 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
365 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400366 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000367 ctx->flags &= ~E2F_FLAG_PROG_BAR;
368}
369
Theodore Ts'o520ead32003-04-19 13:48:27 -0400370int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500371 unsigned int dpynum)
372{
373 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500374 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500375 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500376 struct timeval tv;
377 int dpywidth;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500378 int fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500379
380 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
381 return 0;
382
383 /*
384 * Calculate the new progress position. If the
385 * percentage hasn't changed, then we skip out right
386 * away.
387 */
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500388 fixed_percent = (int) ((10 * percent) + 0.5);
389 if (ctx->progress_last_percent == fixed_percent)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500390 return 0;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500391 ctx->progress_last_percent = fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500392
393 /*
394 * If we've already updated the spinner once within
395 * the last 1/8th of a second, no point doing it
396 * again.
397 */
398 gettimeofday(&tv, NULL);
399 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
400 if ((tick == ctx->progress_last_time) &&
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500401 (fixed_percent != 0) && (fixed_percent != 1000))
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500402 return 0;
403 ctx->progress_last_time = tick;
404
405 /*
406 * Advance the spinner, and note that the progress bar
407 * will be on the screen
408 */
409 ctx->progress_pos = (ctx->progress_pos+1) & 3;
410 ctx->flags |= E2F_FLAG_PROG_BAR;
411
412 dpywidth = 66 - strlen(label);
413 dpywidth = 8 * (dpywidth / 8);
414 if (dpynum)
415 dpywidth -= 8;
416
417 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400418 printf("%s%s: |%s%s", ctx->start_meta, label,
419 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500420 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500421 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500422 fputc('|', stdout);
423 else
424 fputc(spinner[ctx->progress_pos & 3], stdout);
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500425 printf(" %4.1f%% ", percent);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500426 if (dpynum)
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500427 printf("%u\r", dpynum);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500428 else
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500429 fputs(" \r", stdout);
430 fputs(ctx->stop_meta, stdout);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500431
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500432 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500433 e2fsck_clear_progbar(ctx);
434 fflush(stdout);
435
436 return 0;
437}
438
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000439static int e2fsck_update_progress(e2fsck_t ctx, int pass,
440 unsigned long cur, unsigned long max)
441{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000442 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000443 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000444
445 if (pass == 0)
446 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000447
448 if (ctx->progress_fd) {
449 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
450 write(ctx->progress_fd, buf, strlen(buf));
451 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000452 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500453 e2fsck_simple_progress(ctx, ctx->device_name,
454 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000455 }
456 return 0;
457}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000458
459#define PATH_SET "PATH=/sbin"
460
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000461static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000462{
463 int fd;
464
465 while (1) {
466 fd = open("/dev/null", O_RDWR);
467 if (fd > 2)
468 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000469 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000470 fprintf(stderr, _("ERROR: Couldn't open "
471 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000472 strerror(errno));
473 break;
474 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000475 }
476 close(fd);
477}
478
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000479#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500480static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000481{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000482 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000483
484 if (!ctx)
485 return;
486
487 ctx->progress = e2fsck_update_progress;
488 ctx->progress_fd = 0;
489}
490
Theodore Ts'o54434922003-12-07 01:28:50 -0500491static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000492{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000493 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000494
495 if (!ctx)
496 return;
497
498 e2fsck_clear_progbar(ctx);
499 ctx->progress = 0;
500}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400501
Theodore Ts'o54434922003-12-07 01:28:50 -0500502static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400503{
504 e2fsck_t ctx = e2fsck_global_ctx;
505
506 if (!ctx)
507 exit(FSCK_CANCELED);
508
509 ctx->flags |= E2F_FLAG_CANCEL;
510}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000511#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000512
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400513static void parse_extended_opts(e2fsck_t ctx, const char *opts)
514{
515 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500516 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400517 int extended_usage = 0;
518
Theodore Ts'of3640932003-03-01 19:47:44 -0500519 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400520 for (token = buf; token && *token; token = next) {
521 p = strchr(token, ',');
522 next = 0;
523 if (p) {
524 *p = 0;
525 next = p+1;
526 }
527 arg = strchr(token, '=');
528 if (arg) {
529 *arg = 0;
530 arg++;
531 }
532 if (strcmp(token, "ea_ver") == 0) {
533 if (!arg) {
534 extended_usage++;
535 continue;
536 }
537 ea_ver = strtoul(arg, &p, 0);
538 if (*p ||
539 ((ea_ver != 1) && (ea_ver != 2))) {
540 fprintf(stderr,
541 _("Invalid EA version.\n"));
542 extended_usage++;
543 continue;
544 }
545 ctx->ext_attr_ver = ea_ver;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400546 } else {
547 fprintf(stderr, _("Unknown extended option: %s\n"),
548 token);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400549 extended_usage++;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400550 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400551 }
552 if (extended_usage) {
Theodore Ts'obb145b02005-06-20 08:35:27 -0400553 fputs(("\nExtended options are separated by commas, "
554 "and may take an argument which\n"
555 "is set off by an equals ('=') sign. "
556 "Valid extended options are:\n"
557 "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400558 exit(1);
559 }
560}
561
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500562static void syntax_err_report(const char *filename, long err, int line_num)
563{
564 fprintf(stderr,
565 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
566 filename, line_num, error_message(err));
567 exit(FSCK_ERROR);
568}
569
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200570static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400571
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000572static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
573{
574 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000575 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000576#ifdef MTRACE
577 extern void *mallwatch;
578#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000579 e2fsck_t ctx;
580 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000581#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000582 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000583#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400584 char *extended_opts = 0;
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500585 char *cp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000586
587 retval = e2fsck_allocate_context(&ctx);
588 if (retval)
589 return retval;
590
591 *ret_ctx = ctx;
592
Theodore Ts'o908b7852003-04-16 15:20:13 -0400593 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
594 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400595 if (isatty(0) && isatty(1)) {
596 ctx->interactive = 1;
597 } else {
598 ctx->start_meta[0] = '\001';
599 ctx->stop_meta[0] = '\002';
600 }
601 memset(bar, '=', sizeof(bar)-1);
602 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000603 initialize_ext2_error_table();
Theodore Ts'o1017f652005-12-31 00:00:10 -0500604 initialize_prof_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500605 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000606
607 if (argc && *argv)
608 ctx->program_name = *argv;
609 else
610 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500611 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000613 case 'C':
614 ctx->progress = e2fsck_update_progress;
615 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000616 if (!ctx->progress_fd)
617 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000618 /* Validate the file descriptor to avoid disasters */
619 fd = dup(ctx->progress_fd);
620 if (fd < 0) {
621 fprintf(stderr,
622 _("Error validating file descriptor %d: %s\n"),
623 ctx->progress_fd,
624 error_message(errno));
625 fatal_error(ctx,
626 _("Invalid completion information file descriptor"));
627 } else
628 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000629 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400630 case 'D':
631 ctx->options |= E2F_OPT_COMPRESS_DIRS;
632 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400633 case 'E':
634 extended_opts = optarg;
635 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000636 case 'p':
637 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500638 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
639 conflict_opt:
640 fatal_error(ctx,
Theodore Ts'of4b6d2a2005-12-09 17:31:08 -0500641 _("Only one of the options -p/-a, -n or -y may be specified."));
Theodore Ts'o8161a742003-01-02 16:36:44 -0500642 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000643 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000644 break;
645 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500646 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
647 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000648 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000649 break;
650 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500651 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
652 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000653 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000654 break;
655 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000656#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000657 if (ctx->options & E2F_OPT_TIME)
658 ctx->options |= E2F_OPT_TIME2;
659 else
660 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000661#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000662 fprintf(stderr, _("The -t option is not "
663 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000664#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665 break;
666 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500667 if (cflag++)
668 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000669 ctx->options |= E2F_OPT_CHECKBLOCKS;
670 break;
671 case 'r':
672 /* What we do by default, anyway! */
673 break;
674 case 'b':
675 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400676 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000677 break;
678 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500679 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000680 break;
681 case 'I':
682 ctx->inode_buffer_blocks = atoi(optarg);
683 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400684 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500685 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400686 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000687 case 'P':
688 ctx->process_inode_size = atoi(optarg);
689 break;
690 case 'L':
691 replace_bad_blocks++;
692 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500693 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000694 break;
695 case 'd':
696 ctx->options |= E2F_OPT_DEBUG;
697 break;
698 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500699 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700 break;
701 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000702 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000703 break;
704 case 'v':
705 verbose = 1;
706 break;
707 case 'V':
708 show_version_only = 1;
709 break;
710#ifdef MTRACE
711 case 'M':
712 mallwatch = (void *) strtol(optarg, NULL, 0);
713 break;
714#endif
715 case 'N':
716 ctx->device_name = optarg;
717 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000718#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000719 case 's':
720 normalize_swapfs = 1;
721 case 'S':
722 swapfs = 1;
723 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000724#else
725 case 's':
726 case 'S':
727 fprintf(stderr, _("Byte-swapping filesystems "
728 "not compiled in this version "
729 "of e2fsck\n"));
730 exit(1);
731#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500732 case 'k':
733 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500734 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735 default:
736 usage(ctx);
737 }
738 if (show_version_only)
739 return 0;
740 if (optind != argc - 1)
741 usage(ctx);
742 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400743 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000744 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500745 ctx->io_options = strchr(argv[optind], '?');
746 if (ctx->io_options)
747 *ctx->io_options++ = 0;
Theodore Ts'of3640932003-03-01 19:47:44 -0500748 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500749 if (!ctx->filesystem_name) {
750 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
751 argv[optind]);
752 fatal_error(ctx, 0);
753 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400754 if (extended_opts)
755 parse_extended_opts(ctx, extended_opts);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500756
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500757 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
758 config_fn[0] = cp;
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500759 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500760 profile_init(config_fn, &ctx->profile);
761
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000762 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500763 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000764 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000765 com_err("open", errno,
766 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000767 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000768 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000769 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000770 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000771 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000772 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000773 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000774 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000775 }
776 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000777 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000778#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000779 if (swapfs) {
780 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000781 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500782 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000783 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000784 }
785 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000786#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500787 if (cflag && bad_blocks_file) {
788 fprintf(stderr, _("The -c and the -l/-L options may "
789 "not be both used at the same time.\n"));
790 exit(FSCK_USAGE);
791 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000792#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000793 /*
794 * Set up signal action
795 */
796 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400797 sa.sa_handler = signal_cancel;
798 sigaction(SIGINT, &sa, 0);
799 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000800#ifdef SA_RESTART
801 sa.sa_flags = SA_RESTART;
802#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000803 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000804 sa.sa_handler = signal_progress_on;
805 sigaction(SIGUSR1, &sa, 0);
806 sa.sa_handler = signal_progress_off;
807 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000808#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400809
810 /* Update our PATH to include /sbin if we need to run badblocks */
811 if (cflag) {
812 char *oldpath = getenv("PATH");
813 if (oldpath) {
814 char *newpath;
815
816 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
817 strlen (oldpath));
818 if (!newpath)
819 fatal_error(ctx, "Couldn't malloc() newpath");
820 strcpy (newpath, PATH_SET);
821 strcat (newpath, ":");
822 strcat (newpath, oldpath);
823 putenv (newpath);
824 } else
825 putenv (PATH_SET);
826 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500827#ifdef CONFIG_JBD_DEBUG
828 if (getenv("E2FSCK_JBD_DEBUG"))
829 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
830#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000831 return 0;
832}
833
834static const char *my_ver_string = E2FSPROGS_VERSION;
835static const char *my_ver_date = E2FSPROGS_DATE;
836
837int main (int argc, char *argv[])
838{
839 errcode_t retval = 0;
840 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000841 ext2_filsys fs = 0;
842 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000843 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000844 const char *lib_ver_date;
845 int my_ver, lib_ver;
846 e2fsck_t ctx;
847 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000848 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000849
850 clear_problem_context(&pctx);
851#ifdef MTRACE
852 mtrace();
853#endif
854#ifdef MCHECK
855 mcheck(0);
856#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000857#ifdef ENABLE_NLS
858 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500859 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000860 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
861 textdomain(NLS_CAT_NAME);
862#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000863 my_ver = ext2fs_parse_version_string(my_ver_string);
864 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
865 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000866 fprintf( stderr, _("Error: ext2fs library version "
867 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000868 show_version_only++;
869 }
870
871 retval = PRS(argc, argv, &ctx);
872 if (retval) {
873 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000874 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000875 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000876 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000877 reserve_stdio_fds();
878
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000879#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000880 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000881#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000882
883 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400884 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400885 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000886
887 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000888 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000889 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000890 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000891 }
892
893 check_mount(ctx);
894
895 if (!(ctx->options & E2F_OPT_PREEN) &&
896 !(ctx->options & E2F_OPT_NO) &&
897 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400898 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000899 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000900 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000901 }
902 ctx->superblock = ctx->use_superblock;
903restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400904#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000905 io_ptr = test_io_manager;
906 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400907#else
908 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000909#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000910 flags = 0;
911 if ((ctx->options & E2F_OPT_READONLY) == 0)
912 flags |= EXT2_FLAG_RW;
Theodore Ts'o2e14e0c2006-03-18 20:01:09 -0500913 if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
914 flags |= EXT2_FLAG_EXCLUSIVE;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000915
Theodore Ts'of1a17612001-12-23 22:27:52 -0500916 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500917 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
918 flags, ctx->superblock, ctx->blocksize,
919 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000920 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600921 int blocksize;
922 for (blocksize = EXT2_MIN_BLOCK_SIZE;
923 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500924 retval = ext2fs_open2(ctx->filesystem_name,
925 ctx->io_options, flags,
926 ctx->superblock, blocksize,
927 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000928 if (!retval)
929 break;
930 }
931 } else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500932 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
933 flags, 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400934 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
935 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000936 ((retval == EXT2_ET_BAD_MAGIC) ||
937 ((retval == 0) && ext2fs_check_desc(fs)))) {
938 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000939 printf(_("%s trying backup blocks...\n"),
940 retval ? _("Couldn't find ext2 superblock,") :
941 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500942 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000943 if (fs)
944 ext2fs_close(fs);
945 goto restart;
946 }
947 }
948 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000949 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000950 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000951 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000952 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000953 "too high for this version of e2fsck.\n"
954 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000955 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000956 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
957 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000958 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000959 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000960 printf(_("You must have %s access to the "
961 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000962 (ctx->options & E2F_OPT_READONLY) ?
963 "r/o" : "r/w");
964 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000965 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o2e14e0c2006-03-18 20:01:09 -0500966 else if (retval == EBUSY)
967 printf(_("Filesystem mounted or opened exclusively "
968 "by another program?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000969#ifdef EROFS
970 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000971 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000972 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000973 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000974#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000975 else
976 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000977 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000978 }
979 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000980 fs->priv_data = ctx;
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400981 fs->now = ctx->now;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000982 sb = fs->super;
983 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000984 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000985 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000986 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000987 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000988 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000989 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000990
Theodore Ts'o17390c02000-07-07 04:13:21 +0000991 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000992 * Set the device name, which is used whenever we print error
993 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000994 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000995 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000996 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500997 ctx->device_name = string_copy(ctx, sb->s_volume_name,
998 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000999 }
1000 if (ctx->device_name == 0)
1001 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001002
1003 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +00001004 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001005 */
1006 retval = e2fsck_check_ext3_journal(ctx);
1007 if (retval) {
1008 com_err(ctx->program_name, retval,
1009 _("while checking ext3 journal for %s"),
1010 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001011 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001012 }
1013
Theodore Ts'o9b565752000-12-13 18:50:22 +00001014 /*
1015 * Check to see if we need to do ext3-style recovery. If so,
1016 * do it, and then restart the fsck.
1017 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001018 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001019 if (ctx->options & E2F_OPT_READONLY) {
1020 printf(_("Warning: skipping journal recovery "
1021 "because doing a read-only filesystem "
1022 "check.\n"));
1023 io_channel_flush(ctx->fs->io);
1024 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001025 if (ctx->flags & E2F_FLAG_RESTARTED) {
1026 /*
1027 * Whoops, we attempted to run the
1028 * journal twice. This should never
1029 * happen, unless the hardware or
1030 * device driver is being bogus.
1031 */
1032 com_err(ctx->program_name, 0,
1033 _("unable to set superblock flags on %s\n"), ctx->device_name);
1034 fatal_error(ctx, 0);
1035 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001036 retval = e2fsck_run_ext3_journal(ctx);
1037 if (retval) {
1038 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001039 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001040 ctx->device_name);
1041 fatal_error(ctx, 0);
1042 }
1043 ext2fs_close(ctx->fs);
1044 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001045 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001046 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001047 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001048 }
1049
1050 /*
1051 * Check for compatibility with the feature sets. We need to
1052 * be more stringent than ext2fs_open().
1053 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001054 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1055 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001056 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1057 "(%s)", ctx->device_name);
1058 goto get_newer;
1059 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001060 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001061 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1062 "(%s)", ctx->device_name);
1063 goto get_newer;
1064 }
1065#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001066 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001067 com_err(ctx->program_name, 0,
1068 _("Warning: compression support is experimental.\n"));
1069#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001070#ifndef ENABLE_HTREE
1071 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1072 com_err(ctx->program_name, 0,
1073 _("E2fsck not compiled with HTREE support,\n\t"
1074 "but filesystem %s has HTREE directories.\n"),
1075 ctx->device_name);
1076 goto get_newer;
1077 }
1078#endif
1079
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001080 /*
1081 * If the user specified a specific superblock, presumably the
1082 * master superblock has been trashed. So we mark the
1083 * superblock as dirty, so it can be written out.
1084 */
1085 if (ctx->superblock &&
1086 !(ctx->options & E2F_OPT_READONLY))
1087 ext2fs_mark_super_dirty(fs);
1088
1089 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001090 * We only update the master superblock because (a) paranoia;
1091 * we don't want to corrupt the backup superblocks, and (b) we
1092 * don't need to update the mount count and last checked
1093 * fields in the backup superblock (the kernel doesn't
1094 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001095 */
1096 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1097
1098 ehandler_init(fs->io);
1099
1100 if (ctx->superblock)
1101 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
Theodore Ts'o550a4af2005-01-25 03:09:24 -05001102 ext2fs_mark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001103 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001104 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001105 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001106 check_if_skip(ctx);
1107 if (bad_blocks_file)
1108 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1109 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001110 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001111 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001112 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001113#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001114 if (normalize_swapfs) {
1115 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1116 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001117 fprintf(stderr, _("%s: Filesystem byte order "
1118 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001119 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001120 }
1121 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001122 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001123 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001124 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001125 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001126 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001127#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001128
1129 /*
1130 * Mark the system as valid, 'til proven otherwise
1131 */
1132 ext2fs_mark_valid(fs);
1133
1134 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1135 if (retval) {
1136 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001137 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001138 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001139 printf(_("This doesn't bode well,"
1140 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001141 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001142
1143 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001144 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001145 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001146 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001147 retval = e2fsck_reset_context(ctx);
1148 if (retval) {
1149 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001150 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001151 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001152 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001153 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001154 goto restart;
1155 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001156 if (run_result & E2F_FLAG_CANCEL) {
1157 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1158 ctx->device_name : ctx->filesystem_name);
1159 exit_value |= FSCK_CANCELED;
1160 }
1161 if (run_result & E2F_FLAG_ABORT)
1162 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001163
1164#ifdef MTRACE
1165 mtrace_print("Cleanup");
1166#endif
1167 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001168 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001169 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001170 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001171 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001172 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001173 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001174 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001175 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001176 }
1177 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001178 if (!ext2fs_test_valid(fs) ||
1179 ((exit_value & FSCK_CANCELED) &&
1180 (sb->s_state & EXT2_ERROR_FS))) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001181 printf(_("\n%s: ********** WARNING: Filesystem still has "
1182 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001183 exit_value |= FSCK_UNCORRECTED;
1184 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001185 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001186 if (exit_value & FSCK_CANCELED) {
1187 int allow_cancellation;
1188
1189 profile_get_boolean(ctx->profile, "options",
1190 "allow_cancellation", 0, 0,
1191 &allow_cancellation);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001192 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001193 if (allow_cancellation && ext2fs_test_valid(fs) &&
1194 (sb->s_state & EXT2_VALID_FS) &&
1195 !(sb->s_state & EXT2_ERROR_FS))
1196 exit_value = 0;
1197 } else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001198 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001199 if (!(ctx->options & E2F_OPT_READONLY)) {
1200 if (ext2fs_test_valid(fs)) {
1201 if (!(sb->s_state & EXT2_VALID_FS))
1202 exit_value |= FSCK_NONDESTRUCT;
1203 sb->s_state = EXT2_VALID_FS;
1204 } else
1205 sb->s_state &= ~EXT2_VALID_FS;
1206 sb->s_mnt_count = 0;
Theodore Ts'o1f3ad142005-04-14 14:07:53 -04001207 sb->s_lastcheck = ctx->now;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001208 ext2fs_mark_super_dirty(fs);
1209 }
1210 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001211
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001212 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001213
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001214 ext2fs_close(fs);
1215 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001216 free(ctx->filesystem_name);
1217 free(ctx->journal_name);
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001218
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001219#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001220 if (ctx->options & E2F_OPT_TIME)
1221 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001222#endif
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001223 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001224 return exit_value;
1225}