blob: 492ba4a1a4d3d0bfaad9a8086f234519773e4406 [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'o5e72cdb2002-11-08 15:35:13 -050056#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
57int journal_enable_debug = -1;
58#endif
59
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060static void usage(e2fsck_t ctx)
61{
62 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050063 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000064 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040065 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
66 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000067 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000068
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000069 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000070 " -p Automatic repair (no questions)\n"
71 " -n Make no changes to the filesystem\n"
72 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050073 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000074 " -f Force checking even if filesystem is marked clean\n"));
75 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000076 " -v Be verbose\n"
77 " -b superblock Use alternative superblock\n"
78 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040079 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000080 " -l bad_blocks_file Add to badblocks list\n"
81 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000082 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000083
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000084 exit(FSCK_USAGE);
85}
86
87static void show_stats(e2fsck_t ctx)
88{
89 ext2_filsys fs = ctx->fs;
90 int inodes, inodes_used, blocks, blocks_used;
91 int dir_links;
92 int num_files, num_links;
93 int frag_percent;
94
95 dir_links = 2 * ctx->fs_directory_count - 1;
96 num_files = ctx->fs_total_count - dir_links;
97 num_links = ctx->fs_links_count - dir_links;
98 inodes = fs->super->s_inodes_count;
99 inodes_used = (fs->super->s_inodes_count -
100 fs->super->s_free_inodes_count);
101 blocks = fs->super->s_blocks_count;
102 blocks_used = (fs->super->s_blocks_count -
103 fs->super->s_free_blocks_count);
104
105 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
106 frag_percent = (frag_percent + 5) / 10;
107
108 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000109 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000110 ctx->device_name, inodes_used, inodes,
111 frag_percent / 10, frag_percent % 10,
112 blocks_used, blocks);
113 return;
114 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000115 /*
116 * This is a bit ugly. But I think there will nearly always be more
117 * than one "thing" to report about, so I won't try writing complex
118 * code to handle one/two/many forms of all words.
119 * Some languages (Italian, at least) never uses the plural form
120 * of foreign words, so in real life this could not be a problem.
121 * md@linux.it - 2000-1-15
122 */
123#ifdef ENABLE_NLS
124 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400125 100 * inodes_used / inodes);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000126 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
127 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
128 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
129 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
130 printf (_("%8d blocks used (%d%%)\n"
131 "%8d bad blocks\n"), blocks_used,
Theodore Ts'o636a9542001-06-29 17:57:26 -0400132 (int) ((long long) 100 * blocks_used / blocks),
133 ctx->fs_badblocks_count);
Theodore Ts'o2b94c652001-08-09 04:08:52 -0400134 printf(_("%8d large files\n"), ctx->large_files);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000135 printf (_("\n%8d regular files\n"
136 "%8d directories\n"
137 "%8d character device files\n"
138 "%8d block device files\n"
139 "%8d fifos\n"
140 "%8d links\n"
141 "%8d symbolic links (%d fast symbolic links)\n"
142 "%8d sockets\n"
143 "--------\n"
144 "%8d files\n"),
145 ctx->fs_regular_count,
146 ctx->fs_directory_count,
147 ctx->fs_chardev_count,
148 ctx->fs_blockdev_count,
149 ctx->fs_fifo_count,
150 ctx->fs_links_count - dir_links,
151 ctx->fs_symlinks_count,
152 ctx->fs_fast_symlinks_count,
153 ctx->fs_sockets_count,
154 ctx->fs_total_count - dir_links);
155#else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000156 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
157 (inodes_used != 1) ? "s" : "",
158 100 * inodes_used / inodes);
159 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
160 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
161 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
162 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
163 printf ("%8d block%s used (%d%%)\n"
164 "%8d bad block%s\n", blocks_used,
165 (blocks_used != 1) ? "s" : "",
166 100 * blocks_used / blocks, ctx->fs_badblocks_count,
167 ctx->fs_badblocks_count != 1 ? "s" : "");
Theodore Ts'o2b94c652001-08-09 04:08:52 -0400168 printf(_("%8d large file%s\n"), ctx->large_files,
169 (ctx->large_files != 1) ? "s" : "");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000170 printf ("\n%8d regular file%s\n"
171 "%8d director%s\n"
172 "%8d character device file%s\n"
173 "%8d block device file%s\n"
174 "%8d fifo%s\n"
175 "%8d link%s\n"
176 "%8d symbolic link%s (%d fast symbolic link%s)\n"
177 "%8d socket%s\n"
178 "--------\n"
179 "%8d file%s\n",
180 ctx->fs_regular_count,
181 (ctx->fs_regular_count != 1) ? "s" : "",
182 ctx->fs_directory_count,
183 (ctx->fs_directory_count != 1) ? "ies" : "y",
184 ctx->fs_chardev_count,
185 (ctx->fs_chardev_count != 1) ? "s" : "",
186 ctx->fs_blockdev_count,
187 (ctx->fs_blockdev_count != 1) ? "s" : "",
188 ctx->fs_fifo_count,
189 (ctx->fs_fifo_count != 1) ? "s" : "",
190 ctx->fs_links_count - dir_links,
191 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
192 ctx->fs_symlinks_count,
193 (ctx->fs_symlinks_count != 1) ? "s" : "",
194 ctx->fs_fast_symlinks_count,
195 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
196 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
197 ctx->fs_total_count - dir_links,
198 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000199#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000200}
201
202static void check_mount(e2fsck_t ctx)
203{
204 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500205 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000206
Theodore Ts'oee895132002-11-07 16:16:55 -0500207 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
208 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000209 if (retval) {
210 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000211 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000212 ctx->filesystem_name);
213 return;
214 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400215
216 /*
217 * If the filesystem isn't mounted, or it's the root filesystem
218 * and it's mounted read-only, then everything's fine.
219 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500220 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
221 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
222 (ctx->mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000223 return;
224
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000225 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000226 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000227 return;
228 }
229
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000230 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000231 if (!isatty(0) || !isatty(1))
232 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000233 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000234 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000235 "SEVERE filesystem damage.\007\007\007\n\n"));
236 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000237 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000238 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000239 exit (0);
240 }
241 return;
242}
243
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000244/*
245 * This routine checks to see if a filesystem can be skipped; if so,
246 * it will exit with E2FSCK_OK. Under some conditions it will print a
247 * message explaining why a check is being forced.
248 */
249static void check_if_skip(e2fsck_t ctx)
250{
251 ext2_filsys fs = ctx->fs;
252 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000253 unsigned int reason_arg = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000254
Theodore Ts'od37066a2001-12-21 23:28:54 -0500255 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
256 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000257 return;
258
259 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000260 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000261 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000262 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000263 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000264 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000265 (unsigned) fs->super->s_max_mnt_count)) {
266 reason = _(" has been mounted %u times without being checked");
267 reason_arg = fs->super->s_mnt_count;
268 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000269 time(0) >= (fs->super->s_lastcheck +
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000270 fs->super->s_checkinterval)) {
271 reason = _(" has gone %u days without being checked");
272 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
273 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000274 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000275 fputs(ctx->device_name, stdout);
276 printf(reason, reason_arg);
277 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000278 return;
279 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000280 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000281 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
282 fs->super->s_inodes_count,
283 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
284 fs->super->s_blocks_count);
285 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400286 ctx->fs = NULL;
287 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000288 exit(FSCK_OK);
289}
290
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000291/*
292 * For completion notice
293 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000294struct percent_tbl {
295 int max_pass;
296 int table[32];
297};
298struct percent_tbl e2fsck_tbl = {
299 5, { 0, 70, 90, 92, 95, 100 }
300};
Theodore Ts'o5596def1999-07-19 15:27:37 +0000301static char bar[] =
302 "==============================================================="
303 "===============================================================";
304static char spaces[] =
305 " "
306 " ";
307
308static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
309 int max)
310{
311 float percent;
312
313 if (pass <= 0)
314 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000315 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000316 return 100.0;
317 percent = ((float) curr) / ((float) max);
318 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
319 + tbl->table[pass-1]);
320}
321
322extern void e2fsck_clear_progbar(e2fsck_t ctx)
323{
324 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
325 return;
326
327 printf("%s\r", spaces + (sizeof(spaces) - 80));
328 ctx->flags &= ~E2F_FLAG_PROG_BAR;
329}
330
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500331int e2fsck_simple_progress(e2fsck_t ctx, char *label, float percent,
332 unsigned int dpynum)
333{
334 static const char spinner[] = "\\|/-";
335 char buf[80];
336 int i;
337 int tick;
338 struct timeval tv;
339 int dpywidth;
340
341 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
342 return 0;
343
344 /*
345 * Calculate the new progress position. If the
346 * percentage hasn't changed, then we skip out right
347 * away.
348 */
349 if (ctx->progress_last_percent == (int) 10 * percent)
350 return 0;
351 ctx->progress_last_percent = (int) 10 * percent;
352
353 /*
354 * If we've already updated the spinner once within
355 * the last 1/8th of a second, no point doing it
356 * again.
357 */
358 gettimeofday(&tv, NULL);
359 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
360 if ((tick == ctx->progress_last_time) &&
361 (percent != 0.0) && (percent != 100.0))
362 return 0;
363 ctx->progress_last_time = tick;
364
365 /*
366 * Advance the spinner, and note that the progress bar
367 * will be on the screen
368 */
369 ctx->progress_pos = (ctx->progress_pos+1) & 3;
370 ctx->flags |= E2F_FLAG_PROG_BAR;
371
372 dpywidth = 66 - strlen(label);
373 dpywidth = 8 * (dpywidth / 8);
374 if (dpynum)
375 dpywidth -= 8;
376
377 i = ((percent * dpywidth) + 50) / 100;
378 printf("%s: |%s%s", label, bar + (sizeof(bar) - (i+1)),
379 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
380 if (percent == 100.0)
381 fputc('|', stdout);
382 else
383 fputc(spinner[ctx->progress_pos & 3], stdout);
384 if (dpynum)
385 printf(" %4.1f%% %u\r", percent, dpynum);
386 else
387 printf(" %4.1f%% \r", percent);
388
389 if (percent == 100.0)
390 e2fsck_clear_progbar(ctx);
391 fflush(stdout);
392
393 return 0;
394}
395
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000396static int e2fsck_update_progress(e2fsck_t ctx, int pass,
397 unsigned long cur, unsigned long max)
398{
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000399 static const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000400 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000401 int i;
402 float percent;
Theodore Ts'o06012322000-02-12 20:12:43 +0000403 int tick;
404 struct timeval tv;
Theodore Ts'o17390c02000-07-07 04:13:21 +0000405 static int dpywidth = 0;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000406
407 if (pass == 0)
408 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000409
410 if (ctx->progress_fd) {
411 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
412 write(ctx->progress_fd, buf, strlen(buf));
413 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000414 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500415 e2fsck_simple_progress(ctx, ctx->device_name,
416 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000417 }
418 return 0;
419}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000420
421#define PATH_SET "PATH=/sbin"
422
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000423static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000424{
425 int fd;
426
427 while (1) {
428 fd = open("/dev/null", O_RDWR);
429 if (fd > 2)
430 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000431 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000432 fprintf(stderr, _("ERROR: Couldn't open "
433 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000434 strerror(errno));
435 break;
436 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000437 }
438 close(fd);
439}
440
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000441#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000442static void signal_progress_on(int sig)
443{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000444 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000445
446 if (!ctx)
447 return;
448
449 ctx->progress = e2fsck_update_progress;
450 ctx->progress_fd = 0;
451}
452
453static void signal_progress_off(int sig)
454{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000455 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000456
457 if (!ctx)
458 return;
459
460 e2fsck_clear_progbar(ctx);
461 ctx->progress = 0;
462}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400463
464static void signal_cancel(int sig)
465{
466 e2fsck_t ctx = e2fsck_global_ctx;
467
468 if (!ctx)
469 exit(FSCK_CANCELED);
470
471 ctx->flags |= E2F_FLAG_CANCEL;
472}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000473#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000474
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400475static void parse_extended_opts(e2fsck_t ctx, const char *opts)
476{
477 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500478 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400479 int extended_usage = 0;
480
Theodore Ts'of3640932003-03-01 19:47:44 -0500481 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400482 for (token = buf; token && *token; token = next) {
483 p = strchr(token, ',');
484 next = 0;
485 if (p) {
486 *p = 0;
487 next = p+1;
488 }
489 arg = strchr(token, '=');
490 if (arg) {
491 *arg = 0;
492 arg++;
493 }
494 if (strcmp(token, "ea_ver") == 0) {
495 if (!arg) {
496 extended_usage++;
497 continue;
498 }
499 ea_ver = strtoul(arg, &p, 0);
500 if (*p ||
501 ((ea_ver != 1) && (ea_ver != 2))) {
502 fprintf(stderr,
503 _("Invalid EA version.\n"));
504 extended_usage++;
505 continue;
506 }
507 ctx->ext_attr_ver = ea_ver;
508 } else
509 extended_usage++;
510 }
511 if (extended_usage) {
512 fprintf(stderr, _("Extended options are separated by commas, "
513 "and may take an argument which\n"
514 "is set off by an equals ('=') sign. "
515 "Valid raid options are:\n"
516 "\tea_ver=<ea_version (1 or 2)\n\n"));
517 exit(1);
518 }
519}
520
521
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000522static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
523{
524 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000525 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000526#ifdef MTRACE
527 extern void *mallwatch;
528#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000529 e2fsck_t ctx;
530 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000531#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000532 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000533#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400534 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000535
536 retval = e2fsck_allocate_context(&ctx);
537 if (retval)
538 return retval;
539
540 *ret_ctx = ctx;
541
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000542 setbuf(stdout, NULL);
543 setbuf(stderr, NULL);
544 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500545 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000546
547 if (argc && *argv)
548 ctx->program_name = *argv;
549 else
550 ctx->program_name = "e2fsck";
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400551 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 +0000552 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000553 case 'C':
554 ctx->progress = e2fsck_update_progress;
555 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000556 if (!ctx->progress_fd)
557 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000558 /* Validate the file descriptor to avoid disasters */
559 fd = dup(ctx->progress_fd);
560 if (fd < 0) {
561 fprintf(stderr,
562 _("Error validating file descriptor %d: %s\n"),
563 ctx->progress_fd,
564 error_message(errno));
565 fatal_error(ctx,
566 _("Invalid completion information file descriptor"));
567 } else
568 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000569 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400570 case 'D':
571 ctx->options |= E2F_OPT_COMPRESS_DIRS;
572 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400573 case 'E':
574 extended_opts = optarg;
575 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000576 case 'p':
577 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500578 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
579 conflict_opt:
580 fatal_error(ctx,
581 _("Only one the options -p/-a, -n or -y may be specified."));
582 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000583 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 break;
585 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500586 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
587 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000588 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000589 break;
590 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500591 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
592 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000593 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000594 break;
595 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000596#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000597 if (ctx->options & E2F_OPT_TIME)
598 ctx->options |= E2F_OPT_TIME2;
599 else
600 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000601#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000602 fprintf(stderr, _("The -t option is not "
603 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000604#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000605 break;
606 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500607 if (cflag++)
608 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000609 ctx->options |= E2F_OPT_CHECKBLOCKS;
610 break;
611 case 'r':
612 /* What we do by default, anyway! */
613 break;
614 case 'b':
615 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400616 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 break;
618 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500619 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000620 break;
621 case 'I':
622 ctx->inode_buffer_blocks = atoi(optarg);
623 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400624 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500625 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400626 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000627 case 'P':
628 ctx->process_inode_size = atoi(optarg);
629 break;
630 case 'L':
631 replace_bad_blocks++;
632 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500633 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000634 break;
635 case 'd':
636 ctx->options |= E2F_OPT_DEBUG;
637 break;
638 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500639 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000640 break;
641 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000642 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000643 break;
644 case 'v':
645 verbose = 1;
646 break;
647 case 'V':
648 show_version_only = 1;
649 break;
650#ifdef MTRACE
651 case 'M':
652 mallwatch = (void *) strtol(optarg, NULL, 0);
653 break;
654#endif
655 case 'N':
656 ctx->device_name = optarg;
657 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000658#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000659 case 's':
660 normalize_swapfs = 1;
661 case 'S':
662 swapfs = 1;
663 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000664#else
665 case 's':
666 case 'S':
667 fprintf(stderr, _("Byte-swapping filesystems "
668 "not compiled in this version "
669 "of e2fsck\n"));
670 exit(1);
671#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000672 default:
673 usage(ctx);
674 }
675 if (show_version_only)
676 return 0;
677 if (optind != argc - 1)
678 usage(ctx);
679 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400680 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000681 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'of3640932003-03-01 19:47:44 -0500682 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400683 if (extended_opts)
684 parse_extended_opts(ctx, extended_opts);
685
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000686 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500687 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000688 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000689 com_err("open", errno,
690 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000691 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000692 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000693 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000694 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000695 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000696 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000697 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000698 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000699 }
700 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000701 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000702#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000703 if (swapfs) {
704 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000705 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500706 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000707 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000708 }
709 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000710#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500711 if (cflag && bad_blocks_file) {
712 fprintf(stderr, _("The -c and the -l/-L options may "
713 "not be both used at the same time.\n"));
714 exit(FSCK_USAGE);
715 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000716#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000717 /*
718 * Set up signal action
719 */
720 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400721 sa.sa_handler = signal_cancel;
722 sigaction(SIGINT, &sa, 0);
723 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000724#ifdef SA_RESTART
725 sa.sa_flags = SA_RESTART;
726#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000727 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000728 sa.sa_handler = signal_progress_on;
729 sigaction(SIGUSR1, &sa, 0);
730 sa.sa_handler = signal_progress_off;
731 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000732#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400733
734 /* Update our PATH to include /sbin if we need to run badblocks */
735 if (cflag) {
736 char *oldpath = getenv("PATH");
737 if (oldpath) {
738 char *newpath;
739
740 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
741 strlen (oldpath));
742 if (!newpath)
743 fatal_error(ctx, "Couldn't malloc() newpath");
744 strcpy (newpath, PATH_SET);
745 strcat (newpath, ":");
746 strcat (newpath, oldpath);
747 putenv (newpath);
748 } else
749 putenv (PATH_SET);
750 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500751#ifdef CONFIG_JBD_DEBUG
752 if (getenv("E2FSCK_JBD_DEBUG"))
753 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
754#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000755 return 0;
756}
757
758static const char *my_ver_string = E2FSPROGS_VERSION;
759static const char *my_ver_date = E2FSPROGS_DATE;
760
761int main (int argc, char *argv[])
762{
763 errcode_t retval = 0;
764 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000765 ext2_filsys fs = 0;
766 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000767 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000768 const char *lib_ver_date;
769 int my_ver, lib_ver;
770 e2fsck_t ctx;
771 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000772 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000773
774 clear_problem_context(&pctx);
775#ifdef MTRACE
776 mtrace();
777#endif
778#ifdef MCHECK
779 mcheck(0);
780#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000781#ifdef ENABLE_NLS
782 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500783 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000784 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
785 textdomain(NLS_CAT_NAME);
786#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000787 my_ver = ext2fs_parse_version_string(my_ver_string);
788 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
789 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000790 fprintf( stderr, _("Error: ext2fs library version "
791 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000792 show_version_only++;
793 }
794
795 retval = PRS(argc, argv, &ctx);
796 if (retval) {
797 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000798 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000799 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000800 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000801 reserve_stdio_fds();
802
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000803#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000804 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000805#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000806
807 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400808 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string,
809 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000810
811 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000812 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000813 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000814 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000815 }
816
817 check_mount(ctx);
818
819 if (!(ctx->options & E2F_OPT_PREEN) &&
820 !(ctx->options & E2F_OPT_NO) &&
821 !(ctx->options & E2F_OPT_YES)) {
822 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000823 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000824 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000825 }
826 ctx->superblock = ctx->use_superblock;
827restart:
828#if 1
829 io_ptr = unix_io_manager;
830#else
831 io_ptr = test_io_manager;
832 test_io_backing_manager = unix_io_manager;
833#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000834 flags = 0;
835 if ((ctx->options & E2F_OPT_READONLY) == 0)
836 flags |= EXT2_FLAG_RW;
837
Theodore Ts'of1a17612001-12-23 22:27:52 -0500838 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500840 ctx->superblock, ctx->blocksize,
841 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000842 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600843 int blocksize;
844 for (blocksize = EXT2_MIN_BLOCK_SIZE;
845 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000846 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600847 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000848 io_ptr, &fs);
849 if (!retval)
850 break;
851 }
852 } else
853 retval = ext2fs_open(ctx->filesystem_name, flags,
854 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400855 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
856 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000857 ((retval == EXT2_ET_BAD_MAGIC) ||
858 ((retval == 0) && ext2fs_check_desc(fs)))) {
859 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000860 printf(_("%s trying backup blocks...\n"),
861 retval ? _("Couldn't find ext2 superblock,") :
862 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500863 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000864 if (fs)
865 ext2fs_close(fs);
866 goto restart;
867 }
868 }
869 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000870 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000871 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000872 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000873 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000874 "too high for this version of e2fsck.\n"
875 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000876 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000877 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
878 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000879 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000880 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000881 printf(_("You must have %s access to the "
882 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000883 (ctx->options & E2F_OPT_READONLY) ?
884 "r/o" : "r/w");
885 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000886 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000887#ifdef EROFS
888 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000889 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000890 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000891 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000892#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000893 else
894 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000895 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000896 }
897 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000898 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000899 sb = fs->super;
900 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000901 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000902 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000903 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000904 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000905 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000906 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000907
Theodore Ts'o17390c02000-07-07 04:13:21 +0000908 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000909 * Set the device name, which is used whenever we print error
910 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000911 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000912 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000913 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500914 ctx->device_name = string_copy(ctx, sb->s_volume_name,
915 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000916 }
917 if (ctx->device_name == 0)
918 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000919
920 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000921 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000922 */
923 retval = e2fsck_check_ext3_journal(ctx);
924 if (retval) {
925 com_err(ctx->program_name, retval,
926 _("while checking ext3 journal for %s"),
927 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000928 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000929 }
930
Theodore Ts'o9b565752000-12-13 18:50:22 +0000931 /*
932 * Check to see if we need to do ext3-style recovery. If so,
933 * do it, and then restart the fsck.
934 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000935 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000936 if (ctx->options & E2F_OPT_READONLY) {
937 printf(_("Warning: skipping journal recovery "
938 "because doing a read-only filesystem "
939 "check.\n"));
940 io_channel_flush(ctx->fs->io);
941 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500942 if (ctx->flags & E2F_FLAG_RESTARTED) {
943 /*
944 * Whoops, we attempted to run the
945 * journal twice. This should never
946 * happen, unless the hardware or
947 * device driver is being bogus.
948 */
949 com_err(ctx->program_name, 0,
950 _("unable to set superblock flags on %s\n"), ctx->device_name);
951 fatal_error(ctx, 0);
952 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000953 retval = e2fsck_run_ext3_journal(ctx);
954 if (retval) {
955 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000956 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000957 ctx->device_name);
958 fatal_error(ctx, 0);
959 }
960 ext2fs_close(ctx->fs);
961 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500962 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000963 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000964 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000965 }
966
967 /*
968 * Check for compatibility with the feature sets. We need to
969 * be more stringent than ext2fs_open().
970 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000971 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
972 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000973 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
974 "(%s)", ctx->device_name);
975 goto get_newer;
976 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000977 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000978 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
979 "(%s)", ctx->device_name);
980 goto get_newer;
981 }
982#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000983 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000984 com_err(ctx->program_name, 0,
985 _("Warning: compression support is experimental.\n"));
986#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400987#ifndef ENABLE_HTREE
988 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
989 com_err(ctx->program_name, 0,
990 _("E2fsck not compiled with HTREE support,\n\t"
991 "but filesystem %s has HTREE directories.\n"),
992 ctx->device_name);
993 goto get_newer;
994 }
995#endif
996
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000997 /*
998 * If the user specified a specific superblock, presumably the
999 * master superblock has been trashed. So we mark the
1000 * superblock as dirty, so it can be written out.
1001 */
1002 if (ctx->superblock &&
1003 !(ctx->options & E2F_OPT_READONLY))
1004 ext2fs_mark_super_dirty(fs);
1005
1006 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001007 * We only update the master superblock because (a) paranoia;
1008 * we don't want to corrupt the backup superblocks, and (b) we
1009 * don't need to update the mount count and last checked
1010 * fields in the backup superblock (the kernel doesn't
1011 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001012 */
1013 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1014
1015 ehandler_init(fs->io);
1016
1017 if (ctx->superblock)
1018 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1019 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001020 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001021 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001022 check_if_skip(ctx);
1023 if (bad_blocks_file)
1024 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1025 else if (cflag)
1026 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001027 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001028 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001029#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001030 if (normalize_swapfs) {
1031 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1032 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001033 fprintf(stderr, _("%s: Filesystem byte order "
1034 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001035 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001036 }
1037 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001038 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001039 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001040 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001041 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001042 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001043#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001044
1045 /*
1046 * Mark the system as valid, 'til proven otherwise
1047 */
1048 ext2fs_mark_valid(fs);
1049
1050 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1051 if (retval) {
1052 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001053 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001054 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001055 printf(_("This doesn't bode well,"
1056 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001057 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001058
1059 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001060 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001061 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001062 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001063 retval = e2fsck_reset_context(ctx);
1064 if (retval) {
1065 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001066 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001067 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001068 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001069 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001070 goto restart;
1071 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001072 if (run_result & E2F_FLAG_CANCEL) {
1073 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1074 ctx->device_name : ctx->filesystem_name);
1075 exit_value |= FSCK_CANCELED;
1076 }
1077 if (run_result & E2F_FLAG_ABORT)
1078 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001079
1080#ifdef MTRACE
1081 mtrace_print("Cleanup");
1082#endif
1083 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001084 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001085 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001086 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001087 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001088 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001089 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001090 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001091 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001092 }
1093 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001094 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001095 printf(_("\n%s: ********** WARNING: Filesystem still has "
1096 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001097 exit_value |= FSCK_UNCORRECTED;
1098 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001099 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001100 if (exit_value & FSCK_CANCELED)
1101 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001102 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001103 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001104 if (!(ctx->options & E2F_OPT_READONLY)) {
1105 if (ext2fs_test_valid(fs)) {
1106 if (!(sb->s_state & EXT2_VALID_FS))
1107 exit_value |= FSCK_NONDESTRUCT;
1108 sb->s_state = EXT2_VALID_FS;
1109 } else
1110 sb->s_state &= ~EXT2_VALID_FS;
1111 sb->s_mnt_count = 0;
1112 sb->s_lastcheck = time(NULL);
1113 ext2fs_mark_super_dirty(fs);
1114 }
1115 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001116
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001117 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001118
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001119 ext2fs_close(fs);
1120 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001121 free(ctx->filesystem_name);
1122 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001123 e2fsck_free_context(ctx);
1124
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001125#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001126 if (ctx->options & E2F_OPT_TIME)
1127 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001128#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001129
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001130 return exit_value;
1131}