blob: bee663664c0a481b1d64c5cd44f9ff1e37fe23d8 [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'o243dc312000-08-22 21:37:47 +000054e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
55
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000056static void usage(e2fsck_t ctx)
57{
58 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050059 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040061 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
62 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000063 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000064
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000065 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000066 " -p Automatic repair (no questions)\n"
67 " -n Make no changes to the filesystem\n"
68 " -y Assume \"yes\" to all questions\n"
69 " -c Check for bad blocks\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000070 " -f Force checking even if filesystem is marked clean\n"));
71 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000072 " -v Be verbose\n"
73 " -b superblock Use alternative superblock\n"
74 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040075 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000076 " -l bad_blocks_file Add to badblocks list\n"
77 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000078 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000079
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000080 exit(FSCK_USAGE);
81}
82
83static void show_stats(e2fsck_t ctx)
84{
85 ext2_filsys fs = ctx->fs;
86 int inodes, inodes_used, blocks, blocks_used;
87 int dir_links;
88 int num_files, num_links;
89 int frag_percent;
90
91 dir_links = 2 * ctx->fs_directory_count - 1;
92 num_files = ctx->fs_total_count - dir_links;
93 num_links = ctx->fs_links_count - dir_links;
94 inodes = fs->super->s_inodes_count;
95 inodes_used = (fs->super->s_inodes_count -
96 fs->super->s_free_inodes_count);
97 blocks = fs->super->s_blocks_count;
98 blocks_used = (fs->super->s_blocks_count -
99 fs->super->s_free_blocks_count);
100
101 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
102 frag_percent = (frag_percent + 5) / 10;
103
104 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000105 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000106 ctx->device_name, inodes_used, inodes,
107 frag_percent / 10, frag_percent % 10,
108 blocks_used, blocks);
109 return;
110 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000111 /*
112 * This is a bit ugly. But I think there will nearly always be more
113 * than one "thing" to report about, so I won't try writing complex
114 * code to handle one/two/many forms of all words.
115 * Some languages (Italian, at least) never uses the plural form
116 * of foreign words, so in real life this could not be a problem.
117 * md@linux.it - 2000-1-15
118 */
119#ifdef ENABLE_NLS
120 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400121 100 * inodes_used / inodes);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000122 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
123 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
124 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
125 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
126 printf (_("%8d blocks used (%d%%)\n"
127 "%8d bad blocks\n"), blocks_used,
Theodore Ts'o636a9542001-06-29 17:57:26 -0400128 (int) ((long long) 100 * blocks_used / blocks),
129 ctx->fs_badblocks_count);
Theodore Ts'o2b94c652001-08-09 04:08:52 -0400130 printf(_("%8d large files\n"), ctx->large_files);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000131 printf (_("\n%8d regular files\n"
132 "%8d directories\n"
133 "%8d character device files\n"
134 "%8d block device files\n"
135 "%8d fifos\n"
136 "%8d links\n"
137 "%8d symbolic links (%d fast symbolic links)\n"
138 "%8d sockets\n"
139 "--------\n"
140 "%8d files\n"),
141 ctx->fs_regular_count,
142 ctx->fs_directory_count,
143 ctx->fs_chardev_count,
144 ctx->fs_blockdev_count,
145 ctx->fs_fifo_count,
146 ctx->fs_links_count - dir_links,
147 ctx->fs_symlinks_count,
148 ctx->fs_fast_symlinks_count,
149 ctx->fs_sockets_count,
150 ctx->fs_total_count - dir_links);
151#else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000152 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
153 (inodes_used != 1) ? "s" : "",
154 100 * inodes_used / inodes);
155 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
156 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
157 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
158 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
159 printf ("%8d block%s used (%d%%)\n"
160 "%8d bad block%s\n", blocks_used,
161 (blocks_used != 1) ? "s" : "",
162 100 * blocks_used / blocks, ctx->fs_badblocks_count,
163 ctx->fs_badblocks_count != 1 ? "s" : "");
Theodore Ts'o2b94c652001-08-09 04:08:52 -0400164 printf(_("%8d large file%s\n"), ctx->large_files,
165 (ctx->large_files != 1) ? "s" : "");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000166 printf ("\n%8d regular file%s\n"
167 "%8d director%s\n"
168 "%8d character device file%s\n"
169 "%8d block device file%s\n"
170 "%8d fifo%s\n"
171 "%8d link%s\n"
172 "%8d symbolic link%s (%d fast symbolic link%s)\n"
173 "%8d socket%s\n"
174 "--------\n"
175 "%8d file%s\n",
176 ctx->fs_regular_count,
177 (ctx->fs_regular_count != 1) ? "s" : "",
178 ctx->fs_directory_count,
179 (ctx->fs_directory_count != 1) ? "ies" : "y",
180 ctx->fs_chardev_count,
181 (ctx->fs_chardev_count != 1) ? "s" : "",
182 ctx->fs_blockdev_count,
183 (ctx->fs_blockdev_count != 1) ? "s" : "",
184 ctx->fs_fifo_count,
185 (ctx->fs_fifo_count != 1) ? "s" : "",
186 ctx->fs_links_count - dir_links,
187 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
188 ctx->fs_symlinks_count,
189 (ctx->fs_symlinks_count != 1) ? "s" : "",
190 ctx->fs_fast_symlinks_count,
191 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
192 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
193 ctx->fs_total_count - dir_links,
194 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000195#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000196}
197
198static void check_mount(e2fsck_t ctx)
199{
200 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500201 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202
Theodore Ts'oee895132002-11-07 16:16:55 -0500203 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
204 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000205 if (retval) {
206 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000207 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000208 ctx->filesystem_name);
209 return;
210 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400211
212 /*
213 * If the filesystem isn't mounted, or it's the root filesystem
214 * and it's mounted read-only, then everything's fine.
215 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500216 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
217 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
218 (ctx->mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000219 return;
220
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000221 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000222 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000223 return;
224 }
225
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000226 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000227 if (!isatty(0) || !isatty(1))
228 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000229 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000230 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000231 "SEVERE filesystem damage.\007\007\007\n\n"));
232 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000233 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000234 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000235 exit (0);
236 }
237 return;
238}
239
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000240/*
241 * This routine checks to see if a filesystem can be skipped; if so,
242 * it will exit with E2FSCK_OK. Under some conditions it will print a
243 * message explaining why a check is being forced.
244 */
245static void check_if_skip(e2fsck_t ctx)
246{
247 ext2_filsys fs = ctx->fs;
248 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000249 unsigned int reason_arg = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000250
Theodore Ts'od37066a2001-12-21 23:28:54 -0500251 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
252 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000253 return;
254
255 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000256 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000257 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000258 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000259 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000260 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000261 (unsigned) fs->super->s_max_mnt_count)) {
262 reason = _(" has been mounted %u times without being checked");
263 reason_arg = fs->super->s_mnt_count;
264 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000265 time(0) >= (fs->super->s_lastcheck +
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000266 fs->super->s_checkinterval)) {
267 reason = _(" has gone %u days without being checked");
268 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
269 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000270 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000271 fputs(ctx->device_name, stdout);
272 printf(reason, reason_arg);
273 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000274 return;
275 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000276 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000277 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
278 fs->super->s_inodes_count,
279 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
280 fs->super->s_blocks_count);
281 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400282 ctx->fs = NULL;
283 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000284 exit(FSCK_OK);
285}
286
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000287/*
288 * For completion notice
289 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000290struct percent_tbl {
291 int max_pass;
292 int table[32];
293};
294struct percent_tbl e2fsck_tbl = {
295 5, { 0, 70, 90, 92, 95, 100 }
296};
Theodore Ts'o5596def1999-07-19 15:27:37 +0000297static char bar[] =
298 "==============================================================="
299 "===============================================================";
300static char spaces[] =
301 " "
302 " ";
303
304static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
305 int max)
306{
307 float percent;
308
309 if (pass <= 0)
310 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000311 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000312 return 100.0;
313 percent = ((float) curr) / ((float) max);
314 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
315 + tbl->table[pass-1]);
316}
317
318extern void e2fsck_clear_progbar(e2fsck_t ctx)
319{
320 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
321 return;
322
323 printf("%s\r", spaces + (sizeof(spaces) - 80));
324 ctx->flags &= ~E2F_FLAG_PROG_BAR;
325}
326
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000327static int e2fsck_update_progress(e2fsck_t ctx, int pass,
328 unsigned long cur, unsigned long max)
329{
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000330 static const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000331 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000332 int i;
333 float percent;
Theodore Ts'o06012322000-02-12 20:12:43 +0000334 int tick;
335 struct timeval tv;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000336 static int dpywidth = 0;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000337
338 if (pass == 0)
339 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000340
341 if (ctx->progress_fd) {
342 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
343 write(ctx->progress_fd, buf, strlen(buf));
344 } else {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000345 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
346 return 0;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000347 if (dpywidth == 0) {
348 dpywidth = 66 - strlen(ctx->device_name);
349 dpywidth = 8 * (dpywidth / 8);
350 }
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000351 /*
352 * Calculate the new progress position. If the
353 * percentage hasn't changed, then we skip out right
354 * away.
355 */
356 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
357 if (ctx->progress_last_percent == (int) 10 * percent)
358 return 0;
359 ctx->progress_last_percent = (int) 10 * percent;
360
361 /*
362 * If we've already updated the spinner once within
363 * the last 1/8th of a second, no point doing it
364 * again.
365 */
Theodore Ts'o06012322000-02-12 20:12:43 +0000366 gettimeofday(&tv, NULL);
367 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
368 if ((tick == ctx->progress_last_time) &&
369 (cur != max) && (cur != 0))
370 return 0;
371 ctx->progress_last_time = tick;
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000372
373 /*
374 * Advance the spinner, and note that the progress bar
375 * will be on the screen
376 */
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000377 ctx->progress_pos = (ctx->progress_pos+1) & 3;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000378 ctx->flags |= E2F_FLAG_PROG_BAR;
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000379
Theodore Ts'o5596def1999-07-19 15:27:37 +0000380 i = ((percent * dpywidth) + 50) / 100;
381 printf("%s: |%s%s", ctx->device_name,
382 bar + (sizeof(bar) - (i+1)),
383 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
384 if (percent == 100.0)
385 fputc('|', stdout);
386 else
387 fputc(spinner[ctx->progress_pos & 3], stdout);
388 printf(" %4.1f%% \r", percent);
389 if (percent == 100.0)
390 e2fsck_clear_progbar(ctx);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000391 fflush(stdout);
392 }
393 return 0;
394}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000395
396#define PATH_SET "PATH=/sbin"
397
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000398static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000399{
400 int fd;
401
402 while (1) {
403 fd = open("/dev/null", O_RDWR);
404 if (fd > 2)
405 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000406 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000407 fprintf(stderr, _("ERROR: Couldn't open "
408 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000409 strerror(errno));
410 break;
411 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000412 }
413 close(fd);
414}
415
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000416#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000417static void signal_progress_on(int sig)
418{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000419 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000420
421 if (!ctx)
422 return;
423
424 ctx->progress = e2fsck_update_progress;
425 ctx->progress_fd = 0;
426}
427
428static void signal_progress_off(int sig)
429{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000430 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000431
432 if (!ctx)
433 return;
434
435 e2fsck_clear_progbar(ctx);
436 ctx->progress = 0;
437}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400438
439static void signal_cancel(int sig)
440{
441 e2fsck_t ctx = e2fsck_global_ctx;
442
443 if (!ctx)
444 exit(FSCK_CANCELED);
445
446 ctx->flags |= E2F_FLAG_CANCEL;
447}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000448#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000449
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400450static void parse_extended_opts(e2fsck_t ctx, const char *opts)
451{
452 char *buf, *token, *next, *p, *arg;
453 int len, ea_ver;
454 int extended_usage = 0;
455
456 len = strlen(opts);
457 buf = malloc(len+1);
458 if (!buf) {
459 fprintf(stderr, _("Couldn't allocate memory to parse "
460 "extended options!\n"));
461 exit(1);
462 }
463 strcpy(buf, opts);
464 for (token = buf; token && *token; token = next) {
465 p = strchr(token, ',');
466 next = 0;
467 if (p) {
468 *p = 0;
469 next = p+1;
470 }
471 arg = strchr(token, '=');
472 if (arg) {
473 *arg = 0;
474 arg++;
475 }
476 if (strcmp(token, "ea_ver") == 0) {
477 if (!arg) {
478 extended_usage++;
479 continue;
480 }
481 ea_ver = strtoul(arg, &p, 0);
482 if (*p ||
483 ((ea_ver != 1) && (ea_ver != 2))) {
484 fprintf(stderr,
485 _("Invalid EA version.\n"));
486 extended_usage++;
487 continue;
488 }
489 ctx->ext_attr_ver = ea_ver;
490 } else
491 extended_usage++;
492 }
493 if (extended_usage) {
494 fprintf(stderr, _("Extended options are separated by commas, "
495 "and may take an argument which\n"
496 "is set off by an equals ('=') sign. "
497 "Valid raid options are:\n"
498 "\tea_ver=<ea_version (1 or 2)\n\n"));
499 exit(1);
500 }
501}
502
503
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000504static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
505{
506 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000507 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000508#ifdef MTRACE
509 extern void *mallwatch;
510#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000511 e2fsck_t ctx;
512 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000513#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000514 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000515#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400516 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000517
518 retval = e2fsck_allocate_context(&ctx);
519 if (retval)
520 return retval;
521
522 *ret_ctx = ctx;
523
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000524 setbuf(stdout, NULL);
525 setbuf(stderr, NULL);
526 initialize_ext2_error_table();
527
528 if (argc && *argv)
529 ctx->program_name = *argv;
530 else
531 ctx->program_name = "e2fsck";
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400532 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 +0000533 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000534 case 'C':
535 ctx->progress = e2fsck_update_progress;
536 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000537 if (!ctx->progress_fd)
538 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000539 /* Validate the file descriptor to avoid disasters */
540 fd = dup(ctx->progress_fd);
541 if (fd < 0) {
542 fprintf(stderr,
543 _("Error validating file descriptor %d: %s\n"),
544 ctx->progress_fd,
545 error_message(errno));
546 fatal_error(ctx,
547 _("Invalid completion information file descriptor"));
548 } else
549 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000550 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400551 case 'D':
552 ctx->options |= E2F_OPT_COMPRESS_DIRS;
553 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400554 case 'E':
555 extended_opts = optarg;
556 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000557 case 'p':
558 case 'a':
559 ctx->options |= E2F_OPT_PREEN;
560 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
561 break;
562 case 'n':
563 ctx->options |= E2F_OPT_NO;
564 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
565 break;
566 case 'y':
567 ctx->options |= E2F_OPT_YES;
568 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
569 break;
570 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000571#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000572 if (ctx->options & E2F_OPT_TIME)
573 ctx->options |= E2F_OPT_TIME2;
574 else
575 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000576#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000577 fprintf(stderr, _("The -t option is not "
578 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000579#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000580 break;
581 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500582 if (cflag++)
583 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 ctx->options |= E2F_OPT_CHECKBLOCKS;
585 break;
586 case 'r':
587 /* What we do by default, anyway! */
588 break;
589 case 'b':
590 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400591 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000592 break;
593 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500594 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000595 break;
596 case 'I':
597 ctx->inode_buffer_blocks = atoi(optarg);
598 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400599 case 'j':
600 ctx->journal_name = optarg;
601 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000602 case 'P':
603 ctx->process_inode_size = atoi(optarg);
604 break;
605 case 'L':
606 replace_bad_blocks++;
607 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000608 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000609 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000610 fatal_error(ctx,
611 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 strcpy(bad_blocks_file, optarg);
613 break;
614 case 'd':
615 ctx->options |= E2F_OPT_DEBUG;
616 break;
617 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500618 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 break;
620 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000621 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000622 break;
623 case 'v':
624 verbose = 1;
625 break;
626 case 'V':
627 show_version_only = 1;
628 break;
629#ifdef MTRACE
630 case 'M':
631 mallwatch = (void *) strtol(optarg, NULL, 0);
632 break;
633#endif
634 case 'N':
635 ctx->device_name = optarg;
636 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000637#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000638 case 's':
639 normalize_swapfs = 1;
640 case 'S':
641 swapfs = 1;
642 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000643#else
644 case 's':
645 case 'S':
646 fprintf(stderr, _("Byte-swapping filesystems "
647 "not compiled in this version "
648 "of e2fsck\n"));
649 exit(1);
650#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000651 default:
652 usage(ctx);
653 }
654 if (show_version_only)
655 return 0;
656 if (optind != argc - 1)
657 usage(ctx);
658 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400659 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000660 ctx->options |= E2F_OPT_READONLY;
661 ctx->filesystem_name = argv[optind];
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400662 if (extended_opts)
663 parse_extended_opts(ctx, extended_opts);
664
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000665 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500666 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000667 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000668 com_err("open", errno,
669 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000671 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000672 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000673 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000674 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000675 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000676 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000677 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000678 }
679 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000680 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000681#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000682 if (swapfs) {
683 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000684 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500685 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000686 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000687 }
688 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000689#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500690 if (cflag && bad_blocks_file) {
691 fprintf(stderr, _("The -c and the -l/-L options may "
692 "not be both used at the same time.\n"));
693 exit(FSCK_USAGE);
694 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000695#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000696 /*
697 * Set up signal action
698 */
699 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400700 sa.sa_handler = signal_cancel;
701 sigaction(SIGINT, &sa, 0);
702 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000703#ifdef SA_RESTART
704 sa.sa_flags = SA_RESTART;
705#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000706 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000707 sa.sa_handler = signal_progress_on;
708 sigaction(SIGUSR1, &sa, 0);
709 sa.sa_handler = signal_progress_off;
710 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000711#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400712
713 /* Update our PATH to include /sbin if we need to run badblocks */
714 if (cflag) {
715 char *oldpath = getenv("PATH");
716 if (oldpath) {
717 char *newpath;
718
719 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
720 strlen (oldpath));
721 if (!newpath)
722 fatal_error(ctx, "Couldn't malloc() newpath");
723 strcpy (newpath, PATH_SET);
724 strcat (newpath, ":");
725 strcat (newpath, oldpath);
726 putenv (newpath);
727 } else
728 putenv (PATH_SET);
729 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000730 return 0;
731}
732
733static const char *my_ver_string = E2FSPROGS_VERSION;
734static const char *my_ver_date = E2FSPROGS_DATE;
735
736int main (int argc, char *argv[])
737{
738 errcode_t retval = 0;
739 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000740 ext2_filsys fs = 0;
741 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000742 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000743 const char *lib_ver_date;
744 int my_ver, lib_ver;
745 e2fsck_t ctx;
746 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000747 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000748
749 clear_problem_context(&pctx);
750#ifdef MTRACE
751 mtrace();
752#endif
753#ifdef MCHECK
754 mcheck(0);
755#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000756#ifdef ENABLE_NLS
757 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500758 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000759 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
760 textdomain(NLS_CAT_NAME);
761#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000762 my_ver = ext2fs_parse_version_string(my_ver_string);
763 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
764 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000765 fprintf( stderr, _("Error: ext2fs library version "
766 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000767 show_version_only++;
768 }
769
770 retval = PRS(argc, argv, &ctx);
771 if (retval) {
772 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000773 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000774 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000775 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000776 reserve_stdio_fds();
777
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000778#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000779 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000780#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000781
782 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400783 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string,
784 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000785
786 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000787 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000788 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000789 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000790 }
791
792 check_mount(ctx);
793
794 if (!(ctx->options & E2F_OPT_PREEN) &&
795 !(ctx->options & E2F_OPT_NO) &&
796 !(ctx->options & E2F_OPT_YES)) {
797 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000798 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000799 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000800 }
801 ctx->superblock = ctx->use_superblock;
802restart:
803#if 1
804 io_ptr = unix_io_manager;
805#else
806 io_ptr = test_io_manager;
807 test_io_backing_manager = unix_io_manager;
808#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000809 flags = 0;
810 if ((ctx->options & E2F_OPT_READONLY) == 0)
811 flags |= EXT2_FLAG_RW;
812
Theodore Ts'of1a17612001-12-23 22:27:52 -0500813 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000814 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500815 ctx->superblock, ctx->blocksize,
816 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000817 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600818 int blocksize;
819 for (blocksize = EXT2_MIN_BLOCK_SIZE;
820 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000821 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600822 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000823 io_ptr, &fs);
824 if (!retval)
825 break;
826 }
827 } else
828 retval = ext2fs_open(ctx->filesystem_name, flags,
829 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400830 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
831 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000832 ((retval == EXT2_ET_BAD_MAGIC) ||
833 ((retval == 0) && ext2fs_check_desc(fs)))) {
834 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000835 printf(_("%s trying backup blocks...\n"),
836 retval ? _("Couldn't find ext2 superblock,") :
837 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500838 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839 if (fs)
840 ext2fs_close(fs);
841 goto restart;
842 }
843 }
844 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000845 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000846 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000847 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000848 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000849 "too high for this version of e2fsck.\n"
850 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000851 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000852 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
853 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000854 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000855 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000856 printf(_("You must have %s access to the "
857 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000858 (ctx->options & E2F_OPT_READONLY) ?
859 "r/o" : "r/w");
860 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000861 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000862#ifdef EROFS
863 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000864 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000865 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000866 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000867#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000868 else
869 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000870 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000871 }
872 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000873 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000874 sb = fs->super;
875 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000876 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000877 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000878 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000879 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000880 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000881 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000882
Theodore Ts'o17390c02000-07-07 04:13:21 +0000883 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000884 * Set the device name, which is used whenever we print error
885 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000886 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000887 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000888 (sb->s_volume_name[0] != 0)) {
889 char *cp = malloc(sizeof(sb->s_volume_name)+1);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000890 if (cp) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000891 strncpy(cp, sb->s_volume_name,
892 sizeof(sb->s_volume_name));
893 cp[sizeof(sb->s_volume_name)] = 0;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000894 ctx->device_name = cp;
895 }
896 }
897 if (ctx->device_name == 0)
898 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000899
900 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000901 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000902 */
903 retval = e2fsck_check_ext3_journal(ctx);
904 if (retval) {
905 com_err(ctx->program_name, retval,
906 _("while checking ext3 journal for %s"),
907 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000908 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000909 }
910
Theodore Ts'o9b565752000-12-13 18:50:22 +0000911 /*
912 * Check to see if we need to do ext3-style recovery. If so,
913 * do it, and then restart the fsck.
914 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000915 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000916 if (ctx->options & E2F_OPT_READONLY) {
917 printf(_("Warning: skipping journal recovery "
918 "because doing a read-only filesystem "
919 "check.\n"));
920 io_channel_flush(ctx->fs->io);
921 } else {
922 retval = e2fsck_run_ext3_journal(ctx);
923 if (retval) {
924 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000925 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000926 ctx->device_name);
927 fatal_error(ctx, 0);
928 }
929 ext2fs_close(ctx->fs);
930 ctx->fs = 0;
931 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000932 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000933 }
934
935 /*
936 * Check for compatibility with the feature sets. We need to
937 * be more stringent than ext2fs_open().
938 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000939 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
940 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000941 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
942 "(%s)", ctx->device_name);
943 goto get_newer;
944 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000945 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000946 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
947 "(%s)", ctx->device_name);
948 goto get_newer;
949 }
950#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000951 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000952 com_err(ctx->program_name, 0,
953 _("Warning: compression support is experimental.\n"));
954#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400955#ifndef ENABLE_HTREE
956 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
957 com_err(ctx->program_name, 0,
958 _("E2fsck not compiled with HTREE support,\n\t"
959 "but filesystem %s has HTREE directories.\n"),
960 ctx->device_name);
961 goto get_newer;
962 }
963#endif
964
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000965 /*
966 * If the user specified a specific superblock, presumably the
967 * master superblock has been trashed. So we mark the
968 * superblock as dirty, so it can be written out.
969 */
970 if (ctx->superblock &&
971 !(ctx->options & E2F_OPT_READONLY))
972 ext2fs_mark_super_dirty(fs);
973
974 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400975 * We only update the master superblock because (a) paranoia;
976 * we don't want to corrupt the backup superblocks, and (b) we
977 * don't need to update the mount count and last checked
978 * fields in the backup superblock (the kernel doesn't
979 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000980 */
981 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
982
983 ehandler_init(fs->io);
984
985 if (ctx->superblock)
986 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
987 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000988 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000989 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000990 check_if_skip(ctx);
991 if (bad_blocks_file)
992 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
993 else if (cflag)
994 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000995 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000996 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000997#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000998 if (normalize_swapfs) {
999 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1000 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001001 fprintf(stderr, _("%s: Filesystem byte order "
1002 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001003 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001004 }
1005 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001006 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001007 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001008 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001009 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001010 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001011#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001012
1013 /*
1014 * Mark the system as valid, 'til proven otherwise
1015 */
1016 ext2fs_mark_valid(fs);
1017
1018 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1019 if (retval) {
1020 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001021 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001022 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001023 printf(_("This doesn't bode well,"
1024 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001025 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001026
1027 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001028 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001029 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001030 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001031 retval = e2fsck_reset_context(ctx);
1032 if (retval) {
1033 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001034 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001035 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001036 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001037 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001038 goto restart;
1039 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001040 if (run_result & E2F_FLAG_CANCEL) {
1041 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1042 ctx->device_name : ctx->filesystem_name);
1043 exit_value |= FSCK_CANCELED;
1044 }
1045 if (run_result & E2F_FLAG_ABORT)
1046 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001047
1048#ifdef MTRACE
1049 mtrace_print("Cleanup");
1050#endif
1051 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001052 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001053 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001054 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001055 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001056 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001057 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001058 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001059 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001060 }
1061 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001062 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001063 printf(_("\n%s: ********** WARNING: Filesystem still has "
1064 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001065 exit_value |= FSCK_UNCORRECTED;
1066 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001067 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001068 if (!(ctx->options & E2F_OPT_READONLY)) {
1069 if (ext2fs_test_valid(fs)) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001070 if (!(sb->s_state & EXT2_VALID_FS))
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001071 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001072 sb->s_state = EXT2_VALID_FS;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001073 } else
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001074 sb->s_state &= ~EXT2_VALID_FS;
1075 sb->s_mnt_count = 0;
1076 sb->s_lastcheck = time(NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001077 ext2fs_mark_super_dirty(fs);
1078 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001079 if (exit_value & FSCK_CANCELED)
1080 exit_value &= ~FSCK_NONDESTRUCT;
1081 else
1082 show_stats(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001083
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001084 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001085
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001086 ext2fs_close(fs);
1087 ctx->fs = NULL;
1088 e2fsck_free_context(ctx);
1089
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001090#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001091 if (ctx->options & E2F_OPT_TIME)
1092 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001093#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001094
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001095 return exit_value;
1096}