blob: e3ff40407f3b0a891482b02a97a4f65ca180bae0 [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 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000045static int swapfs = 0;
46static int normalize_swapfs = 0;
47static int cflag = 0; /* check disk */
48static int show_version_only = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000049static int verbose = 0;
50
51static int replace_bad_blocks = 0;
52static char *bad_blocks_file = 0;
53
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054static int root_filesystem = 0;
55static int read_only_root = 0;
56
Theodore Ts'o243dc312000-08-22 21:37:47 +000057e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
58
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000059static void usage(e2fsck_t ctx)
60{
61 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050062 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000063 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040064 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
65 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000066 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000067
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000068 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000069 " -p Automatic repair (no questions)\n"
70 " -n Make no changes to the filesystem\n"
71 " -y Assume \"yes\" to all questions\n"
72 " -c Check for bad blocks\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000073 " -f Force checking even if filesystem is marked clean\n"));
74 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000075 " -v Be verbose\n"
76 " -b superblock Use alternative superblock\n"
77 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040078 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000079 " -l bad_blocks_file Add to badblocks list\n"
80 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000081 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000082
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000083 exit(FSCK_USAGE);
84}
85
86static void show_stats(e2fsck_t ctx)
87{
88 ext2_filsys fs = ctx->fs;
89 int inodes, inodes_used, blocks, blocks_used;
90 int dir_links;
91 int num_files, num_links;
92 int frag_percent;
93
94 dir_links = 2 * ctx->fs_directory_count - 1;
95 num_files = ctx->fs_total_count - dir_links;
96 num_links = ctx->fs_links_count - dir_links;
97 inodes = fs->super->s_inodes_count;
98 inodes_used = (fs->super->s_inodes_count -
99 fs->super->s_free_inodes_count);
100 blocks = fs->super->s_blocks_count;
101 blocks_used = (fs->super->s_blocks_count -
102 fs->super->s_free_blocks_count);
103
104 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
105 frag_percent = (frag_percent + 5) / 10;
106
107 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000108 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000109 ctx->device_name, inodes_used, inodes,
110 frag_percent / 10, frag_percent % 10,
111 blocks_used, blocks);
112 return;
113 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000114 /*
115 * This is a bit ugly. But I think there will nearly always be more
116 * than one "thing" to report about, so I won't try writing complex
117 * code to handle one/two/many forms of all words.
118 * Some languages (Italian, at least) never uses the plural form
119 * of foreign words, so in real life this could not be a problem.
120 * md@linux.it - 2000-1-15
121 */
122#ifdef ENABLE_NLS
123 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400124 100 * inodes_used / inodes);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000125 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
126 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
127 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
128 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
129 printf (_("%8d blocks used (%d%%)\n"
130 "%8d bad blocks\n"), blocks_used,
Theodore Ts'o636a9542001-06-29 17:57:26 -0400131 (int) ((long long) 100 * blocks_used / blocks),
132 ctx->fs_badblocks_count);
Theodore Ts'o2b94c652001-08-09 04:08:52 -0400133 printf(_("%8d large files\n"), ctx->large_files);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000134 printf (_("\n%8d regular files\n"
135 "%8d directories\n"
136 "%8d character device files\n"
137 "%8d block device files\n"
138 "%8d fifos\n"
139 "%8d links\n"
140 "%8d symbolic links (%d fast symbolic links)\n"
141 "%8d sockets\n"
142 "--------\n"
143 "%8d files\n"),
144 ctx->fs_regular_count,
145 ctx->fs_directory_count,
146 ctx->fs_chardev_count,
147 ctx->fs_blockdev_count,
148 ctx->fs_fifo_count,
149 ctx->fs_links_count - dir_links,
150 ctx->fs_symlinks_count,
151 ctx->fs_fast_symlinks_count,
152 ctx->fs_sockets_count,
153 ctx->fs_total_count - dir_links);
154#else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000155 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
156 (inodes_used != 1) ? "s" : "",
157 100 * inodes_used / inodes);
158 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
159 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
160 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
161 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
162 printf ("%8d block%s used (%d%%)\n"
163 "%8d bad block%s\n", blocks_used,
164 (blocks_used != 1) ? "s" : "",
165 100 * blocks_used / blocks, ctx->fs_badblocks_count,
166 ctx->fs_badblocks_count != 1 ? "s" : "");
Theodore Ts'o2b94c652001-08-09 04:08:52 -0400167 printf(_("%8d large file%s\n"), ctx->large_files,
168 (ctx->large_files != 1) ? "s" : "");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000169 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;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500204 int mount_flags, cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000205
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 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400213
214 /*
215 * If the filesystem isn't mounted, or it's the root filesystem
216 * and it's mounted read-only, then everything's fine.
217 */
218 if ((!(mount_flags & EXT2_MF_MOUNTED)) ||
219 ((mount_flags & EXT2_MF_ISROOT) &&
220 (mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000221 return;
222
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000223 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000224 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000225 return;
226 }
227
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000228 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000229 if (!isatty(0) || !isatty(1))
230 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000231 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000232 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000233 "SEVERE filesystem damage.\007\007\007\n\n"));
234 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000235 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000236 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000237 exit (0);
238 }
239 return;
240}
241
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000242/*
243 * This routine checks to see if a filesystem can be skipped; if so,
244 * it will exit with E2FSCK_OK. Under some conditions it will print a
245 * message explaining why a check is being forced.
246 */
247static void check_if_skip(e2fsck_t ctx)
248{
249 ext2_filsys fs = ctx->fs;
250 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000251 unsigned int reason_arg = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000252
Theodore Ts'od37066a2001-12-21 23:28:54 -0500253 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
254 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000255 return;
256
257 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000258 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000259 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000260 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000261 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000262 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000263 (unsigned) fs->super->s_max_mnt_count)) {
264 reason = _(" has been mounted %u times without being checked");
265 reason_arg = fs->super->s_mnt_count;
266 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000267 time(0) >= (fs->super->s_lastcheck +
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000268 fs->super->s_checkinterval)) {
269 reason = _(" has gone %u days without being checked");
270 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
271 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000272 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000273 fputs(ctx->device_name, stdout);
274 printf(reason, reason_arg);
275 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000276 return;
277 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000278 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000279 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
280 fs->super->s_inodes_count,
281 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
282 fs->super->s_blocks_count);
283 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400284 ctx->fs = NULL;
285 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000286 exit(FSCK_OK);
287}
288
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000289/*
290 * For completion notice
291 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000292struct percent_tbl {
293 int max_pass;
294 int table[32];
295};
296struct percent_tbl e2fsck_tbl = {
297 5, { 0, 70, 90, 92, 95, 100 }
298};
Theodore Ts'o5596def1999-07-19 15:27:37 +0000299static char bar[] =
300 "==============================================================="
301 "===============================================================";
302static char spaces[] =
303 " "
304 " ";
305
306static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
307 int max)
308{
309 float percent;
310
311 if (pass <= 0)
312 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000313 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000314 return 100.0;
315 percent = ((float) curr) / ((float) max);
316 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
317 + tbl->table[pass-1]);
318}
319
320extern void e2fsck_clear_progbar(e2fsck_t ctx)
321{
322 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
323 return;
324
325 printf("%s\r", spaces + (sizeof(spaces) - 80));
326 ctx->flags &= ~E2F_FLAG_PROG_BAR;
327}
328
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000329static int e2fsck_update_progress(e2fsck_t ctx, int pass,
330 unsigned long cur, unsigned long max)
331{
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000332 static const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000333 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000334 int i;
335 float percent;
Theodore Ts'o06012322000-02-12 20:12:43 +0000336 int tick;
337 struct timeval tv;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000338 static int dpywidth = 0;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000339
340 if (pass == 0)
341 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000342
343 if (ctx->progress_fd) {
344 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
345 write(ctx->progress_fd, buf, strlen(buf));
346 } else {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000347 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
348 return 0;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000349 if (dpywidth == 0) {
350 dpywidth = 66 - strlen(ctx->device_name);
351 dpywidth = 8 * (dpywidth / 8);
352 }
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000353 /*
354 * Calculate the new progress position. If the
355 * percentage hasn't changed, then we skip out right
356 * away.
357 */
358 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
359 if (ctx->progress_last_percent == (int) 10 * percent)
360 return 0;
361 ctx->progress_last_percent = (int) 10 * percent;
362
363 /*
364 * If we've already updated the spinner once within
365 * the last 1/8th of a second, no point doing it
366 * again.
367 */
Theodore Ts'o06012322000-02-12 20:12:43 +0000368 gettimeofday(&tv, NULL);
369 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
370 if ((tick == ctx->progress_last_time) &&
371 (cur != max) && (cur != 0))
372 return 0;
373 ctx->progress_last_time = tick;
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000374
375 /*
376 * Advance the spinner, and note that the progress bar
377 * will be on the screen
378 */
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000379 ctx->progress_pos = (ctx->progress_pos+1) & 3;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000380 ctx->flags |= E2F_FLAG_PROG_BAR;
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000381
Theodore Ts'o5596def1999-07-19 15:27:37 +0000382 i = ((percent * dpywidth) + 50) / 100;
383 printf("%s: |%s%s", ctx->device_name,
384 bar + (sizeof(bar) - (i+1)),
385 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
386 if (percent == 100.0)
387 fputc('|', stdout);
388 else
389 fputc(spinner[ctx->progress_pos & 3], stdout);
390 printf(" %4.1f%% \r", percent);
391 if (percent == 100.0)
392 e2fsck_clear_progbar(ctx);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000393 fflush(stdout);
394 }
395 return 0;
396}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000397
398#define PATH_SET "PATH=/sbin"
399
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000400static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000401{
402 int fd;
403
404 while (1) {
405 fd = open("/dev/null", O_RDWR);
406 if (fd > 2)
407 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000408 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000409 fprintf(stderr, _("ERROR: Couldn't open "
410 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000411 strerror(errno));
412 break;
413 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000414 }
415 close(fd);
416}
417
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000418#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000419static void signal_progress_on(int sig)
420{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000421 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000422
423 if (!ctx)
424 return;
425
426 ctx->progress = e2fsck_update_progress;
427 ctx->progress_fd = 0;
428}
429
430static void signal_progress_off(int sig)
431{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000432 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000433
434 if (!ctx)
435 return;
436
437 e2fsck_clear_progbar(ctx);
438 ctx->progress = 0;
439}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400440
441static void signal_cancel(int sig)
442{
443 e2fsck_t ctx = e2fsck_global_ctx;
444
445 if (!ctx)
446 exit(FSCK_CANCELED);
447
448 ctx->flags |= E2F_FLAG_CANCEL;
449}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000450#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000451
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400452static void parse_extended_opts(e2fsck_t ctx, const char *opts)
453{
454 char *buf, *token, *next, *p, *arg;
455 int len, ea_ver;
456 int extended_usage = 0;
457
458 len = strlen(opts);
459 buf = malloc(len+1);
460 if (!buf) {
461 fprintf(stderr, _("Couldn't allocate memory to parse "
462 "extended options!\n"));
463 exit(1);
464 }
465 strcpy(buf, opts);
466 for (token = buf; token && *token; token = next) {
467 p = strchr(token, ',');
468 next = 0;
469 if (p) {
470 *p = 0;
471 next = p+1;
472 }
473 arg = strchr(token, '=');
474 if (arg) {
475 *arg = 0;
476 arg++;
477 }
478 if (strcmp(token, "ea_ver") == 0) {
479 if (!arg) {
480 extended_usage++;
481 continue;
482 }
483 ea_ver = strtoul(arg, &p, 0);
484 if (*p ||
485 ((ea_ver != 1) && (ea_ver != 2))) {
486 fprintf(stderr,
487 _("Invalid EA version.\n"));
488 extended_usage++;
489 continue;
490 }
491 ctx->ext_attr_ver = ea_ver;
492 } else
493 extended_usage++;
494 }
495 if (extended_usage) {
496 fprintf(stderr, _("Extended options are separated by commas, "
497 "and may take an argument which\n"
498 "is set off by an equals ('=') sign. "
499 "Valid raid options are:\n"
500 "\tea_ver=<ea_version (1 or 2)\n\n"));
501 exit(1);
502 }
503}
504
505
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000506static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
507{
508 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000509 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510#ifdef MTRACE
511 extern void *mallwatch;
512#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000513 e2fsck_t ctx;
514 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000515#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000516 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000517#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400518 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000519
520 retval = e2fsck_allocate_context(&ctx);
521 if (retval)
522 return retval;
523
524 *ret_ctx = ctx;
525
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000526 setbuf(stdout, NULL);
527 setbuf(stderr, NULL);
528 initialize_ext2_error_table();
529
530 if (argc && *argv)
531 ctx->program_name = *argv;
532 else
533 ctx->program_name = "e2fsck";
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400534 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsD")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000535 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000536 case 'C':
537 ctx->progress = e2fsck_update_progress;
538 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000539 if (!ctx->progress_fd)
540 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000541 /* Validate the file descriptor to avoid disasters */
542 fd = dup(ctx->progress_fd);
543 if (fd < 0) {
544 fprintf(stderr,
545 _("Error validating file descriptor %d: %s\n"),
546 ctx->progress_fd,
547 error_message(errno));
548 fatal_error(ctx,
549 _("Invalid completion information file descriptor"));
550 } else
551 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000552 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400553 case 'D':
554 ctx->options |= E2F_OPT_COMPRESS_DIRS;
555 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400556 case 'E':
557 extended_opts = optarg;
558 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000559 case 'p':
560 case 'a':
561 ctx->options |= E2F_OPT_PREEN;
562 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
563 break;
564 case 'n':
565 ctx->options |= E2F_OPT_NO;
566 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
567 break;
568 case 'y':
569 ctx->options |= E2F_OPT_YES;
570 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
571 break;
572 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000573#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000574 if (ctx->options & E2F_OPT_TIME)
575 ctx->options |= E2F_OPT_TIME2;
576 else
577 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000578#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000579 fprintf(stderr, _("The -t option is not "
580 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000581#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000582 break;
583 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500584 if (cflag++)
585 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000586 ctx->options |= E2F_OPT_CHECKBLOCKS;
587 break;
588 case 'r':
589 /* What we do by default, anyway! */
590 break;
591 case 'b':
592 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400593 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000594 break;
595 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500596 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000597 break;
598 case 'I':
599 ctx->inode_buffer_blocks = atoi(optarg);
600 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400601 case 'j':
602 ctx->journal_name = optarg;
603 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000604 case 'P':
605 ctx->process_inode_size = atoi(optarg);
606 break;
607 case 'L':
608 replace_bad_blocks++;
609 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000610 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000611 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000612 fatal_error(ctx,
613 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000614 strcpy(bad_blocks_file, optarg);
615 break;
616 case 'd':
617 ctx->options |= E2F_OPT_DEBUG;
618 break;
619 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500620 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000621 break;
622 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000623 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 break;
625 case 'v':
626 verbose = 1;
627 break;
628 case 'V':
629 show_version_only = 1;
630 break;
631#ifdef MTRACE
632 case 'M':
633 mallwatch = (void *) strtol(optarg, NULL, 0);
634 break;
635#endif
636 case 'N':
637 ctx->device_name = optarg;
638 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000639#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000640 case 's':
641 normalize_swapfs = 1;
642 case 'S':
643 swapfs = 1;
644 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000645#else
646 case 's':
647 case 'S':
648 fprintf(stderr, _("Byte-swapping filesystems "
649 "not compiled in this version "
650 "of e2fsck\n"));
651 exit(1);
652#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000653 default:
654 usage(ctx);
655 }
656 if (show_version_only)
657 return 0;
658 if (optind != argc - 1)
659 usage(ctx);
660 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400661 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 ctx->options |= E2F_OPT_READONLY;
663 ctx->filesystem_name = argv[optind];
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400664 if (extended_opts)
665 parse_extended_opts(ctx, extended_opts);
666
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000667 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500668 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000669 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000670 com_err("open", errno,
671 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000672 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000673 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000674 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000675 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000676 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000677 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000678 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000679 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000680 }
681 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000682 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000683#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684 if (swapfs) {
685 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000686 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500687 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000688 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000689 }
690 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000691#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500692 if (cflag && bad_blocks_file) {
693 fprintf(stderr, _("The -c and the -l/-L options may "
694 "not be both used at the same time.\n"));
695 exit(FSCK_USAGE);
696 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000697#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000698 /*
699 * Set up signal action
700 */
701 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400702 sa.sa_handler = signal_cancel;
703 sigaction(SIGINT, &sa, 0);
704 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000705#ifdef SA_RESTART
706 sa.sa_flags = SA_RESTART;
707#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000708 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000709 sa.sa_handler = signal_progress_on;
710 sigaction(SIGUSR1, &sa, 0);
711 sa.sa_handler = signal_progress_off;
712 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000713#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400714
715 /* Update our PATH to include /sbin if we need to run badblocks */
716 if (cflag) {
717 char *oldpath = getenv("PATH");
718 if (oldpath) {
719 char *newpath;
720
721 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
722 strlen (oldpath));
723 if (!newpath)
724 fatal_error(ctx, "Couldn't malloc() newpath");
725 strcpy (newpath, PATH_SET);
726 strcat (newpath, ":");
727 strcat (newpath, oldpath);
728 putenv (newpath);
729 } else
730 putenv (PATH_SET);
731 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000732 return 0;
733}
734
735static const char *my_ver_string = E2FSPROGS_VERSION;
736static const char *my_ver_date = E2FSPROGS_DATE;
737
738int main (int argc, char *argv[])
739{
740 errcode_t retval = 0;
741 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000742 ext2_filsys fs = 0;
743 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000744 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000745 const char *lib_ver_date;
746 int my_ver, lib_ver;
747 e2fsck_t ctx;
748 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000749 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000750
751 clear_problem_context(&pctx);
752#ifdef MTRACE
753 mtrace();
754#endif
755#ifdef MCHECK
756 mcheck(0);
757#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000758#ifdef ENABLE_NLS
759 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500760 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000761 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
762 textdomain(NLS_CAT_NAME);
763#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000764 my_ver = ext2fs_parse_version_string(my_ver_string);
765 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
766 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000767 fprintf( stderr, _("Error: ext2fs library version "
768 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000769 show_version_only++;
770 }
771
772 retval = PRS(argc, argv, &ctx);
773 if (retval) {
774 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000775 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000776 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000777 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000778 reserve_stdio_fds();
779
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000780#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000781 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000782#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000783
784 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400785 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string,
786 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000787
788 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000789 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000790 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000791 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000792 }
793
794 check_mount(ctx);
795
796 if (!(ctx->options & E2F_OPT_PREEN) &&
797 !(ctx->options & E2F_OPT_NO) &&
798 !(ctx->options & E2F_OPT_YES)) {
799 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000800 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000801 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000802 }
803 ctx->superblock = ctx->use_superblock;
804restart:
805#if 1
806 io_ptr = unix_io_manager;
807#else
808 io_ptr = test_io_manager;
809 test_io_backing_manager = unix_io_manager;
810#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000811 flags = 0;
812 if ((ctx->options & E2F_OPT_READONLY) == 0)
813 flags |= EXT2_FLAG_RW;
814
Theodore Ts'of1a17612001-12-23 22:27:52 -0500815 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000816 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500817 ctx->superblock, ctx->blocksize,
818 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000819 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600820 int blocksize;
821 for (blocksize = EXT2_MIN_BLOCK_SIZE;
822 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000823 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600824 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000825 io_ptr, &fs);
826 if (!retval)
827 break;
828 }
829 } else
830 retval = ext2fs_open(ctx->filesystem_name, flags,
831 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400832 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
833 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000834 ((retval == EXT2_ET_BAD_MAGIC) ||
835 ((retval == 0) && ext2fs_check_desc(fs)))) {
836 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000837 printf(_("%s trying backup blocks...\n"),
838 retval ? _("Couldn't find ext2 superblock,") :
839 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500840 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000841 if (fs)
842 ext2fs_close(fs);
843 goto restart;
844 }
845 }
846 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000847 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000848 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000849 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000850 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000851 "too high for this version of e2fsck.\n"
852 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000853 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000854 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
855 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000856 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000857 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000858 printf(_("You must have %s access to the "
859 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000860 (ctx->options & E2F_OPT_READONLY) ?
861 "r/o" : "r/w");
862 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000863 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000864#ifdef EROFS
865 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000866 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000867 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000868 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000869#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000870 else
871 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000872 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000873 }
874 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000875 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000876 sb = fs->super;
877 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000878 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000879 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000880 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000881 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000882 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000883 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000884
Theodore Ts'o17390c02000-07-07 04:13:21 +0000885 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000886 * Set the device name, which is used whenever we print error
887 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000888 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000889 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000890 (sb->s_volume_name[0] != 0)) {
891 char *cp = malloc(sizeof(sb->s_volume_name)+1);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000892 if (cp) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000893 strncpy(cp, sb->s_volume_name,
894 sizeof(sb->s_volume_name));
895 cp[sizeof(sb->s_volume_name)] = 0;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000896 ctx->device_name = cp;
897 }
898 }
899 if (ctx->device_name == 0)
900 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000901
902 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000903 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000904 */
905 retval = e2fsck_check_ext3_journal(ctx);
906 if (retval) {
907 com_err(ctx->program_name, retval,
908 _("while checking ext3 journal for %s"),
909 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000910 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000911 }
912
Theodore Ts'o9b565752000-12-13 18:50:22 +0000913 /*
914 * Check to see if we need to do ext3-style recovery. If so,
915 * do it, and then restart the fsck.
916 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000917 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000918 if (ctx->options & E2F_OPT_READONLY) {
919 printf(_("Warning: skipping journal recovery "
920 "because doing a read-only filesystem "
921 "check.\n"));
922 io_channel_flush(ctx->fs->io);
923 } else {
924 retval = e2fsck_run_ext3_journal(ctx);
925 if (retval) {
926 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000927 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000928 ctx->device_name);
929 fatal_error(ctx, 0);
930 }
931 ext2fs_close(ctx->fs);
932 ctx->fs = 0;
933 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000934 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000935 }
936
937 /*
938 * Check for compatibility with the feature sets. We need to
939 * be more stringent than ext2fs_open().
940 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000941 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
942 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000943 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
944 "(%s)", ctx->device_name);
945 goto get_newer;
946 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000947 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000948 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
949 "(%s)", ctx->device_name);
950 goto get_newer;
951 }
952#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000953 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000954 com_err(ctx->program_name, 0,
955 _("Warning: compression support is experimental.\n"));
956#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400957#ifndef ENABLE_HTREE
958 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
959 com_err(ctx->program_name, 0,
960 _("E2fsck not compiled with HTREE support,\n\t"
961 "but filesystem %s has HTREE directories.\n"),
962 ctx->device_name);
963 goto get_newer;
964 }
965#endif
966
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000967 /*
968 * If the user specified a specific superblock, presumably the
969 * master superblock has been trashed. So we mark the
970 * superblock as dirty, so it can be written out.
971 */
972 if (ctx->superblock &&
973 !(ctx->options & E2F_OPT_READONLY))
974 ext2fs_mark_super_dirty(fs);
975
976 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400977 * We only update the master superblock because (a) paranoia;
978 * we don't want to corrupt the backup superblocks, and (b) we
979 * don't need to update the mount count and last checked
980 * fields in the backup superblock (the kernel doesn't
981 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000982 */
983 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
984
985 ehandler_init(fs->io);
986
987 if (ctx->superblock)
988 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
989 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000990 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000991 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000992 check_if_skip(ctx);
993 if (bad_blocks_file)
994 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
995 else if (cflag)
996 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000997 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000998 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000999#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001000 if (normalize_swapfs) {
1001 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1002 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001003 fprintf(stderr, _("%s: Filesystem byte order "
1004 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001005 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001006 }
1007 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001008 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001009 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001010 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001011 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001012 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001013#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001014
1015 /*
1016 * Mark the system as valid, 'til proven otherwise
1017 */
1018 ext2fs_mark_valid(fs);
1019
1020 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1021 if (retval) {
1022 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001023 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001024 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001025 printf(_("This doesn't bode well,"
1026 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001027 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001028
1029 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001030 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001031 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001032 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001033 retval = e2fsck_reset_context(ctx);
1034 if (retval) {
1035 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001036 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001037 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001038 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001039 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001040 goto restart;
1041 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001042 if (run_result & E2F_FLAG_CANCEL) {
1043 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1044 ctx->device_name : ctx->filesystem_name);
1045 exit_value |= FSCK_CANCELED;
1046 }
1047 if (run_result & E2F_FLAG_ABORT)
1048 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001049
1050#ifdef MTRACE
1051 mtrace_print("Cleanup");
1052#endif
1053 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001054 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001055 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001056 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001057 ctx->device_name);
1058 if (root_filesystem && !read_only_root) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001059 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001060 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001061 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001062 }
1063 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001064 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001065 printf(_("\n%s: ********** WARNING: Filesystem still has "
1066 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001067 exit_value |= FSCK_UNCORRECTED;
1068 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001069 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001070 if (!(ctx->options & E2F_OPT_READONLY)) {
1071 if (ext2fs_test_valid(fs)) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001072 if (!(sb->s_state & EXT2_VALID_FS))
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001073 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001074 sb->s_state = EXT2_VALID_FS;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001075 } else
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001076 sb->s_state &= ~EXT2_VALID_FS;
1077 sb->s_mnt_count = 0;
1078 sb->s_lastcheck = time(NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001079 ext2fs_mark_super_dirty(fs);
1080 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001081 if (exit_value & FSCK_CANCELED)
1082 exit_value &= ~FSCK_NONDESTRUCT;
1083 else
1084 show_stats(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001085
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001086 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001087
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001088 ext2fs_close(fs);
1089 ctx->fs = NULL;
1090 e2fsck_free_context(ctx);
1091
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001092#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001093 if (ctx->options & E2F_OPT_TIME)
1094 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001095#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001096
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001097 return exit_value;
1098}