blob: c9c11b32e231781fdbf180dde0a2f5a4dacf219f [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
36#include <sys/ioctl.h>
37#include <malloc.h>
38
39#include "et/com_err.h"
40#include "e2fsck.h"
41#include "problem.h"
42#include "../version.h"
43
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000044/* Command line options */
45static int blocksize = 0;
46static int swapfs = 0;
47static int normalize_swapfs = 0;
48static int cflag = 0; /* check disk */
49static int show_version_only = 0;
50static int force = 0;
51static int verbose = 0;
52
53static int replace_bad_blocks = 0;
54static char *bad_blocks_file = 0;
55
56static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
57
58static int root_filesystem = 0;
59static int read_only_root = 0;
60
Theodore Ts'o243dc312000-08-22 21:37:47 +000061e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
62
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000063static void usage(e2fsck_t ctx)
64{
65 fprintf(stderr,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000066 _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000067 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040068 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000069 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000070
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000071 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000072 " -p Automatic repair (no questions)\n"
73 " -n Make no changes to the filesystem\n"
74 " -y Assume \"yes\" to all questions\n"
75 " -c Check for bad blocks\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000076 " -f Force checking even if filesystem is marked clean\n"));
77 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000078 " -v Be verbose\n"
79 " -b superblock Use alternative superblock\n"
80 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040081 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000082 " -l bad_blocks_file Add to badblocks list\n"
83 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000084 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000085
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000086 exit(FSCK_USAGE);
87}
88
89static void show_stats(e2fsck_t ctx)
90{
91 ext2_filsys fs = ctx->fs;
92 int inodes, inodes_used, blocks, blocks_used;
93 int dir_links;
94 int num_files, num_links;
95 int frag_percent;
96
97 dir_links = 2 * ctx->fs_directory_count - 1;
98 num_files = ctx->fs_total_count - dir_links;
99 num_links = ctx->fs_links_count - dir_links;
100 inodes = fs->super->s_inodes_count;
101 inodes_used = (fs->super->s_inodes_count -
102 fs->super->s_free_inodes_count);
103 blocks = fs->super->s_blocks_count;
104 blocks_used = (fs->super->s_blocks_count -
105 fs->super->s_free_blocks_count);
106
107 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
108 frag_percent = (frag_percent + 5) / 10;
109
110 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000111 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000112 ctx->device_name, inodes_used, inodes,
113 frag_percent / 10, frag_percent % 10,
114 blocks_used, blocks);
115 return;
116 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000117 /*
118 * This is a bit ugly. But I think there will nearly always be more
119 * than one "thing" to report about, so I won't try writing complex
120 * code to handle one/two/many forms of all words.
121 * Some languages (Italian, at least) never uses the plural form
122 * of foreign words, so in real life this could not be a problem.
123 * md@linux.it - 2000-1-15
124 */
125#ifdef ENABLE_NLS
126 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
127 (inodes_used != 1), 100 * inodes_used / inodes);
128 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
129 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
130 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
131 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
132 printf (_("%8d blocks used (%d%%)\n"
133 "%8d bad blocks\n"), blocks_used,
Theodore Ts'o636a9542001-06-29 17:57:26 -0400134 (int) ((long long) 100 * blocks_used / blocks),
135 ctx->fs_badblocks_count);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000136 printf (_("\n%8d regular files\n"
137 "%8d directories\n"
138 "%8d character device files\n"
139 "%8d block device files\n"
140 "%8d fifos\n"
141 "%8d links\n"
142 "%8d symbolic links (%d fast symbolic links)\n"
143 "%8d sockets\n"
144 "--------\n"
145 "%8d files\n"),
146 ctx->fs_regular_count,
147 ctx->fs_directory_count,
148 ctx->fs_chardev_count,
149 ctx->fs_blockdev_count,
150 ctx->fs_fifo_count,
151 ctx->fs_links_count - dir_links,
152 ctx->fs_symlinks_count,
153 ctx->fs_fast_symlinks_count,
154 ctx->fs_sockets_count,
155 ctx->fs_total_count - dir_links);
156#else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000157 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
158 (inodes_used != 1) ? "s" : "",
159 100 * inodes_used / inodes);
160 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
161 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
162 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
163 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
164 printf ("%8d block%s used (%d%%)\n"
165 "%8d bad block%s\n", blocks_used,
166 (blocks_used != 1) ? "s" : "",
167 100 * blocks_used / blocks, ctx->fs_badblocks_count,
168 ctx->fs_badblocks_count != 1 ? "s" : "");
169 printf ("\n%8d regular file%s\n"
170 "%8d director%s\n"
171 "%8d character device file%s\n"
172 "%8d block device file%s\n"
173 "%8d fifo%s\n"
174 "%8d link%s\n"
175 "%8d symbolic link%s (%d fast symbolic link%s)\n"
176 "%8d socket%s\n"
177 "--------\n"
178 "%8d file%s\n",
179 ctx->fs_regular_count,
180 (ctx->fs_regular_count != 1) ? "s" : "",
181 ctx->fs_directory_count,
182 (ctx->fs_directory_count != 1) ? "ies" : "y",
183 ctx->fs_chardev_count,
184 (ctx->fs_chardev_count != 1) ? "s" : "",
185 ctx->fs_blockdev_count,
186 (ctx->fs_blockdev_count != 1) ? "s" : "",
187 ctx->fs_fifo_count,
188 (ctx->fs_fifo_count != 1) ? "s" : "",
189 ctx->fs_links_count - dir_links,
190 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
191 ctx->fs_symlinks_count,
192 (ctx->fs_symlinks_count != 1) ? "s" : "",
193 ctx->fs_fast_symlinks_count,
194 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
195 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
196 ctx->fs_total_count - dir_links,
197 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000198#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000199}
200
201static void check_mount(e2fsck_t ctx)
202{
203 errcode_t retval;
204 int mount_flags, cont, fd;
205
206 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
207 if (retval) {
208 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000209 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000210 ctx->filesystem_name);
211 return;
212 }
213 if (!(mount_flags & EXT2_MF_MOUNTED))
214 return;
215
216#if (defined(__linux__) && defined(HAVE_MNTENT_H))
217 /*
218 * If the root is mounted read-only, then /etc/mtab is
219 * probably not correct; so we won't issue a warning based on
220 * it.
221 */
222 fd = open(MOUNTED, O_RDWR);
223 if (fd < 0) {
224 if (errno == EROFS)
225 return;
226 } else
227 close(fd);
228#endif
229
230 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000231 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000232 return;
233 }
234
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000235 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000236 if (!isatty(0) || !isatty(1))
237 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000238 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000239 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000240 "SEVERE filesystem damage.\007\007\007\n\n"));
241 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000242 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000243 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000244 exit (0);
245 }
246 return;
247}
248
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000249/*
250 * This routine checks to see if a filesystem can be skipped; if so,
251 * it will exit with E2FSCK_OK. Under some conditions it will print a
252 * message explaining why a check is being forced.
253 */
254static void check_if_skip(e2fsck_t ctx)
255{
256 ext2_filsys fs = ctx->fs;
257 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000258 unsigned int reason_arg = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000259
260 if (force || bad_blocks_file || cflag || swapfs)
261 return;
262
263 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000264 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000265 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000266 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000267 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000268 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000269 (unsigned) fs->super->s_max_mnt_count)) {
270 reason = _(" has been mounted %u times without being checked");
271 reason_arg = fs->super->s_mnt_count;
272 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000273 time(0) >= (fs->super->s_lastcheck +
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000274 fs->super->s_checkinterval)) {
275 reason = _(" has gone %u days without being checked");
276 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
277 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000278 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000279 fputs(ctx->device_name, stdout);
280 printf(reason, reason_arg);
281 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000282 return;
283 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000284 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000285 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
286 fs->super->s_inodes_count,
287 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
288 fs->super->s_blocks_count);
289 ext2fs_close(fs);
290 exit(FSCK_OK);
291}
292
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000293/*
294 * For completion notice
295 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000296struct percent_tbl {
297 int max_pass;
298 int table[32];
299};
300struct percent_tbl e2fsck_tbl = {
301 5, { 0, 70, 90, 92, 95, 100 }
302};
Theodore Ts'o5596def1999-07-19 15:27:37 +0000303static char bar[] =
304 "==============================================================="
305 "===============================================================";
306static char spaces[] =
307 " "
308 " ";
309
310static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
311 int max)
312{
313 float percent;
314
315 if (pass <= 0)
316 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000317 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000318 return 100.0;
319 percent = ((float) curr) / ((float) max);
320 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
321 + tbl->table[pass-1]);
322}
323
324extern void e2fsck_clear_progbar(e2fsck_t ctx)
325{
326 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
327 return;
328
329 printf("%s\r", spaces + (sizeof(spaces) - 80));
330 ctx->flags &= ~E2F_FLAG_PROG_BAR;
331}
332
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000333static int e2fsck_update_progress(e2fsck_t ctx, int pass,
334 unsigned long cur, unsigned long max)
335{
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000336 static const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000337 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000338 int i;
339 float percent;
Theodore Ts'o06012322000-02-12 20:12:43 +0000340 int tick;
341 struct timeval tv;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000342 static int dpywidth = 0;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000343
344 if (pass == 0)
345 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000346
347 if (ctx->progress_fd) {
348 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
349 write(ctx->progress_fd, buf, strlen(buf));
350 } else {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000351 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
352 return 0;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000353 if (dpywidth == 0) {
354 dpywidth = 66 - strlen(ctx->device_name);
355 dpywidth = 8 * (dpywidth / 8);
356 }
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000357 /*
358 * Calculate the new progress position. If the
359 * percentage hasn't changed, then we skip out right
360 * away.
361 */
362 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
363 if (ctx->progress_last_percent == (int) 10 * percent)
364 return 0;
365 ctx->progress_last_percent = (int) 10 * percent;
366
367 /*
368 * If we've already updated the spinner once within
369 * the last 1/8th of a second, no point doing it
370 * again.
371 */
Theodore Ts'o06012322000-02-12 20:12:43 +0000372 gettimeofday(&tv, NULL);
373 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
374 if ((tick == ctx->progress_last_time) &&
375 (cur != max) && (cur != 0))
376 return 0;
377 ctx->progress_last_time = tick;
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000378
379 /*
380 * Advance the spinner, and note that the progress bar
381 * will be on the screen
382 */
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000383 ctx->progress_pos = (ctx->progress_pos+1) & 3;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000384 ctx->flags |= E2F_FLAG_PROG_BAR;
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000385
Theodore Ts'o5596def1999-07-19 15:27:37 +0000386 i = ((percent * dpywidth) + 50) / 100;
387 printf("%s: |%s%s", ctx->device_name,
388 bar + (sizeof(bar) - (i+1)),
389 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
390 if (percent == 100.0)
391 fputc('|', stdout);
392 else
393 fputc(spinner[ctx->progress_pos & 3], stdout);
394 printf(" %4.1f%% \r", percent);
395 if (percent == 100.0)
396 e2fsck_clear_progbar(ctx);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000397 fflush(stdout);
398 }
399 return 0;
400}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000401
402#define PATH_SET "PATH=/sbin"
403
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000404static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000405{
406 int fd;
407
408 while (1) {
409 fd = open("/dev/null", O_RDWR);
410 if (fd > 2)
411 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000412 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000413 fprintf(stderr, _("ERROR: Couldn't open "
414 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000415 strerror(errno));
416 break;
417 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000418 }
419 close(fd);
420}
421
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000422#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000423static void signal_progress_on(int sig)
424{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000425 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000426
427 if (!ctx)
428 return;
429
430 ctx->progress = e2fsck_update_progress;
431 ctx->progress_fd = 0;
432}
433
434static void signal_progress_off(int sig)
435{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000436 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000437
438 if (!ctx)
439 return;
440
441 e2fsck_clear_progbar(ctx);
442 ctx->progress = 0;
443}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000444#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000445
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000446static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
447{
448 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000449 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000450#ifdef MTRACE
451 extern void *mallwatch;
452#endif
453 char *oldpath = getenv("PATH");
454 e2fsck_t ctx;
455 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000456#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000457 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000458#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000459
460 retval = e2fsck_allocate_context(&ctx);
461 if (retval)
462 return retval;
463
464 *ret_ctx = ctx;
465
466 /* Update our PATH to include /sbin */
467 if (oldpath) {
468 char *newpath;
469
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000470 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
471 strlen (oldpath));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000472 if (!newpath)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000473 fatal_error(ctx, "Couldn't malloc() newpath");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000474 strcpy (newpath, PATH_SET);
475 strcat (newpath, ":");
476 strcat (newpath, oldpath);
477 putenv (newpath);
478 } else
479 putenv (PATH_SET);
480
481 setbuf(stdout, NULL);
482 setbuf(stderr, NULL);
483 initialize_ext2_error_table();
484
485 if (argc && *argv)
486 ctx->program_name = *argv;
487 else
488 ctx->program_name = "e2fsck";
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400489 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:j:P:l:L:N:Ss")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000490 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000491 case 'C':
492 ctx->progress = e2fsck_update_progress;
493 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000494 if (!ctx->progress_fd)
495 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000496 /* Validate the file descriptor to avoid disasters */
497 fd = dup(ctx->progress_fd);
498 if (fd < 0) {
499 fprintf(stderr,
500 _("Error validating file descriptor %d: %s\n"),
501 ctx->progress_fd,
502 error_message(errno));
503 fatal_error(ctx,
504 _("Invalid completion information file descriptor"));
505 } else
506 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000507 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000508 case 'p':
509 case 'a':
510 ctx->options |= E2F_OPT_PREEN;
511 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
512 break;
513 case 'n':
514 ctx->options |= E2F_OPT_NO;
515 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
516 break;
517 case 'y':
518 ctx->options |= E2F_OPT_YES;
519 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
520 break;
521 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000522#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000523 if (ctx->options & E2F_OPT_TIME)
524 ctx->options |= E2F_OPT_TIME2;
525 else
526 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000527#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000528 fprintf(stderr, _("The -t option is not "
529 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000530#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000531 break;
532 case 'c':
533 cflag++;
534 ctx->options |= E2F_OPT_CHECKBLOCKS;
535 break;
536 case 'r':
537 /* What we do by default, anyway! */
538 break;
539 case 'b':
540 ctx->use_superblock = atoi(optarg);
541 break;
542 case 'B':
543 blocksize = atoi(optarg);
544 break;
545 case 'I':
546 ctx->inode_buffer_blocks = atoi(optarg);
547 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400548 case 'j':
549 ctx->journal_name = optarg;
550 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551 case 'P':
552 ctx->process_inode_size = atoi(optarg);
553 break;
554 case 'L':
555 replace_bad_blocks++;
556 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000557 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000558 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000559 fatal_error(ctx,
560 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000561 strcpy(bad_blocks_file, optarg);
562 break;
563 case 'd':
564 ctx->options |= E2F_OPT_DEBUG;
565 break;
566 case 'f':
567 force = 1;
568 break;
569 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000570 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000571 break;
572 case 'v':
573 verbose = 1;
574 break;
575 case 'V':
576 show_version_only = 1;
577 break;
578#ifdef MTRACE
579 case 'M':
580 mallwatch = (void *) strtol(optarg, NULL, 0);
581 break;
582#endif
583 case 'N':
584 ctx->device_name = optarg;
585 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000586#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000587 case 's':
588 normalize_swapfs = 1;
589 case 'S':
590 swapfs = 1;
591 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000592#else
593 case 's':
594 case 'S':
595 fprintf(stderr, _("Byte-swapping filesystems "
596 "not compiled in this version "
597 "of e2fsck\n"));
598 exit(1);
599#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000600 default:
601 usage(ctx);
602 }
603 if (show_version_only)
604 return 0;
605 if (optind != argc - 1)
606 usage(ctx);
607 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
608 !cflag && !swapfs)
609 ctx->options |= E2F_OPT_READONLY;
610 ctx->filesystem_name = argv[optind];
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000611 if (flush) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
613
614 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000615 com_err("open", errno,
616 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000618 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000620 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000621 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000622 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000623 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000624 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 }
626 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000627 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000628#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 if (swapfs) {
630 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000631 fprintf(stderr, _("Incompatible options not "
632 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000633 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000634 }
635 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000636#endif
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000637#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000638 /*
639 * Set up signal action
640 */
641 memset(&sa, 0, sizeof(struct sigaction));
642#ifdef SA_RESTART
643 sa.sa_flags = SA_RESTART;
644#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000645 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000646 sa.sa_handler = signal_progress_on;
647 sigaction(SIGUSR1, &sa, 0);
648 sa.sa_handler = signal_progress_off;
649 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000650#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000651 return 0;
652}
653
654static const char *my_ver_string = E2FSPROGS_VERSION;
655static const char *my_ver_date = E2FSPROGS_DATE;
656
657int main (int argc, char *argv[])
658{
659 errcode_t retval = 0;
660 int exit_value = FSCK_OK;
661 int i;
662 ext2_filsys fs = 0;
663 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000664 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665 const char *lib_ver_date;
666 int my_ver, lib_ver;
667 e2fsck_t ctx;
668 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000669 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670
671 clear_problem_context(&pctx);
672#ifdef MTRACE
673 mtrace();
674#endif
675#ifdef MCHECK
676 mcheck(0);
677#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000678#ifdef ENABLE_NLS
679 setlocale(LC_MESSAGES, "");
680 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
681 textdomain(NLS_CAT_NAME);
682#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000683 my_ver = ext2fs_parse_version_string(my_ver_string);
684 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
685 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000686 fprintf( stderr, _("Error: ext2fs library version "
687 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000688 show_version_only++;
689 }
690
691 retval = PRS(argc, argv, &ctx);
692 if (retval) {
693 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000694 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000695 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000696 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000697 reserve_stdio_fds();
698
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000699#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000701#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000702
703 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000704 fprintf (stderr, _("e2fsck %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000705 my_ver_string, my_ver_date, EXT2FS_VERSION,
706 EXT2FS_DATE);
707
708 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000709 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000710 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000711 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000712 }
713
714 check_mount(ctx);
715
716 if (!(ctx->options & E2F_OPT_PREEN) &&
717 !(ctx->options & E2F_OPT_NO) &&
718 !(ctx->options & E2F_OPT_YES)) {
719 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000720 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000721 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000722 }
723 ctx->superblock = ctx->use_superblock;
724restart:
725#if 1
726 io_ptr = unix_io_manager;
727#else
728 io_ptr = test_io_manager;
729 test_io_backing_manager = unix_io_manager;
730#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000731 flags = 0;
732 if ((ctx->options & E2F_OPT_READONLY) == 0)
733 flags |= EXT2_FLAG_RW;
734
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735 if (ctx->superblock && blocksize) {
736 retval = ext2fs_open(ctx->filesystem_name, flags,
737 ctx->superblock, blocksize, io_ptr, &fs);
738 } else if (ctx->superblock) {
739 for (i=0; possible_block_sizes[i]; i++) {
740 retval = ext2fs_open(ctx->filesystem_name, flags,
741 ctx->superblock,
742 possible_block_sizes[i],
743 io_ptr, &fs);
744 if (!retval)
745 break;
746 }
747 } else
748 retval = ext2fs_open(ctx->filesystem_name, flags,
749 0, 0, io_ptr, &fs);
750 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
751 ((retval == EXT2_ET_BAD_MAGIC) ||
752 ((retval == 0) && ext2fs_check_desc(fs)))) {
753 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000754 printf(_("%s trying backup blocks...\n"),
755 retval ? _("Couldn't find ext2 superblock,") :
756 _("Group descriptors look bad..."));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000757 ctx->superblock = get_backup_sb(fs);
758 if (fs)
759 ext2fs_close(fs);
760 goto restart;
761 }
762 }
763 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000764 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000765 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000766 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000767 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000768 "too high for this version of e2fsck.\n"
769 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000770 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000771 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
772 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000773 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000774 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000775 printf(_("You must have %s access to the "
776 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000777 (ctx->options & E2F_OPT_READONLY) ?
778 "r/o" : "r/w");
779 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000780 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000781#ifdef EROFS
782 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000783 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000784 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000785 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000786#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000787 else
788 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000789 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000790 }
791 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000792 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000793 sb = fs->super;
794 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000795 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000796 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000797 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000798 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000799 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000800 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000801
Theodore Ts'o17390c02000-07-07 04:13:21 +0000802 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000803 * Set the device name, which is used whenever we print error
804 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000805 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000806 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000807 (sb->s_volume_name[0] != 0)) {
808 char *cp = malloc(sizeof(sb->s_volume_name)+1);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000809 if (cp) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000810 strncpy(cp, sb->s_volume_name,
811 sizeof(sb->s_volume_name));
812 cp[sizeof(sb->s_volume_name)] = 0;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000813 ctx->device_name = cp;
814 }
815 }
816 if (ctx->device_name == 0)
817 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000818
819 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000820 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000821 */
822 retval = e2fsck_check_ext3_journal(ctx);
823 if (retval) {
824 com_err(ctx->program_name, retval,
825 _("while checking ext3 journal for %s"),
826 ctx->device_name);
827 ext2fs_close(ctx->fs);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000828 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000829 }
830
Theodore Ts'o9b565752000-12-13 18:50:22 +0000831 /*
832 * Check to see if we need to do ext3-style recovery. If so,
833 * do it, and then restart the fsck.
834 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000835 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000836 if (ctx->options & E2F_OPT_READONLY) {
837 printf(_("Warning: skipping journal recovery "
838 "because doing a read-only filesystem "
839 "check.\n"));
840 io_channel_flush(ctx->fs->io);
841 } else {
842 retval = e2fsck_run_ext3_journal(ctx);
843 if (retval) {
844 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000845 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000846 ctx->device_name);
847 fatal_error(ctx, 0);
848 }
849 ext2fs_close(ctx->fs);
850 ctx->fs = 0;
851 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000852 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000853 }
854
855 /*
856 * Check for compatibility with the feature sets. We need to
857 * be more stringent than ext2fs_open().
858 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000859 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
860 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000861 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
862 "(%s)", ctx->device_name);
863 goto get_newer;
864 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000865 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000866 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
867 "(%s)", ctx->device_name);
868 goto get_newer;
869 }
870#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000871 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000872 com_err(ctx->program_name, 0,
873 _("Warning: compression support is experimental.\n"));
874#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000875
876 /*
877 * If the user specified a specific superblock, presumably the
878 * master superblock has been trashed. So we mark the
879 * superblock as dirty, so it can be written out.
880 */
881 if (ctx->superblock &&
882 !(ctx->options & E2F_OPT_READONLY))
883 ext2fs_mark_super_dirty(fs);
884
885 /*
886 * Don't overwrite the backup superblock and block
887 * descriptors, until we're sure the filesystem is OK....
888 */
889 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
890
891 ehandler_init(fs->io);
892
893 if (ctx->superblock)
894 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
895 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000896 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000897 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000898 check_if_skip(ctx);
899 if (bad_blocks_file)
900 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
901 else if (cflag)
902 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000903 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000904 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000905#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000906 if (normalize_swapfs) {
907 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
908 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000909 fprintf(stderr, _("%s: Filesystem byte order "
910 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000911 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000912 }
913 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000914 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000915 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000916 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000917 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000918 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000919#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000920
921 /*
922 * Mark the system as valid, 'til proven otherwise
923 */
924 ext2fs_mark_valid(fs);
925
926 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
927 if (retval) {
928 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000929 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000930 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000931 printf(_("This doesn't bode well,"
932 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000933 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000934
935 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000936 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000937 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000938 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000939 retval = e2fsck_reset_context(ctx);
940 if (retval) {
941 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000942 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000943 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000944 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000945 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000946 goto restart;
947 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000948 if (run_result & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000949 fatal_error(ctx, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000950 if (run_result & E2F_FLAG_CANCEL)
951 ext2fs_unmark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000952
953#ifdef MTRACE
954 mtrace_print("Cleanup");
955#endif
956 if (ext2fs_test_changed(fs)) {
957 exit_value = FSCK_NONDESTRUCT;
958 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000959 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000960 ctx->device_name);
961 if (root_filesystem && !read_only_root) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000962 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000963 ctx->device_name);
964 exit_value = FSCK_REBOOT;
965 }
966 }
967 if (ext2fs_test_valid(fs))
968 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'od3124012001-07-20 14:13:49 -0400969 else {
970 printf(_("\n%s: ********** WARNING: Filesystem still has "
971 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000972 exit_value = FSCK_UNCORRECTED;
Theodore Ts'od3124012001-07-20 14:13:49 -0400973 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000974 if (!(ctx->options & E2F_OPT_READONLY)) {
975 if (ext2fs_test_valid(fs)) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000976 if (!(sb->s_state & EXT2_VALID_FS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000977 exit_value = FSCK_NONDESTRUCT;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000978 sb->s_state = EXT2_VALID_FS;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000979 } else
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000980 sb->s_state &= ~EXT2_VALID_FS;
981 sb->s_mnt_count = 0;
982 sb->s_lastcheck = time(NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000983 ext2fs_mark_super_dirty(fs);
984 }
985 show_stats(ctx);
986
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000987 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000988
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000989#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000990 if (ctx->options & E2F_OPT_TIME)
991 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000992#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000993
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000994 ext2fs_close(fs);
Theodore Ts'o7142db01999-11-08 18:46:54 +0000995 ctx->fs = NULL;
996 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000997
998 return exit_value;
999}