blob: 4a7e08ae1177f38241ed3609f851eeb8ef71e02d [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;
101 int inodes, inodes_used, blocks, blocks_used;
102 int dir_links;
103 int num_files, num_links;
104 int frag_percent;
105
106 dir_links = 2 * ctx->fs_directory_count - 1;
107 num_files = ctx->fs_total_count - dir_links;
108 num_links = ctx->fs_links_count - dir_links;
109 inodes = fs->super->s_inodes_count;
110 inodes_used = (fs->super->s_inodes_count -
111 fs->super->s_free_inodes_count);
112 blocks = fs->super->s_blocks_count;
113 blocks_used = (fs->super->s_blocks_count -
114 fs->super->s_free_blocks_count);
115
116 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
117 frag_percent = (frag_percent + 5) / 10;
118
119 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000120 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 ctx->device_name, inodes_used, inodes,
122 frag_percent / 10, frag_percent % 10,
123 blocks_used, blocks);
124 return;
125 }
Theodore Ts'o113e4052003-05-17 21:00:46 -0400126 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
127 inodes_used), inodes_used, 100 * inodes_used / inodes);
128 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
129 "%8d non-contiguous inodes (%0d.%d%%)\n",
130 ctx->fs_fragmented),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000131 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
132 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
133 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Theodore Ts'o113e4052003-05-17 21:00:46 -0400134 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
135 blocks_used),
136 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
137 printf (P_("%8d bad block\n", "%8d bad blocks\n",
138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
139 printf (P_("%8d large file\n", "%8d large files\n",
140 ctx->large_files), ctx->large_files);
141 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
142 ctx->fs_regular_count), ctx->fs_regular_count);
143 printf (P_("%8d directory\n", "%8d directories\n",
144 ctx->fs_directory_count), ctx->fs_directory_count);
145 printf (P_("%8d character device file\n",
146 "%8d character device files\n", ctx->fs_chardev_count),
147 ctx->fs_chardev_count);
148 printf (P_("%8d block device file\n", "%8d block device files\n",
149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
150 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
151 ctx->fs_fifo_count);
152 printf (P_("%8d link\n", "%8d links\n",
153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
155 printf (P_("%8d symbolic link", "%8d symbolic links",
156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
157 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
159 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
160 ctx->fs_sockets_count);
161 printf ("--------\n");
162 printf (P_("%8d file\n", "%8d files\n",
163 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'o1b6bf171997-10-03 17:48:10 +0000262
Theodore Ts'od37066a2001-12-21 23:28:54 -0500263 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
264 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000265 return;
266
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500267 if ((fs->super->s_state & EXT2_ERROR_FS) ||
268 !ext2fs_test_valid(fs))
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000269 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000270 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000271 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000272 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000273 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000274 (unsigned) fs->super->s_max_mnt_count)) {
275 reason = _(" has been mounted %u times without being checked");
276 reason_arg = fs->super->s_mnt_count;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500277 if (batt && (fs->super->s_mnt_count <
278 (unsigned) fs->super->s_max_mnt_count*2))
279 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000280 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400281 ((ctx->now - fs->super->s_lastcheck) >=
Theodore Ts'o54434922003-12-07 01:28:50 -0500282 fs->super->s_checkinterval)) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000283 reason = _(" has gone %u days without being checked");
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400284 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
285 if (batt && ((ctx->now - fs->super->s_lastcheck) <
Theodore Ts'o54434922003-12-07 01:28:50 -0500286 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500287 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000288 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000289 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000290 fputs(ctx->device_name, stdout);
291 printf(reason, reason_arg);
292 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000293 return;
294 }
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500295 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000296 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
297 fs->super->s_inodes_count,
298 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
299 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500300 next_check = 100000;
301 if (fs->super->s_max_mnt_count > 0) {
302 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
303 if (next_check <= 0)
304 next_check = 1;
305 }
Theodore Ts'o66fbee82004-05-04 20:38:17 -0400306 if (fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400307 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500308 next_check = 1;
309 if (next_check <= 5) {
Theodore Ts'obc3392c2006-01-29 05:05:31 -0500310 if (next_check == 1) {
311 if (batt)
312 fputs(_(" (check deferred; on battery)"),
313 stdout);
314 else
315 fputs(_(" (check after next mount)"), stdout);
316 } else
Theodore Ts'o54434922003-12-07 01:28:50 -0500317 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500318 }
319 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000320 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400321 ctx->fs = NULL;
322 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000323 exit(FSCK_OK);
324}
325
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000326/*
327 * For completion notice
328 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000329struct percent_tbl {
330 int max_pass;
331 int table[32];
332};
333struct percent_tbl e2fsck_tbl = {
334 5, { 0, 70, 90, 92, 95, 100 }
335};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400336static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000337
338static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
339 int max)
340{
341 float percent;
342
343 if (pass <= 0)
344 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000345 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000346 return 100.0;
347 percent = ((float) curr) / ((float) max);
348 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
349 + tbl->table[pass-1]);
350}
351
352extern void e2fsck_clear_progbar(e2fsck_t ctx)
353{
354 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
355 return;
356
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400357 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
358 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400359 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000360 ctx->flags &= ~E2F_FLAG_PROG_BAR;
361}
362
Theodore Ts'o520ead32003-04-19 13:48:27 -0400363int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500364 unsigned int dpynum)
365{
366 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500367 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500368 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500369 struct timeval tv;
370 int dpywidth;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500371 int fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500372
373 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
374 return 0;
375
376 /*
377 * Calculate the new progress position. If the
378 * percentage hasn't changed, then we skip out right
379 * away.
380 */
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500381 fixed_percent = (int) ((10 * percent) + 0.5);
382 if (ctx->progress_last_percent == fixed_percent)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500383 return 0;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500384 ctx->progress_last_percent = fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500385
386 /*
387 * If we've already updated the spinner once within
388 * the last 1/8th of a second, no point doing it
389 * again.
390 */
391 gettimeofday(&tv, NULL);
392 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
393 if ((tick == ctx->progress_last_time) &&
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500394 (fixed_percent != 0) && (fixed_percent != 1000))
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500395 return 0;
396 ctx->progress_last_time = tick;
397
398 /*
399 * Advance the spinner, and note that the progress bar
400 * will be on the screen
401 */
402 ctx->progress_pos = (ctx->progress_pos+1) & 3;
403 ctx->flags |= E2F_FLAG_PROG_BAR;
404
405 dpywidth = 66 - strlen(label);
406 dpywidth = 8 * (dpywidth / 8);
407 if (dpynum)
408 dpywidth -= 8;
409
410 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400411 printf("%s%s: |%s%s", ctx->start_meta, label,
412 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500413 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500414 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500415 fputc('|', stdout);
416 else
417 fputc(spinner[ctx->progress_pos & 3], stdout);
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500418 printf(" %4.1f%% ", percent);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500419 if (dpynum)
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500420 printf("%u\r", dpynum);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500421 else
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500422 fputs(" \r", stdout);
423 fputs(ctx->stop_meta, stdout);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500424
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500425 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500426 e2fsck_clear_progbar(ctx);
427 fflush(stdout);
428
429 return 0;
430}
431
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000432static int e2fsck_update_progress(e2fsck_t ctx, int pass,
433 unsigned long cur, unsigned long max)
434{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000435 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000436 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000437
438 if (pass == 0)
439 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000440
441 if (ctx->progress_fd) {
442 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
443 write(ctx->progress_fd, buf, strlen(buf));
444 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000445 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500446 e2fsck_simple_progress(ctx, ctx->device_name,
447 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000448 }
449 return 0;
450}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000451
452#define PATH_SET "PATH=/sbin"
453
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000454static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000455{
456 int fd;
457
458 while (1) {
459 fd = open("/dev/null", O_RDWR);
460 if (fd > 2)
461 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000462 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000463 fprintf(stderr, _("ERROR: Couldn't open "
464 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000465 strerror(errno));
466 break;
467 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000468 }
469 close(fd);
470}
471
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000472#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500473static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000474{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000475 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000476
477 if (!ctx)
478 return;
479
480 ctx->progress = e2fsck_update_progress;
481 ctx->progress_fd = 0;
482}
483
Theodore Ts'o54434922003-12-07 01:28:50 -0500484static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000485{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000486 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000487
488 if (!ctx)
489 return;
490
491 e2fsck_clear_progbar(ctx);
492 ctx->progress = 0;
493}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400494
Theodore Ts'o54434922003-12-07 01:28:50 -0500495static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400496{
497 e2fsck_t ctx = e2fsck_global_ctx;
498
499 if (!ctx)
500 exit(FSCK_CANCELED);
501
502 ctx->flags |= E2F_FLAG_CANCEL;
503}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000504#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000505
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400506static void parse_extended_opts(e2fsck_t ctx, const char *opts)
507{
508 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500509 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400510 int extended_usage = 0;
511
Theodore Ts'of3640932003-03-01 19:47:44 -0500512 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400513 for (token = buf; token && *token; token = next) {
514 p = strchr(token, ',');
515 next = 0;
516 if (p) {
517 *p = 0;
518 next = p+1;
519 }
520 arg = strchr(token, '=');
521 if (arg) {
522 *arg = 0;
523 arg++;
524 }
525 if (strcmp(token, "ea_ver") == 0) {
526 if (!arg) {
527 extended_usage++;
528 continue;
529 }
530 ea_ver = strtoul(arg, &p, 0);
531 if (*p ||
532 ((ea_ver != 1) && (ea_ver != 2))) {
533 fprintf(stderr,
534 _("Invalid EA version.\n"));
535 extended_usage++;
536 continue;
537 }
538 ctx->ext_attr_ver = ea_ver;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400539 } else {
540 fprintf(stderr, _("Unknown extended option: %s\n"),
541 token);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400542 extended_usage++;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400543 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400544 }
545 if (extended_usage) {
Theodore Ts'obb145b02005-06-20 08:35:27 -0400546 fputs(("\nExtended options are separated by commas, "
547 "and may take an argument which\n"
548 "is set off by an equals ('=') sign. "
549 "Valid extended options are:\n"
550 "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400551 exit(1);
552 }
553}
554
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500555static void syntax_err_report(const char *filename, long err, int line_num)
556{
557 fprintf(stderr,
558 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
559 filename, line_num, error_message(err));
560 exit(FSCK_ERROR);
561}
562
Theodore Ts'o1017f652005-12-31 00:00:10 -0500563static const char *config_fn[] = { "/etc/e2fsck.conf", 0 };
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400564
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000565static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
566{
567 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000568 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000569#ifdef MTRACE
570 extern void *mallwatch;
571#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000572 e2fsck_t ctx;
573 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000574#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000575 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000576#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400577 char *extended_opts = 0;
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500578 char *cp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000579
580 retval = e2fsck_allocate_context(&ctx);
581 if (retval)
582 return retval;
583
584 *ret_ctx = ctx;
585
Theodore Ts'o908b7852003-04-16 15:20:13 -0400586 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
587 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400588 if (isatty(0) && isatty(1)) {
589 ctx->interactive = 1;
590 } else {
591 ctx->start_meta[0] = '\001';
592 ctx->stop_meta[0] = '\002';
593 }
594 memset(bar, '=', sizeof(bar)-1);
595 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000596 initialize_ext2_error_table();
Theodore Ts'o1017f652005-12-31 00:00:10 -0500597 initialize_prof_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500598 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000599
600 if (argc && *argv)
601 ctx->program_name = *argv;
602 else
603 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500604 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 +0000605 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000606 case 'C':
607 ctx->progress = e2fsck_update_progress;
608 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000609 if (!ctx->progress_fd)
610 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000611 /* Validate the file descriptor to avoid disasters */
612 fd = dup(ctx->progress_fd);
613 if (fd < 0) {
614 fprintf(stderr,
615 _("Error validating file descriptor %d: %s\n"),
616 ctx->progress_fd,
617 error_message(errno));
618 fatal_error(ctx,
619 _("Invalid completion information file descriptor"));
620 } else
621 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000622 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400623 case 'D':
624 ctx->options |= E2F_OPT_COMPRESS_DIRS;
625 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400626 case 'E':
627 extended_opts = optarg;
628 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 case 'p':
630 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500631 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
632 conflict_opt:
633 fatal_error(ctx,
Theodore Ts'of4b6d2a2005-12-09 17:31:08 -0500634 _("Only one of the options -p/-a, -n or -y may be specified."));
Theodore Ts'o8161a742003-01-02 16:36:44 -0500635 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000636 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000637 break;
638 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500639 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
640 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000641 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000642 break;
643 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500644 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
645 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000646 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000647 break;
648 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000649#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000650 if (ctx->options & E2F_OPT_TIME)
651 ctx->options |= E2F_OPT_TIME2;
652 else
653 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000654#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000655 fprintf(stderr, _("The -t option is not "
656 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000657#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000658 break;
659 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500660 if (cflag++)
661 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 ctx->options |= E2F_OPT_CHECKBLOCKS;
663 break;
664 case 'r':
665 /* What we do by default, anyway! */
666 break;
667 case 'b':
668 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400669 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670 break;
671 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500672 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000673 break;
674 case 'I':
675 ctx->inode_buffer_blocks = atoi(optarg);
676 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400677 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500678 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400679 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000680 case 'P':
681 ctx->process_inode_size = atoi(optarg);
682 break;
683 case 'L':
684 replace_bad_blocks++;
685 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500686 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000687 break;
688 case 'd':
689 ctx->options |= E2F_OPT_DEBUG;
690 break;
691 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500692 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000693 break;
694 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000695 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000696 break;
697 case 'v':
698 verbose = 1;
699 break;
700 case 'V':
701 show_version_only = 1;
702 break;
703#ifdef MTRACE
704 case 'M':
705 mallwatch = (void *) strtol(optarg, NULL, 0);
706 break;
707#endif
708 case 'N':
709 ctx->device_name = optarg;
710 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000711#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000712 case 's':
713 normalize_swapfs = 1;
714 case 'S':
715 swapfs = 1;
716 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000717#else
718 case 's':
719 case 'S':
720 fprintf(stderr, _("Byte-swapping filesystems "
721 "not compiled in this version "
722 "of e2fsck\n"));
723 exit(1);
724#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500725 case 'k':
726 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500727 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000728 default:
729 usage(ctx);
730 }
731 if (show_version_only)
732 return 0;
733 if (optind != argc - 1)
734 usage(ctx);
735 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400736 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000737 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500738 ctx->io_options = strchr(argv[optind], '?');
739 if (ctx->io_options)
740 *ctx->io_options++ = 0;
Theodore Ts'of3640932003-03-01 19:47:44 -0500741 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500742 if (!ctx->filesystem_name) {
743 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
744 argv[optind]);
745 fatal_error(ctx, 0);
746 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400747 if (extended_opts)
748 parse_extended_opts(ctx, extended_opts);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500749
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500750 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
751 config_fn[0] = cp;
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500752 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500753 profile_init(config_fn, &ctx->profile);
754
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000755 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500756 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000757 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000758 com_err("open", errno,
759 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000760 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000761 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000762 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000763 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000764 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000765 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000766 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000767 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000768 }
769 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000770 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000771#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000772 if (swapfs) {
773 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000774 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500775 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000776 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000777 }
778 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000779#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500780 if (cflag && bad_blocks_file) {
781 fprintf(stderr, _("The -c and the -l/-L options may "
782 "not be both used at the same time.\n"));
783 exit(FSCK_USAGE);
784 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000785#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000786 /*
787 * Set up signal action
788 */
789 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400790 sa.sa_handler = signal_cancel;
791 sigaction(SIGINT, &sa, 0);
792 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000793#ifdef SA_RESTART
794 sa.sa_flags = SA_RESTART;
795#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000796 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000797 sa.sa_handler = signal_progress_on;
798 sigaction(SIGUSR1, &sa, 0);
799 sa.sa_handler = signal_progress_off;
800 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000801#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400802
803 /* Update our PATH to include /sbin if we need to run badblocks */
804 if (cflag) {
805 char *oldpath = getenv("PATH");
806 if (oldpath) {
807 char *newpath;
808
809 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
810 strlen (oldpath));
811 if (!newpath)
812 fatal_error(ctx, "Couldn't malloc() newpath");
813 strcpy (newpath, PATH_SET);
814 strcat (newpath, ":");
815 strcat (newpath, oldpath);
816 putenv (newpath);
817 } else
818 putenv (PATH_SET);
819 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500820#ifdef CONFIG_JBD_DEBUG
821 if (getenv("E2FSCK_JBD_DEBUG"))
822 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
823#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000824 return 0;
825}
826
827static const char *my_ver_string = E2FSPROGS_VERSION;
828static const char *my_ver_date = E2FSPROGS_DATE;
829
830int main (int argc, char *argv[])
831{
832 errcode_t retval = 0;
833 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000834 ext2_filsys fs = 0;
835 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000836 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000837 const char *lib_ver_date;
838 int my_ver, lib_ver;
839 e2fsck_t ctx;
840 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000841 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000842
843 clear_problem_context(&pctx);
844#ifdef MTRACE
845 mtrace();
846#endif
847#ifdef MCHECK
848 mcheck(0);
849#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000850#ifdef ENABLE_NLS
851 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500852 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000853 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
854 textdomain(NLS_CAT_NAME);
855#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000856 my_ver = ext2fs_parse_version_string(my_ver_string);
857 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
858 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000859 fprintf( stderr, _("Error: ext2fs library version "
860 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000861 show_version_only++;
862 }
863
864 retval = PRS(argc, argv, &ctx);
865 if (retval) {
866 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000867 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000868 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000869 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000870 reserve_stdio_fds();
871
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000872#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000873 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000874#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000875
876 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400877 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400878 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000879
880 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000881 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000882 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000883 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000884 }
885
886 check_mount(ctx);
887
888 if (!(ctx->options & E2F_OPT_PREEN) &&
889 !(ctx->options & E2F_OPT_NO) &&
890 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400891 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000892 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000893 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000894 }
895 ctx->superblock = ctx->use_superblock;
896restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400897#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000898 io_ptr = test_io_manager;
899 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400900#else
901 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000902#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000903 flags = 0;
904 if ((ctx->options & E2F_OPT_READONLY) == 0)
905 flags |= EXT2_FLAG_RW;
906
Theodore Ts'of1a17612001-12-23 22:27:52 -0500907 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500908 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
909 flags, ctx->superblock, ctx->blocksize,
910 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000911 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600912 int blocksize;
913 for (blocksize = EXT2_MIN_BLOCK_SIZE;
914 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500915 retval = ext2fs_open2(ctx->filesystem_name,
916 ctx->io_options, flags,
917 ctx->superblock, blocksize,
918 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000919 if (!retval)
920 break;
921 }
922 } else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500923 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
924 flags, 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400925 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
926 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000927 ((retval == EXT2_ET_BAD_MAGIC) ||
928 ((retval == 0) && ext2fs_check_desc(fs)))) {
929 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000930 printf(_("%s trying backup blocks...\n"),
931 retval ? _("Couldn't find ext2 superblock,") :
932 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500933 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000934 if (fs)
935 ext2fs_close(fs);
936 goto restart;
937 }
938 }
939 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000940 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000941 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000942 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000943 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000944 "too high for this version of e2fsck.\n"
945 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000946 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000947 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
948 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000949 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000950 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000951 printf(_("You must have %s access to the "
952 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000953 (ctx->options & E2F_OPT_READONLY) ?
954 "r/o" : "r/w");
955 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000956 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000957#ifdef EROFS
958 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000959 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000960 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000961 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000962#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000963 else
964 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000965 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000966 }
967 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000968 fs->priv_data = ctx;
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400969 fs->now = ctx->now;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000970 sb = fs->super;
971 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000972 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000973 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000974 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000975 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000976 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000977 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000978
Theodore Ts'o17390c02000-07-07 04:13:21 +0000979 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000980 * Set the device name, which is used whenever we print error
981 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000982 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000983 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000984 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500985 ctx->device_name = string_copy(ctx, sb->s_volume_name,
986 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000987 }
988 if (ctx->device_name == 0)
989 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000990
991 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000992 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000993 */
994 retval = e2fsck_check_ext3_journal(ctx);
995 if (retval) {
996 com_err(ctx->program_name, retval,
997 _("while checking ext3 journal for %s"),
998 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000999 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001000 }
1001
Theodore Ts'o9b565752000-12-13 18:50:22 +00001002 /*
1003 * Check to see if we need to do ext3-style recovery. If so,
1004 * do it, and then restart the fsck.
1005 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001006 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001007 if (ctx->options & E2F_OPT_READONLY) {
1008 printf(_("Warning: skipping journal recovery "
1009 "because doing a read-only filesystem "
1010 "check.\n"));
1011 io_channel_flush(ctx->fs->io);
1012 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001013 if (ctx->flags & E2F_FLAG_RESTARTED) {
1014 /*
1015 * Whoops, we attempted to run the
1016 * journal twice. This should never
1017 * happen, unless the hardware or
1018 * device driver is being bogus.
1019 */
1020 com_err(ctx->program_name, 0,
1021 _("unable to set superblock flags on %s\n"), ctx->device_name);
1022 fatal_error(ctx, 0);
1023 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001024 retval = e2fsck_run_ext3_journal(ctx);
1025 if (retval) {
1026 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001027 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001028 ctx->device_name);
1029 fatal_error(ctx, 0);
1030 }
1031 ext2fs_close(ctx->fs);
1032 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001033 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001034 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001035 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001036 }
1037
1038 /*
1039 * Check for compatibility with the feature sets. We need to
1040 * be more stringent than ext2fs_open().
1041 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001042 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1043 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001044 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1045 "(%s)", ctx->device_name);
1046 goto get_newer;
1047 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001048 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001049 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1050 "(%s)", ctx->device_name);
1051 goto get_newer;
1052 }
1053#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001054 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001055 com_err(ctx->program_name, 0,
1056 _("Warning: compression support is experimental.\n"));
1057#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001058#ifndef ENABLE_HTREE
1059 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1060 com_err(ctx->program_name, 0,
1061 _("E2fsck not compiled with HTREE support,\n\t"
1062 "but filesystem %s has HTREE directories.\n"),
1063 ctx->device_name);
1064 goto get_newer;
1065 }
1066#endif
1067
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001068 /*
1069 * If the user specified a specific superblock, presumably the
1070 * master superblock has been trashed. So we mark the
1071 * superblock as dirty, so it can be written out.
1072 */
1073 if (ctx->superblock &&
1074 !(ctx->options & E2F_OPT_READONLY))
1075 ext2fs_mark_super_dirty(fs);
1076
1077 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001078 * We only update the master superblock because (a) paranoia;
1079 * we don't want to corrupt the backup superblocks, and (b) we
1080 * don't need to update the mount count and last checked
1081 * fields in the backup superblock (the kernel doesn't
1082 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001083 */
1084 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1085
1086 ehandler_init(fs->io);
1087
1088 if (ctx->superblock)
1089 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
Theodore Ts'o550a4af2005-01-25 03:09:24 -05001090 ext2fs_mark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001091 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001092 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001093 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001094 check_if_skip(ctx);
1095 if (bad_blocks_file)
1096 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1097 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001098 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001099 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001100 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001101#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001102 if (normalize_swapfs) {
1103 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1104 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001105 fprintf(stderr, _("%s: Filesystem byte order "
1106 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001107 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001108 }
1109 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001110 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001111 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001112 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001113 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001114 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001115#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001116
1117 /*
1118 * Mark the system as valid, 'til proven otherwise
1119 */
1120 ext2fs_mark_valid(fs);
1121
1122 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1123 if (retval) {
1124 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001125 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001126 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001127 printf(_("This doesn't bode well,"
1128 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001129 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001130
1131 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001132 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001133 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001134 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001135 retval = e2fsck_reset_context(ctx);
1136 if (retval) {
1137 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001138 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001139 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001140 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001141 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001142 goto restart;
1143 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001144 if (run_result & E2F_FLAG_CANCEL) {
1145 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1146 ctx->device_name : ctx->filesystem_name);
1147 exit_value |= FSCK_CANCELED;
1148 }
1149 if (run_result & E2F_FLAG_ABORT)
1150 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001151
1152#ifdef MTRACE
1153 mtrace_print("Cleanup");
1154#endif
1155 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001156 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001157 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001158 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001159 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001160 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001161 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001162 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001163 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001164 }
1165 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001166 if (!ext2fs_test_valid(fs) ||
1167 ((exit_value & FSCK_CANCELED) &&
1168 (sb->s_state & EXT2_ERROR_FS))) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001169 printf(_("\n%s: ********** WARNING: Filesystem still has "
1170 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001171 exit_value |= FSCK_UNCORRECTED;
1172 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001173 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001174 if (exit_value & FSCK_CANCELED) {
1175 int allow_cancellation;
1176
1177 profile_get_boolean(ctx->profile, "options",
1178 "allow_cancellation", 0, 0,
1179 &allow_cancellation);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001180 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001181 if (allow_cancellation && ext2fs_test_valid(fs) &&
1182 (sb->s_state & EXT2_VALID_FS) &&
1183 !(sb->s_state & EXT2_ERROR_FS))
1184 exit_value = 0;
1185 } else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001186 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001187 if (!(ctx->options & E2F_OPT_READONLY)) {
1188 if (ext2fs_test_valid(fs)) {
1189 if (!(sb->s_state & EXT2_VALID_FS))
1190 exit_value |= FSCK_NONDESTRUCT;
1191 sb->s_state = EXT2_VALID_FS;
1192 } else
1193 sb->s_state &= ~EXT2_VALID_FS;
1194 sb->s_mnt_count = 0;
Theodore Ts'o1f3ad142005-04-14 14:07:53 -04001195 sb->s_lastcheck = ctx->now;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001196 ext2fs_mark_super_dirty(fs);
1197 }
1198 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001199
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001200 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001201
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001202 ext2fs_close(fs);
1203 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001204 free(ctx->filesystem_name);
1205 free(ctx->journal_name);
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001206
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001207#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001208 if (ctx->options & E2F_OPT_TIME)
1209 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001210#endif
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001211 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001212 return exit_value;
1213}