blob: 43ef4c93f2501f09849d446a83447fad45f079da [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'oc07f9f22004-04-12 00:16:44 -040042#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_DIRENT_H
46#include <dirent.h>
47#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000048
49#include "et/com_err.h"
50#include "e2fsck.h"
51#include "problem.h"
52#include "../version.h"
53
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054/* Command line options */
Theodore Ts'o54a31a32003-08-19 10:08:34 -040055static int swapfs;
56static int normalize_swapfs;
57static int cflag; /* check disk */
58static int show_version_only;
59static int verbose;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060
Theodore Ts'o54a31a32003-08-19 10:08:34 -040061static int replace_bad_blocks;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -050062static int keep_bad_blocks;
Theodore Ts'o54a31a32003-08-19 10:08:34 -040063static char *bad_blocks_file;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000064
Theodore Ts'o243dc312000-08-22 21:37:47 +000065e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
66
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -050067#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
68int journal_enable_debug = -1;
69#endif
70
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071static void usage(e2fsck_t ctx)
72{
73 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050074 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000075 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040076 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
77 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000078 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000079
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000080 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000081 " -p Automatic repair (no questions)\n"
82 " -n Make no changes to the filesystem\n"
83 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050084 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000085 " -f Force checking even if filesystem is marked clean\n"));
86 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000087 " -v Be verbose\n"
88 " -b superblock Use alternative superblock\n"
89 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040090 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000091 " -l bad_blocks_file Add to badblocks list\n"
92 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000093 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000094
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000095 exit(FSCK_USAGE);
96}
97
98static void show_stats(e2fsck_t ctx)
99{
100 ext2_filsys fs = ctx->fs;
101 int inodes, inodes_used, blocks, blocks_used;
102 int dir_links;
103 int num_files, num_links;
104 int frag_percent;
105
106 dir_links = 2 * ctx->fs_directory_count - 1;
107 num_files = ctx->fs_total_count - dir_links;
108 num_links = ctx->fs_links_count - dir_links;
109 inodes = fs->super->s_inodes_count;
110 inodes_used = (fs->super->s_inodes_count -
111 fs->super->s_free_inodes_count);
112 blocks = fs->super->s_blocks_count;
113 blocks_used = (fs->super->s_blocks_count -
114 fs->super->s_free_blocks_count);
115
116 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
117 frag_percent = (frag_percent + 5) / 10;
118
119 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000120 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 ctx->device_name, inodes_used, inodes,
122 frag_percent / 10, frag_percent % 10,
123 blocks_used, blocks);
124 return;
125 }
Theodore Ts'o113e4052003-05-17 21:00:46 -0400126 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
127 inodes_used), inodes_used, 100 * inodes_used / inodes);
128 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
129 "%8d non-contiguous inodes (%0d.%d%%)\n",
130 ctx->fs_fragmented),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000131 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
132 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
133 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Theodore Ts'o113e4052003-05-17 21:00:46 -0400134 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
135 blocks_used),
136 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
137 printf (P_("%8d bad block\n", "%8d bad blocks\n",
138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
139 printf (P_("%8d large file\n", "%8d large files\n",
140 ctx->large_files), ctx->large_files);
141 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
142 ctx->fs_regular_count), ctx->fs_regular_count);
143 printf (P_("%8d directory\n", "%8d directories\n",
144 ctx->fs_directory_count), ctx->fs_directory_count);
145 printf (P_("%8d character device file\n",
146 "%8d character device files\n", ctx->fs_chardev_count),
147 ctx->fs_chardev_count);
148 printf (P_("%8d block device file\n", "%8d block device files\n",
149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
150 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
151 ctx->fs_fifo_count);
152 printf (P_("%8d link\n", "%8d links\n",
153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
155 printf (P_("%8d symbolic link", "%8d symbolic links",
156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
157 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
159 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
160 ctx->fs_sockets_count);
161 printf ("--------\n");
162 printf (P_("%8d file\n", "%8d files\n",
163 ctx->fs_total_count - dir_links),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000164 ctx->fs_total_count - dir_links);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000165}
166
167static void check_mount(e2fsck_t ctx)
168{
169 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500170 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000171
Theodore Ts'oee895132002-11-07 16:16:55 -0500172 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
173 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000174 if (retval) {
175 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000176 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000177 ctx->filesystem_name);
178 return;
179 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400180
181 /*
182 * If the filesystem isn't mounted, or it's the root filesystem
183 * and it's mounted read-only, then everything's fine.
184 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500185 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
186 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
187 (ctx->mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000188 return;
189
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000190 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000191 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000192 return;
193 }
194
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000195 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400196 if (!ctx->interactive)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000197 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000198 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000199 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000200 "SEVERE filesystem damage.\007\007\007\n\n"));
201 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000203 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000204 exit (0);
205 }
206 return;
207}
208
Theodore Ts'o54434922003-12-07 01:28:50 -0500209static int is_on_batt(void)
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500210{
211 FILE *f;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400212 DIR *d;
213 char tmp[80], tmp2[80], fname[80];
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500214 unsigned int acflag;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400215 struct dirent* de;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500216
217 f = fopen("/proc/apm", "r");
218 if (f) {
219 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
220 acflag = 1;
221 fclose(f);
222 return (acflag != 1);
223 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400224 d = opendir("/proc/acpi/ac_adapter");
225 while (d && (de=readdir(d))) {
226 if (!strncmp(".", de->d_name, 1))
227 continue;
228 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
229 de->d_name);
230 f = fopen(fname, "r");
231 if (!f)
232 continue;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500233 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
234 tmp[0] = 0;
235 fclose(f);
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400236 if (strncmp(tmp, "off-line", 8) == 0) {
237 closedir(d);
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500238 return 1;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400239 }
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500240 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400241 closedir(d);
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500242 return 0;
243}
244
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000245/*
246 * This routine checks to see if a filesystem can be skipped; if so,
247 * it will exit with E2FSCK_OK. Under some conditions it will print a
248 * message explaining why a check is being forced.
249 */
250static void check_if_skip(e2fsck_t ctx)
251{
252 ext2_filsys fs = ctx->fs;
253 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000254 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500255 long next_check;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500256 int batt = is_on_batt();
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500257 time_t now = time(0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000258
Theodore Ts'od37066a2001-12-21 23:28:54 -0500259 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
260 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000261 return;
262
263 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000264 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000265 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000266 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000267 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000268 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000269 (unsigned) fs->super->s_max_mnt_count)) {
270 reason = _(" has been mounted %u times without being checked");
271 reason_arg = fs->super->s_mnt_count;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500272 if (batt && (fs->super->s_mnt_count <
273 (unsigned) fs->super->s_max_mnt_count*2))
274 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000275 } else if (fs->super->s_checkinterval &&
Theodore Ts'o54434922003-12-07 01:28:50 -0500276 ((now - fs->super->s_lastcheck) >=
277 fs->super->s_checkinterval)) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000278 reason = _(" has gone %u days without being checked");
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500279 reason_arg = (now - fs->super->s_lastcheck)/(3600*24);
Theodore Ts'o54434922003-12-07 01:28:50 -0500280 if (batt && ((now - fs->super->s_lastcheck) <
281 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500282 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000283 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000284 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000285 fputs(ctx->device_name, stdout);
286 printf(reason, reason_arg);
287 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000288 return;
289 }
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500290 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000291 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
292 fs->super->s_inodes_count,
293 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
294 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500295 next_check = 100000;
296 if (fs->super->s_max_mnt_count > 0) {
297 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
298 if (next_check <= 0)
299 next_check = 1;
300 }
Theodore Ts'o66fbee82004-05-04 20:38:17 -0400301 if (fs->super->s_checkinterval &&
302 ((now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500303 next_check = 1;
304 if (next_check <= 5) {
305 if (next_check == 1)
306 fputs(_(" (check after next mount)"), stdout);
307 else
Theodore Ts'o54434922003-12-07 01:28:50 -0500308 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500309 }
310 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000311 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400312 ctx->fs = NULL;
313 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000314 exit(FSCK_OK);
315}
316
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000317/*
318 * For completion notice
319 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000320struct percent_tbl {
321 int max_pass;
322 int table[32];
323};
324struct percent_tbl e2fsck_tbl = {
325 5, { 0, 70, 90, 92, 95, 100 }
326};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400327static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000328
329static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
330 int max)
331{
332 float percent;
333
334 if (pass <= 0)
335 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000336 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000337 return 100.0;
338 percent = ((float) curr) / ((float) max);
339 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
340 + tbl->table[pass-1]);
341}
342
343extern void e2fsck_clear_progbar(e2fsck_t ctx)
344{
345 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
346 return;
347
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400348 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
349 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400350 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000351 ctx->flags &= ~E2F_FLAG_PROG_BAR;
352}
353
Theodore Ts'o520ead32003-04-19 13:48:27 -0400354int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500355 unsigned int dpynum)
356{
357 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500358 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500359 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500360 struct timeval tv;
361 int dpywidth;
362
363 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
364 return 0;
365
366 /*
367 * Calculate the new progress position. If the
368 * percentage hasn't changed, then we skip out right
369 * away.
370 */
371 if (ctx->progress_last_percent == (int) 10 * percent)
372 return 0;
373 ctx->progress_last_percent = (int) 10 * percent;
374
375 /*
376 * If we've already updated the spinner once within
377 * the last 1/8th of a second, no point doing it
378 * again.
379 */
380 gettimeofday(&tv, NULL);
381 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
382 if ((tick == ctx->progress_last_time) &&
383 (percent != 0.0) && (percent != 100.0))
384 return 0;
385 ctx->progress_last_time = tick;
386
387 /*
388 * Advance the spinner, and note that the progress bar
389 * will be on the screen
390 */
391 ctx->progress_pos = (ctx->progress_pos+1) & 3;
392 ctx->flags |= E2F_FLAG_PROG_BAR;
393
394 dpywidth = 66 - strlen(label);
395 dpywidth = 8 * (dpywidth / 8);
396 if (dpynum)
397 dpywidth -= 8;
398
399 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400400 printf("%s%s: |%s%s", ctx->start_meta, label,
401 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500402 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
403 if (percent == 100.0)
404 fputc('|', stdout);
405 else
406 fputc(spinner[ctx->progress_pos & 3], stdout);
407 if (dpynum)
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400408 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500409 else
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400410 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500411
412 if (percent == 100.0)
413 e2fsck_clear_progbar(ctx);
414 fflush(stdout);
415
416 return 0;
417}
418
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000419static int e2fsck_update_progress(e2fsck_t ctx, int pass,
420 unsigned long cur, unsigned long max)
421{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000422 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000423 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000424
425 if (pass == 0)
426 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000427
428 if (ctx->progress_fd) {
429 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
430 write(ctx->progress_fd, buf, strlen(buf));
431 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000432 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500433 e2fsck_simple_progress(ctx, ctx->device_name,
434 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000435 }
436 return 0;
437}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000438
439#define PATH_SET "PATH=/sbin"
440
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000441static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000442{
443 int fd;
444
445 while (1) {
446 fd = open("/dev/null", O_RDWR);
447 if (fd > 2)
448 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000449 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000450 fprintf(stderr, _("ERROR: Couldn't open "
451 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000452 strerror(errno));
453 break;
454 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000455 }
456 close(fd);
457}
458
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000459#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500460static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000461{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000462 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000463
464 if (!ctx)
465 return;
466
467 ctx->progress = e2fsck_update_progress;
468 ctx->progress_fd = 0;
469}
470
Theodore Ts'o54434922003-12-07 01:28:50 -0500471static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000472{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000473 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000474
475 if (!ctx)
476 return;
477
478 e2fsck_clear_progbar(ctx);
479 ctx->progress = 0;
480}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400481
Theodore Ts'o54434922003-12-07 01:28:50 -0500482static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400483{
484 e2fsck_t ctx = e2fsck_global_ctx;
485
486 if (!ctx)
487 exit(FSCK_CANCELED);
488
489 ctx->flags |= E2F_FLAG_CANCEL;
490}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000491#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000492
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400493static void parse_extended_opts(e2fsck_t ctx, const char *opts)
494{
495 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500496 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400497 int extended_usage = 0;
498
Theodore Ts'of3640932003-03-01 19:47:44 -0500499 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400500 for (token = buf; token && *token; token = next) {
501 p = strchr(token, ',');
502 next = 0;
503 if (p) {
504 *p = 0;
505 next = p+1;
506 }
507 arg = strchr(token, '=');
508 if (arg) {
509 *arg = 0;
510 arg++;
511 }
512 if (strcmp(token, "ea_ver") == 0) {
513 if (!arg) {
514 extended_usage++;
515 continue;
516 }
517 ea_ver = strtoul(arg, &p, 0);
518 if (*p ||
519 ((ea_ver != 1) && (ea_ver != 2))) {
520 fprintf(stderr,
521 _("Invalid EA version.\n"));
522 extended_usage++;
523 continue;
524 }
525 ctx->ext_attr_ver = ea_ver;
526 } else
527 extended_usage++;
528 }
529 if (extended_usage) {
530 fprintf(stderr, _("Extended options are separated by commas, "
531 "and may take an argument which\n"
532 "is set off by an equals ('=') sign. "
533 "Valid raid options are:\n"
534 "\tea_ver=<ea_version (1 or 2)\n\n"));
535 exit(1);
536 }
537}
538
539
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000540static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
541{
542 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000543 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000544#ifdef MTRACE
545 extern void *mallwatch;
546#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000547 e2fsck_t ctx;
548 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000549#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000550 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000551#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400552 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000553
554 retval = e2fsck_allocate_context(&ctx);
555 if (retval)
556 return retval;
557
558 *ret_ctx = ctx;
559
Theodore Ts'o908b7852003-04-16 15:20:13 -0400560 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
561 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400562 if (isatty(0) && isatty(1)) {
563 ctx->interactive = 1;
564 } else {
565 ctx->start_meta[0] = '\001';
566 ctx->stop_meta[0] = '\002';
567 }
568 memset(bar, '=', sizeof(bar)-1);
569 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000570 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500571 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000572
573 if (argc && *argv)
574 ctx->program_name = *argv;
575 else
576 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500577 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 +0000578 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000579 case 'C':
580 ctx->progress = e2fsck_update_progress;
581 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000582 if (!ctx->progress_fd)
583 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000584 /* Validate the file descriptor to avoid disasters */
585 fd = dup(ctx->progress_fd);
586 if (fd < 0) {
587 fprintf(stderr,
588 _("Error validating file descriptor %d: %s\n"),
589 ctx->progress_fd,
590 error_message(errno));
591 fatal_error(ctx,
592 _("Invalid completion information file descriptor"));
593 } else
594 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000595 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400596 case 'D':
597 ctx->options |= E2F_OPT_COMPRESS_DIRS;
598 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400599 case 'E':
600 extended_opts = optarg;
601 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000602 case 'p':
603 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500604 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
605 conflict_opt:
606 fatal_error(ctx,
607 _("Only one the options -p/-a, -n or -y may be specified."));
608 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000609 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000610 break;
611 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500612 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
613 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000614 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000615 break;
616 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500617 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
618 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000620 break;
621 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000622#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000623 if (ctx->options & E2F_OPT_TIME)
624 ctx->options |= E2F_OPT_TIME2;
625 else
626 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000627#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000628 fprintf(stderr, _("The -t option is not "
629 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000630#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000631 break;
632 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500633 if (cflag++)
634 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000635 ctx->options |= E2F_OPT_CHECKBLOCKS;
636 break;
637 case 'r':
638 /* What we do by default, anyway! */
639 break;
640 case 'b':
641 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400642 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000643 break;
644 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500645 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000646 break;
647 case 'I':
648 ctx->inode_buffer_blocks = atoi(optarg);
649 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400650 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500651 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400652 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000653 case 'P':
654 ctx->process_inode_size = atoi(optarg);
655 break;
656 case 'L':
657 replace_bad_blocks++;
658 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500659 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000660 break;
661 case 'd':
662 ctx->options |= E2F_OPT_DEBUG;
663 break;
664 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500665 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000666 break;
667 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000669 break;
670 case 'v':
671 verbose = 1;
672 break;
673 case 'V':
674 show_version_only = 1;
675 break;
676#ifdef MTRACE
677 case 'M':
678 mallwatch = (void *) strtol(optarg, NULL, 0);
679 break;
680#endif
681 case 'N':
682 ctx->device_name = optarg;
683 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000684#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000685 case 's':
686 normalize_swapfs = 1;
687 case 'S':
688 swapfs = 1;
689 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000690#else
691 case 's':
692 case 'S':
693 fprintf(stderr, _("Byte-swapping filesystems "
694 "not compiled in this version "
695 "of e2fsck\n"));
696 exit(1);
697#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500698 case 'k':
699 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500700 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000701 default:
702 usage(ctx);
703 }
704 if (show_version_only)
705 return 0;
706 if (optind != argc - 1)
707 usage(ctx);
708 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400709 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000710 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'of3640932003-03-01 19:47:44 -0500711 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500712 if (!ctx->filesystem_name) {
713 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
714 argv[optind]);
715 fatal_error(ctx, 0);
716 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400717 if (extended_opts)
718 parse_extended_opts(ctx, extended_opts);
719
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000720 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500721 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000722 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000723 com_err("open", errno,
724 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000725 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000726 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000727 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000728 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000729 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000730 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000731 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000732 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000733 }
734 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000736#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000737 if (swapfs) {
738 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000739 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500740 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000741 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000742 }
743 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000744#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500745 if (cflag && bad_blocks_file) {
746 fprintf(stderr, _("The -c and the -l/-L options may "
747 "not be both used at the same time.\n"));
748 exit(FSCK_USAGE);
749 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000750#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000751 /*
752 * Set up signal action
753 */
754 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400755 sa.sa_handler = signal_cancel;
756 sigaction(SIGINT, &sa, 0);
757 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000758#ifdef SA_RESTART
759 sa.sa_flags = SA_RESTART;
760#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000761 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000762 sa.sa_handler = signal_progress_on;
763 sigaction(SIGUSR1, &sa, 0);
764 sa.sa_handler = signal_progress_off;
765 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000766#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400767
768 /* Update our PATH to include /sbin if we need to run badblocks */
769 if (cflag) {
770 char *oldpath = getenv("PATH");
771 if (oldpath) {
772 char *newpath;
773
774 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
775 strlen (oldpath));
776 if (!newpath)
777 fatal_error(ctx, "Couldn't malloc() newpath");
778 strcpy (newpath, PATH_SET);
779 strcat (newpath, ":");
780 strcat (newpath, oldpath);
781 putenv (newpath);
782 } else
783 putenv (PATH_SET);
784 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500785#ifdef CONFIG_JBD_DEBUG
786 if (getenv("E2FSCK_JBD_DEBUG"))
787 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
788#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000789 return 0;
790}
791
792static const char *my_ver_string = E2FSPROGS_VERSION;
793static const char *my_ver_date = E2FSPROGS_DATE;
794
795int main (int argc, char *argv[])
796{
797 errcode_t retval = 0;
798 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000799 ext2_filsys fs = 0;
800 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000801 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000802 const char *lib_ver_date;
803 int my_ver, lib_ver;
804 e2fsck_t ctx;
805 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000806 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000807
808 clear_problem_context(&pctx);
809#ifdef MTRACE
810 mtrace();
811#endif
812#ifdef MCHECK
813 mcheck(0);
814#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000815#ifdef ENABLE_NLS
816 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500817 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000818 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
819 textdomain(NLS_CAT_NAME);
820#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000821 my_ver = ext2fs_parse_version_string(my_ver_string);
822 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
823 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000824 fprintf( stderr, _("Error: ext2fs library version "
825 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000826 show_version_only++;
827 }
828
829 retval = PRS(argc, argv, &ctx);
830 if (retval) {
831 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000832 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000833 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000834 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000835 reserve_stdio_fds();
836
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000837#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000838 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000839#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000840
841 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400842 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400843 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000844
845 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000846 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000847 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000848 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000849 }
850
851 check_mount(ctx);
852
853 if (!(ctx->options & E2F_OPT_PREEN) &&
854 !(ctx->options & E2F_OPT_NO) &&
855 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400856 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000857 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000858 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000859 }
860 ctx->superblock = ctx->use_superblock;
861restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400862#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000863 io_ptr = test_io_manager;
864 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400865#else
866 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000867#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000868 flags = 0;
869 if ((ctx->options & E2F_OPT_READONLY) == 0)
870 flags |= EXT2_FLAG_RW;
871
Theodore Ts'of1a17612001-12-23 22:27:52 -0500872 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000873 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500874 ctx->superblock, ctx->blocksize,
875 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000876 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600877 int blocksize;
878 for (blocksize = EXT2_MIN_BLOCK_SIZE;
879 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000880 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600881 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000882 io_ptr, &fs);
883 if (!retval)
884 break;
885 }
886 } else
887 retval = ext2fs_open(ctx->filesystem_name, flags,
888 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400889 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
890 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000891 ((retval == EXT2_ET_BAD_MAGIC) ||
892 ((retval == 0) && ext2fs_check_desc(fs)))) {
893 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000894 printf(_("%s trying backup blocks...\n"),
895 retval ? _("Couldn't find ext2 superblock,") :
896 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500897 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000898 if (fs)
899 ext2fs_close(fs);
900 goto restart;
901 }
902 }
903 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000904 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000905 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000906 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000907 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000908 "too high for this version of e2fsck.\n"
909 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000910 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000911 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
912 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000913 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000914 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000915 printf(_("You must have %s access to the "
916 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000917 (ctx->options & E2F_OPT_READONLY) ?
918 "r/o" : "r/w");
919 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000920 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000921#ifdef EROFS
922 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000923 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000924 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000925 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000926#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000927 else
928 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000929 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000930 }
931 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000932 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000933 sb = fs->super;
934 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000935 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000936 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000937 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000938 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000939 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000940 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000941
Theodore Ts'o17390c02000-07-07 04:13:21 +0000942 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000943 * Set the device name, which is used whenever we print error
944 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000945 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000946 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000947 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500948 ctx->device_name = string_copy(ctx, sb->s_volume_name,
949 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000950 }
951 if (ctx->device_name == 0)
952 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000953
954 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000955 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000956 */
957 retval = e2fsck_check_ext3_journal(ctx);
958 if (retval) {
959 com_err(ctx->program_name, retval,
960 _("while checking ext3 journal for %s"),
961 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000962 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000963 }
964
Theodore Ts'o9b565752000-12-13 18:50:22 +0000965 /*
966 * Check to see if we need to do ext3-style recovery. If so,
967 * do it, and then restart the fsck.
968 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000969 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000970 if (ctx->options & E2F_OPT_READONLY) {
971 printf(_("Warning: skipping journal recovery "
972 "because doing a read-only filesystem "
973 "check.\n"));
974 io_channel_flush(ctx->fs->io);
975 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500976 if (ctx->flags & E2F_FLAG_RESTARTED) {
977 /*
978 * Whoops, we attempted to run the
979 * journal twice. This should never
980 * happen, unless the hardware or
981 * device driver is being bogus.
982 */
983 com_err(ctx->program_name, 0,
984 _("unable to set superblock flags on %s\n"), ctx->device_name);
985 fatal_error(ctx, 0);
986 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000987 retval = e2fsck_run_ext3_journal(ctx);
988 if (retval) {
989 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000990 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000991 ctx->device_name);
992 fatal_error(ctx, 0);
993 }
994 ext2fs_close(ctx->fs);
995 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500996 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000997 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000998 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000999 }
1000
1001 /*
1002 * Check for compatibility with the feature sets. We need to
1003 * be more stringent than ext2fs_open().
1004 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001005 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1006 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001007 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1008 "(%s)", ctx->device_name);
1009 goto get_newer;
1010 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001011 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001012 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1013 "(%s)", ctx->device_name);
1014 goto get_newer;
1015 }
1016#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001017 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001018 com_err(ctx->program_name, 0,
1019 _("Warning: compression support is experimental.\n"));
1020#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001021#ifndef ENABLE_HTREE
1022 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1023 com_err(ctx->program_name, 0,
1024 _("E2fsck not compiled with HTREE support,\n\t"
1025 "but filesystem %s has HTREE directories.\n"),
1026 ctx->device_name);
1027 goto get_newer;
1028 }
1029#endif
1030
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001031 /*
1032 * If the user specified a specific superblock, presumably the
1033 * master superblock has been trashed. So we mark the
1034 * superblock as dirty, so it can be written out.
1035 */
1036 if (ctx->superblock &&
1037 !(ctx->options & E2F_OPT_READONLY))
1038 ext2fs_mark_super_dirty(fs);
1039
1040 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001041 * We only update the master superblock because (a) paranoia;
1042 * we don't want to corrupt the backup superblocks, and (b) we
1043 * don't need to update the mount count and last checked
1044 * fields in the backup superblock (the kernel doesn't
1045 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001046 */
1047 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1048
1049 ehandler_init(fs->io);
1050
1051 if (ctx->superblock)
1052 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1053 check_super_block(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'o1b6bf171997-10-03 17:48:10 +00001056 check_if_skip(ctx);
1057 if (bad_blocks_file)
1058 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1059 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001060 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001061 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001062 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001063#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001064 if (normalize_swapfs) {
1065 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1066 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001067 fprintf(stderr, _("%s: Filesystem byte order "
1068 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001069 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001070 }
1071 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001072 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001073 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001074 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001075 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001076 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001077#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001078
1079 /*
1080 * Mark the system as valid, 'til proven otherwise
1081 */
1082 ext2fs_mark_valid(fs);
1083
1084 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1085 if (retval) {
1086 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001087 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001088 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001089 printf(_("This doesn't bode well,"
1090 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001091 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001092
1093 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001094 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001095 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001096 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001097 retval = e2fsck_reset_context(ctx);
1098 if (retval) {
1099 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001100 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001101 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001102 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001103 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001104 goto restart;
1105 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001106 if (run_result & E2F_FLAG_CANCEL) {
1107 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1108 ctx->device_name : ctx->filesystem_name);
1109 exit_value |= FSCK_CANCELED;
1110 }
1111 if (run_result & E2F_FLAG_ABORT)
1112 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001113
1114#ifdef MTRACE
1115 mtrace_print("Cleanup");
1116#endif
1117 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001118 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001119 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001120 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001121 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001122 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001123 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001124 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001125 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001126 }
1127 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001128 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001129 printf(_("\n%s: ********** WARNING: Filesystem still has "
1130 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001131 exit_value |= FSCK_UNCORRECTED;
1132 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001133 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001134 if (exit_value & FSCK_CANCELED)
1135 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001136 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001137 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001138 if (!(ctx->options & E2F_OPT_READONLY)) {
1139 if (ext2fs_test_valid(fs)) {
1140 if (!(sb->s_state & EXT2_VALID_FS))
1141 exit_value |= FSCK_NONDESTRUCT;
1142 sb->s_state = EXT2_VALID_FS;
1143 } else
1144 sb->s_state &= ~EXT2_VALID_FS;
1145 sb->s_mnt_count = 0;
1146 sb->s_lastcheck = time(NULL);
1147 ext2fs_mark_super_dirty(fs);
1148 }
1149 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001150
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001151 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001152
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001153 ext2fs_close(fs);
1154 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001155 free(ctx->filesystem_name);
1156 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001157 e2fsck_free_context(ctx);
1158
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001159#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001160 if (ctx->options & E2F_OPT_TIME)
1161 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001162#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001163
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001164 return exit_value;
1165}