blob: 55dcfc199daba44eb5125e283aa08f609faf922e [file] [log] [blame]
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001/*
2 * unix.c - The unix-specific code for e2fsck
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#ifdef HAVE_STDLIB_H
14#include <stdlib.h>
15#endif
16#include <string.h>
17#include <fcntl.h>
18#include <ctype.h>
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000019#include <time.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000020#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +000021#include <signal.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000022#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000023#ifdef HAVE_GETOPT_H
24#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000025#else
26extern char *optarg;
27extern int optind;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000028#endif
29#include <unistd.h>
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33#ifdef HAVE_MNTENT_H
34#include <mntent.h>
35#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040036#ifdef HAVE_SYS_IOCTL_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000037#include <sys/ioctl.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040038#endif
Theodore Ts'oe71d8732003-03-14 02:13:48 -050039#ifdef HAVE_MALLOC_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000040#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050041#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000042
43#include "et/com_err.h"
44#include "e2fsck.h"
45#include "problem.h"
46#include "../version.h"
47
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000048/* Command line options */
Theodore Ts'o54a31a32003-08-19 10:08:34 -040049static int swapfs;
50static int normalize_swapfs;
51static int cflag; /* check disk */
52static int show_version_only;
53static int verbose;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054
Theodore Ts'o54a31a32003-08-19 10:08:34 -040055static int replace_bad_blocks;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -050056static int keep_bad_blocks;
Theodore Ts'o54a31a32003-08-19 10:08:34 -040057static char *bad_blocks_file;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000058
Theodore Ts'o243dc312000-08-22 21:37:47 +000059e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
60
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -050061#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
62int journal_enable_debug = -1;
63#endif
64
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000065static void usage(e2fsck_t ctx)
66{
67 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050068 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000069 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040070 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
71 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000072 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000073
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000074 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000075 " -p Automatic repair (no questions)\n"
76 " -n Make no changes to the filesystem\n"
77 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050078 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000079 " -f Force checking even if filesystem is marked clean\n"));
80 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000081 " -v Be verbose\n"
82 " -b superblock Use alternative superblock\n"
83 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040084 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000085 " -l bad_blocks_file Add to badblocks list\n"
86 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000087 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000088
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000089 exit(FSCK_USAGE);
90}
91
92static void show_stats(e2fsck_t ctx)
93{
94 ext2_filsys fs = ctx->fs;
95 int inodes, inodes_used, blocks, blocks_used;
96 int dir_links;
97 int num_files, num_links;
98 int frag_percent;
99
100 dir_links = 2 * ctx->fs_directory_count - 1;
101 num_files = ctx->fs_total_count - dir_links;
102 num_links = ctx->fs_links_count - dir_links;
103 inodes = fs->super->s_inodes_count;
104 inodes_used = (fs->super->s_inodes_count -
105 fs->super->s_free_inodes_count);
106 blocks = fs->super->s_blocks_count;
107 blocks_used = (fs->super->s_blocks_count -
108 fs->super->s_free_blocks_count);
109
110 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
111 frag_percent = (frag_percent + 5) / 10;
112
113 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000114 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000115 ctx->device_name, inodes_used, inodes,
116 frag_percent / 10, frag_percent % 10,
117 blocks_used, blocks);
118 return;
119 }
Theodore Ts'o113e4052003-05-17 21:00:46 -0400120 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
121 inodes_used), inodes_used, 100 * inodes_used / inodes);
122 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
123 "%8d non-contiguous inodes (%0d.%d%%)\n",
124 ctx->fs_fragmented),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000125 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
126 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
127 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Theodore Ts'o113e4052003-05-17 21:00:46 -0400128 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
129 blocks_used),
130 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
131 printf (P_("%8d bad block\n", "%8d bad blocks\n",
132 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
133 printf (P_("%8d large file\n", "%8d large files\n",
134 ctx->large_files), ctx->large_files);
135 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
136 ctx->fs_regular_count), ctx->fs_regular_count);
137 printf (P_("%8d directory\n", "%8d directories\n",
138 ctx->fs_directory_count), ctx->fs_directory_count);
139 printf (P_("%8d character device file\n",
140 "%8d character device files\n", ctx->fs_chardev_count),
141 ctx->fs_chardev_count);
142 printf (P_("%8d block device file\n", "%8d block device files\n",
143 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
144 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
145 ctx->fs_fifo_count);
146 printf (P_("%8d link\n", "%8d links\n",
147 ctx->fs_links_count - dir_links),
148 ctx->fs_links_count - dir_links);
149 printf (P_("%8d symbolic link", "%8d symbolic links",
150 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
151 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
152 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
153 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
154 ctx->fs_sockets_count);
155 printf ("--------\n");
156 printf (P_("%8d file\n", "%8d files\n",
157 ctx->fs_total_count - dir_links),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000158 ctx->fs_total_count - dir_links);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000159}
160
161static void check_mount(e2fsck_t ctx)
162{
163 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500164 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000165
Theodore Ts'oee895132002-11-07 16:16:55 -0500166 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
167 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000168 if (retval) {
169 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000170 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000171 ctx->filesystem_name);
172 return;
173 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400174
175 /*
176 * If the filesystem isn't mounted, or it's the root filesystem
177 * and it's mounted read-only, then everything's fine.
178 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500179 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
180 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
181 (ctx->mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000182 return;
183
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000184 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000185 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000186 return;
187 }
188
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000189 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400190 if (!ctx->interactive)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000191 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000192 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000193 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000194 "SEVERE filesystem damage.\007\007\007\n\n"));
195 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000196 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000197 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000198 exit (0);
199 }
200 return;
201}
202
Theodore Ts'o54434922003-12-07 01:28:50 -0500203static int is_on_batt(void)
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500204{
205 FILE *f;
206 char tmp[80], tmp2[80];
207 unsigned int acflag;
208
209 f = fopen("/proc/apm", "r");
210 if (f) {
211 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
212 acflag = 1;
213 fclose(f);
214 return (acflag != 1);
215 }
216 f = fopen("/proc/acpi/ac_adapter/AC/state", "r");
217 if (f) {
218 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
219 tmp[0] = 0;
220 fclose(f);
221 if (strncmp(tmp, "off-line", 8) == 0)
222 return 1;
223 }
224 return 0;
225}
226
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000227/*
228 * This routine checks to see if a filesystem can be skipped; if so,
229 * it will exit with E2FSCK_OK. Under some conditions it will print a
230 * message explaining why a check is being forced.
231 */
232static void check_if_skip(e2fsck_t ctx)
233{
234 ext2_filsys fs = ctx->fs;
235 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000236 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500237 long next_check;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500238 int batt = is_on_batt();
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500239 time_t now = time(0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000240
Theodore Ts'od37066a2001-12-21 23:28:54 -0500241 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
242 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000243 return;
244
245 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000246 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000247 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000248 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000249 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000250 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000251 (unsigned) fs->super->s_max_mnt_count)) {
252 reason = _(" has been mounted %u times without being checked");
253 reason_arg = fs->super->s_mnt_count;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500254 if (batt && (fs->super->s_mnt_count <
255 (unsigned) fs->super->s_max_mnt_count*2))
256 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000257 } else if (fs->super->s_checkinterval &&
Theodore Ts'o54434922003-12-07 01:28:50 -0500258 ((now - fs->super->s_lastcheck) >=
259 fs->super->s_checkinterval)) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000260 reason = _(" has gone %u days without being checked");
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500261 reason_arg = (now - fs->super->s_lastcheck)/(3600*24);
Theodore Ts'o54434922003-12-07 01:28:50 -0500262 if (batt && ((now - fs->super->s_lastcheck) <
263 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500264 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000265 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000266 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000267 fputs(ctx->device_name, stdout);
268 printf(reason, reason_arg);
269 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000270 return;
271 }
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500272 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000273 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
274 fs->super->s_inodes_count,
275 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
276 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500277 next_check = 100000;
278 if (fs->super->s_max_mnt_count > 0) {
279 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
280 if (next_check <= 0)
281 next_check = 1;
282 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500283 if ((now - fs->super->s_lastcheck) >= fs->super->s_checkinterval)
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500284 next_check = 1;
285 if (next_check <= 5) {
286 if (next_check == 1)
287 fputs(_(" (check after next mount)"), stdout);
288 else
Theodore Ts'o54434922003-12-07 01:28:50 -0500289 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500290 }
291 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000292 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400293 ctx->fs = NULL;
294 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000295 exit(FSCK_OK);
296}
297
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000298/*
299 * For completion notice
300 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000301struct percent_tbl {
302 int max_pass;
303 int table[32];
304};
305struct percent_tbl e2fsck_tbl = {
306 5, { 0, 70, 90, 92, 95, 100 }
307};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400308static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000309
310static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
311 int max)
312{
313 float percent;
314
315 if (pass <= 0)
316 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000317 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000318 return 100.0;
319 percent = ((float) curr) / ((float) max);
320 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
321 + tbl->table[pass-1]);
322}
323
324extern void e2fsck_clear_progbar(e2fsck_t ctx)
325{
326 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
327 return;
328
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400329 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
330 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400331 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000332 ctx->flags &= ~E2F_FLAG_PROG_BAR;
333}
334
Theodore Ts'o520ead32003-04-19 13:48:27 -0400335int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500336 unsigned int dpynum)
337{
338 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500339 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500340 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500341 struct timeval tv;
342 int dpywidth;
343
344 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
345 return 0;
346
347 /*
348 * Calculate the new progress position. If the
349 * percentage hasn't changed, then we skip out right
350 * away.
351 */
352 if (ctx->progress_last_percent == (int) 10 * percent)
353 return 0;
354 ctx->progress_last_percent = (int) 10 * percent;
355
356 /*
357 * If we've already updated the spinner once within
358 * the last 1/8th of a second, no point doing it
359 * again.
360 */
361 gettimeofday(&tv, NULL);
362 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
363 if ((tick == ctx->progress_last_time) &&
364 (percent != 0.0) && (percent != 100.0))
365 return 0;
366 ctx->progress_last_time = tick;
367
368 /*
369 * Advance the spinner, and note that the progress bar
370 * will be on the screen
371 */
372 ctx->progress_pos = (ctx->progress_pos+1) & 3;
373 ctx->flags |= E2F_FLAG_PROG_BAR;
374
375 dpywidth = 66 - strlen(label);
376 dpywidth = 8 * (dpywidth / 8);
377 if (dpynum)
378 dpywidth -= 8;
379
380 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400381 printf("%s%s: |%s%s", ctx->start_meta, label,
382 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500383 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 if (dpynum)
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400389 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500390 else
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400391 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500392
393 if (percent == 100.0)
394 e2fsck_clear_progbar(ctx);
395 fflush(stdout);
396
397 return 0;
398}
399
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000400static int e2fsck_update_progress(e2fsck_t ctx, int pass,
401 unsigned long cur, unsigned long max)
402{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000403 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000404 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000405
406 if (pass == 0)
407 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000408
409 if (ctx->progress_fd) {
410 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
411 write(ctx->progress_fd, buf, strlen(buf));
412 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000413 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500414 e2fsck_simple_progress(ctx, ctx->device_name,
415 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000416 }
417 return 0;
418}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000419
420#define PATH_SET "PATH=/sbin"
421
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000422static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000423{
424 int fd;
425
426 while (1) {
427 fd = open("/dev/null", O_RDWR);
428 if (fd > 2)
429 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000430 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000431 fprintf(stderr, _("ERROR: Couldn't open "
432 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000433 strerror(errno));
434 break;
435 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000436 }
437 close(fd);
438}
439
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000440#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500441static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000442{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000443 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000444
445 if (!ctx)
446 return;
447
448 ctx->progress = e2fsck_update_progress;
449 ctx->progress_fd = 0;
450}
451
Theodore Ts'o54434922003-12-07 01:28:50 -0500452static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000453{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000454 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000455
456 if (!ctx)
457 return;
458
459 e2fsck_clear_progbar(ctx);
460 ctx->progress = 0;
461}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400462
Theodore Ts'o54434922003-12-07 01:28:50 -0500463static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400464{
465 e2fsck_t ctx = e2fsck_global_ctx;
466
467 if (!ctx)
468 exit(FSCK_CANCELED);
469
470 ctx->flags |= E2F_FLAG_CANCEL;
471}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000472#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000473
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400474static void parse_extended_opts(e2fsck_t ctx, const char *opts)
475{
476 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500477 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400478 int extended_usage = 0;
479
Theodore Ts'of3640932003-03-01 19:47:44 -0500480 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400481 for (token = buf; token && *token; token = next) {
482 p = strchr(token, ',');
483 next = 0;
484 if (p) {
485 *p = 0;
486 next = p+1;
487 }
488 arg = strchr(token, '=');
489 if (arg) {
490 *arg = 0;
491 arg++;
492 }
493 if (strcmp(token, "ea_ver") == 0) {
494 if (!arg) {
495 extended_usage++;
496 continue;
497 }
498 ea_ver = strtoul(arg, &p, 0);
499 if (*p ||
500 ((ea_ver != 1) && (ea_ver != 2))) {
501 fprintf(stderr,
502 _("Invalid EA version.\n"));
503 extended_usage++;
504 continue;
505 }
506 ctx->ext_attr_ver = ea_ver;
507 } else
508 extended_usage++;
509 }
510 if (extended_usage) {
511 fprintf(stderr, _("Extended options are separated by commas, "
512 "and may take an argument which\n"
513 "is set off by an equals ('=') sign. "
514 "Valid raid options are:\n"
515 "\tea_ver=<ea_version (1 or 2)\n\n"));
516 exit(1);
517 }
518}
519
520
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000521static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
522{
523 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000524 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000525#ifdef MTRACE
526 extern void *mallwatch;
527#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000528 e2fsck_t ctx;
529 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000530#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000531 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000532#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400533 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000534
535 retval = e2fsck_allocate_context(&ctx);
536 if (retval)
537 return retval;
538
539 *ret_ctx = ctx;
540
Theodore Ts'o908b7852003-04-16 15:20:13 -0400541 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
542 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400543 if (isatty(0) && isatty(1)) {
544 ctx->interactive = 1;
545 } else {
546 ctx->start_meta[0] = '\001';
547 ctx->stop_meta[0] = '\002';
548 }
549 memset(bar, '=', sizeof(bar)-1);
550 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500552 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000553
554 if (argc && *argv)
555 ctx->program_name = *argv;
556 else
557 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500558 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000559 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000560 case 'C':
561 ctx->progress = e2fsck_update_progress;
562 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000563 if (!ctx->progress_fd)
564 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000565 /* Validate the file descriptor to avoid disasters */
566 fd = dup(ctx->progress_fd);
567 if (fd < 0) {
568 fprintf(stderr,
569 _("Error validating file descriptor %d: %s\n"),
570 ctx->progress_fd,
571 error_message(errno));
572 fatal_error(ctx,
573 _("Invalid completion information file descriptor"));
574 } else
575 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000576 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400577 case 'D':
578 ctx->options |= E2F_OPT_COMPRESS_DIRS;
579 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400580 case 'E':
581 extended_opts = optarg;
582 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000583 case 'p':
584 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500585 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
586 conflict_opt:
587 fatal_error(ctx,
588 _("Only one the options -p/-a, -n or -y may be specified."));
589 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000590 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000591 break;
592 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500593 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
594 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000595 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000596 break;
597 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500598 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
599 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000600 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000601 break;
602 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000603#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000604 if (ctx->options & E2F_OPT_TIME)
605 ctx->options |= E2F_OPT_TIME2;
606 else
607 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000608#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000609 fprintf(stderr, _("The -t option is not "
610 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000611#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 break;
613 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500614 if (cflag++)
615 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000616 ctx->options |= E2F_OPT_CHECKBLOCKS;
617 break;
618 case 'r':
619 /* What we do by default, anyway! */
620 break;
621 case 'b':
622 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400623 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 break;
625 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500626 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000627 break;
628 case 'I':
629 ctx->inode_buffer_blocks = atoi(optarg);
630 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400631 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500632 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400633 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000634 case 'P':
635 ctx->process_inode_size = atoi(optarg);
636 break;
637 case 'L':
638 replace_bad_blocks++;
639 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500640 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000641 break;
642 case 'd':
643 ctx->options |= E2F_OPT_DEBUG;
644 break;
645 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500646 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000647 break;
648 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000649 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000650 break;
651 case 'v':
652 verbose = 1;
653 break;
654 case 'V':
655 show_version_only = 1;
656 break;
657#ifdef MTRACE
658 case 'M':
659 mallwatch = (void *) strtol(optarg, NULL, 0);
660 break;
661#endif
662 case 'N':
663 ctx->device_name = optarg;
664 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000665#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000666 case 's':
667 normalize_swapfs = 1;
668 case 'S':
669 swapfs = 1;
670 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000671#else
672 case 's':
673 case 'S':
674 fprintf(stderr, _("Byte-swapping filesystems "
675 "not compiled in this version "
676 "of e2fsck\n"));
677 exit(1);
678#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500679 case 'k':
680 keep_bad_blocks++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000681 default:
682 usage(ctx);
683 }
684 if (show_version_only)
685 return 0;
686 if (optind != argc - 1)
687 usage(ctx);
688 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400689 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000690 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'of3640932003-03-01 19:47:44 -0500691 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500692 if (!ctx->filesystem_name) {
693 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
694 argv[optind]);
695 fatal_error(ctx, 0);
696 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400697 if (extended_opts)
698 parse_extended_opts(ctx, extended_opts);
699
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000700 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500701 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000702 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000703 com_err("open", errno,
704 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000705 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000706 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000707 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000708 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000709 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000710 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000711 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000712 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000713 }
714 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000715 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000716#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000717 if (swapfs) {
718 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000719 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500720 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000721 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000722 }
723 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000724#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500725 if (cflag && bad_blocks_file) {
726 fprintf(stderr, _("The -c and the -l/-L options may "
727 "not be both used at the same time.\n"));
728 exit(FSCK_USAGE);
729 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000730#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000731 /*
732 * Set up signal action
733 */
734 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400735 sa.sa_handler = signal_cancel;
736 sigaction(SIGINT, &sa, 0);
737 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000738#ifdef SA_RESTART
739 sa.sa_flags = SA_RESTART;
740#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000741 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000742 sa.sa_handler = signal_progress_on;
743 sigaction(SIGUSR1, &sa, 0);
744 sa.sa_handler = signal_progress_off;
745 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000746#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400747
748 /* Update our PATH to include /sbin if we need to run badblocks */
749 if (cflag) {
750 char *oldpath = getenv("PATH");
751 if (oldpath) {
752 char *newpath;
753
754 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
755 strlen (oldpath));
756 if (!newpath)
757 fatal_error(ctx, "Couldn't malloc() newpath");
758 strcpy (newpath, PATH_SET);
759 strcat (newpath, ":");
760 strcat (newpath, oldpath);
761 putenv (newpath);
762 } else
763 putenv (PATH_SET);
764 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500765#ifdef CONFIG_JBD_DEBUG
766 if (getenv("E2FSCK_JBD_DEBUG"))
767 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
768#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000769 return 0;
770}
771
772static const char *my_ver_string = E2FSPROGS_VERSION;
773static const char *my_ver_date = E2FSPROGS_DATE;
774
775int main (int argc, char *argv[])
776{
777 errcode_t retval = 0;
778 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000779 ext2_filsys fs = 0;
780 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000781 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000782 const char *lib_ver_date;
783 int my_ver, lib_ver;
784 e2fsck_t ctx;
785 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000786 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000787
788 clear_problem_context(&pctx);
789#ifdef MTRACE
790 mtrace();
791#endif
792#ifdef MCHECK
793 mcheck(0);
794#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000795#ifdef ENABLE_NLS
796 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500797 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000798 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
799 textdomain(NLS_CAT_NAME);
800#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000801 my_ver = ext2fs_parse_version_string(my_ver_string);
802 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
803 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000804 fprintf( stderr, _("Error: ext2fs library version "
805 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000806 show_version_only++;
807 }
808
809 retval = PRS(argc, argv, &ctx);
810 if (retval) {
811 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000812 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000813 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000814 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000815 reserve_stdio_fds();
816
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000817#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000818 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000819#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000820
821 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400822 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400823 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000824
825 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000826 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000827 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000828 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000829 }
830
831 check_mount(ctx);
832
833 if (!(ctx->options & E2F_OPT_PREEN) &&
834 !(ctx->options & E2F_OPT_NO) &&
835 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400836 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000837 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000838 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839 }
840 ctx->superblock = ctx->use_superblock;
841restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400842#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000843 io_ptr = test_io_manager;
844 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400845#else
846 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000847#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000848 flags = 0;
849 if ((ctx->options & E2F_OPT_READONLY) == 0)
850 flags |= EXT2_FLAG_RW;
851
Theodore Ts'of1a17612001-12-23 22:27:52 -0500852 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000853 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500854 ctx->superblock, ctx->blocksize,
855 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000856 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600857 int blocksize;
858 for (blocksize = EXT2_MIN_BLOCK_SIZE;
859 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000860 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600861 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000862 io_ptr, &fs);
863 if (!retval)
864 break;
865 }
866 } else
867 retval = ext2fs_open(ctx->filesystem_name, flags,
868 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400869 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
870 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000871 ((retval == EXT2_ET_BAD_MAGIC) ||
872 ((retval == 0) && ext2fs_check_desc(fs)))) {
873 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000874 printf(_("%s trying backup blocks...\n"),
875 retval ? _("Couldn't find ext2 superblock,") :
876 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500877 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000878 if (fs)
879 ext2fs_close(fs);
880 goto restart;
881 }
882 }
883 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000884 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000885 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000886 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000887 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000888 "too high for this version of e2fsck.\n"
889 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000890 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000891 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
892 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000893 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000894 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000895 printf(_("You must have %s access to the "
896 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000897 (ctx->options & E2F_OPT_READONLY) ?
898 "r/o" : "r/w");
899 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000900 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000901#ifdef EROFS
902 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000903 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000904 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000905 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000906#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000907 else
908 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000909 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000910 }
911 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000912 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000913 sb = fs->super;
914 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000915 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000916 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000917 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000918 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000919 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000920 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000921
Theodore Ts'o17390c02000-07-07 04:13:21 +0000922 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000923 * Set the device name, which is used whenever we print error
924 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000925 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000926 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000927 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500928 ctx->device_name = string_copy(ctx, sb->s_volume_name,
929 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000930 }
931 if (ctx->device_name == 0)
932 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000933
934 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000935 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000936 */
937 retval = e2fsck_check_ext3_journal(ctx);
938 if (retval) {
939 com_err(ctx->program_name, retval,
940 _("while checking ext3 journal for %s"),
941 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000942 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000943 }
944
Theodore Ts'o9b565752000-12-13 18:50:22 +0000945 /*
946 * Check to see if we need to do ext3-style recovery. If so,
947 * do it, and then restart the fsck.
948 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000949 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000950 if (ctx->options & E2F_OPT_READONLY) {
951 printf(_("Warning: skipping journal recovery "
952 "because doing a read-only filesystem "
953 "check.\n"));
954 io_channel_flush(ctx->fs->io);
955 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500956 if (ctx->flags & E2F_FLAG_RESTARTED) {
957 /*
958 * Whoops, we attempted to run the
959 * journal twice. This should never
960 * happen, unless the hardware or
961 * device driver is being bogus.
962 */
963 com_err(ctx->program_name, 0,
964 _("unable to set superblock flags on %s\n"), ctx->device_name);
965 fatal_error(ctx, 0);
966 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000967 retval = e2fsck_run_ext3_journal(ctx);
968 if (retval) {
969 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000970 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000971 ctx->device_name);
972 fatal_error(ctx, 0);
973 }
974 ext2fs_close(ctx->fs);
975 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500976 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000977 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000978 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000979 }
980
981 /*
982 * Check for compatibility with the feature sets. We need to
983 * be more stringent than ext2fs_open().
984 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000985 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
986 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000987 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
988 "(%s)", ctx->device_name);
989 goto get_newer;
990 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000991 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000992 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
993 "(%s)", ctx->device_name);
994 goto get_newer;
995 }
996#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000997 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000998 com_err(ctx->program_name, 0,
999 _("Warning: compression support is experimental.\n"));
1000#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001001#ifndef ENABLE_HTREE
1002 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1003 com_err(ctx->program_name, 0,
1004 _("E2fsck not compiled with HTREE support,\n\t"
1005 "but filesystem %s has HTREE directories.\n"),
1006 ctx->device_name);
1007 goto get_newer;
1008 }
1009#endif
1010
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001011 /*
1012 * If the user specified a specific superblock, presumably the
1013 * master superblock has been trashed. So we mark the
1014 * superblock as dirty, so it can be written out.
1015 */
1016 if (ctx->superblock &&
1017 !(ctx->options & E2F_OPT_READONLY))
1018 ext2fs_mark_super_dirty(fs);
1019
1020 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001021 * We only update the master superblock because (a) paranoia;
1022 * we don't want to corrupt the backup superblocks, and (b) we
1023 * don't need to update the mount count and last checked
1024 * fields in the backup superblock (the kernel doesn't
1025 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001026 */
1027 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1028
1029 ehandler_init(fs->io);
1030
1031 if (ctx->superblock)
1032 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1033 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001034 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001035 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001036 check_if_skip(ctx);
1037 if (bad_blocks_file)
1038 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1039 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001040 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001041 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001042 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001043#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001044 if (normalize_swapfs) {
1045 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1046 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001047 fprintf(stderr, _("%s: Filesystem byte order "
1048 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001049 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001050 }
1051 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001052 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001053 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001054 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001055 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001056 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001057#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001058
1059 /*
1060 * Mark the system as valid, 'til proven otherwise
1061 */
1062 ext2fs_mark_valid(fs);
1063
1064 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1065 if (retval) {
1066 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001067 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001068 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001069 printf(_("This doesn't bode well,"
1070 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001071 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001072
1073 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001074 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001075 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001076 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001077 retval = e2fsck_reset_context(ctx);
1078 if (retval) {
1079 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001080 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001081 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001082 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001083 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001084 goto restart;
1085 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001086 if (run_result & E2F_FLAG_CANCEL) {
1087 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1088 ctx->device_name : ctx->filesystem_name);
1089 exit_value |= FSCK_CANCELED;
1090 }
1091 if (run_result & E2F_FLAG_ABORT)
1092 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001093
1094#ifdef MTRACE
1095 mtrace_print("Cleanup");
1096#endif
1097 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001098 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001099 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001100 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001101 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001102 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001103 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001104 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001105 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001106 }
1107 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001108 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001109 printf(_("\n%s: ********** WARNING: Filesystem still has "
1110 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001111 exit_value |= FSCK_UNCORRECTED;
1112 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001113 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001114 if (exit_value & FSCK_CANCELED)
1115 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001116 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001117 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001118 if (!(ctx->options & E2F_OPT_READONLY)) {
1119 if (ext2fs_test_valid(fs)) {
1120 if (!(sb->s_state & EXT2_VALID_FS))
1121 exit_value |= FSCK_NONDESTRUCT;
1122 sb->s_state = EXT2_VALID_FS;
1123 } else
1124 sb->s_state &= ~EXT2_VALID_FS;
1125 sb->s_mnt_count = 0;
1126 sb->s_lastcheck = time(NULL);
1127 ext2fs_mark_super_dirty(fs);
1128 }
1129 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001130
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001131 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001132
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001133 ext2fs_close(fs);
1134 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001135 free(ctx->filesystem_name);
1136 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001137 e2fsck_free_context(ctx);
1138
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001139#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001140 if (ctx->options & E2F_OPT_TIME)
1141 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001142#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001143
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001144 return exit_value;
1145}