blob: 0f9f696396e548387082edbc5eec803bea4841ff [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'o54434922003-12-07 01:28:50 -0500301 if ((now - fs->super->s_lastcheck) >= fs->super->s_checkinterval)
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500302 next_check = 1;
303 if (next_check <= 5) {
304 if (next_check == 1)
305 fputs(_(" (check after next mount)"), stdout);
306 else
Theodore Ts'o54434922003-12-07 01:28:50 -0500307 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500308 }
309 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000310 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400311 ctx->fs = NULL;
312 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000313 exit(FSCK_OK);
314}
315
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000316/*
317 * For completion notice
318 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000319struct percent_tbl {
320 int max_pass;
321 int table[32];
322};
323struct percent_tbl e2fsck_tbl = {
324 5, { 0, 70, 90, 92, 95, 100 }
325};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400326static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000327
328static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
329 int max)
330{
331 float percent;
332
333 if (pass <= 0)
334 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000335 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000336 return 100.0;
337 percent = ((float) curr) / ((float) max);
338 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
339 + tbl->table[pass-1]);
340}
341
342extern void e2fsck_clear_progbar(e2fsck_t ctx)
343{
344 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
345 return;
346
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400347 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
348 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400349 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000350 ctx->flags &= ~E2F_FLAG_PROG_BAR;
351}
352
Theodore Ts'o520ead32003-04-19 13:48:27 -0400353int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500354 unsigned int dpynum)
355{
356 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500357 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500358 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500359 struct timeval tv;
360 int dpywidth;
361
362 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
363 return 0;
364
365 /*
366 * Calculate the new progress position. If the
367 * percentage hasn't changed, then we skip out right
368 * away.
369 */
370 if (ctx->progress_last_percent == (int) 10 * percent)
371 return 0;
372 ctx->progress_last_percent = (int) 10 * percent;
373
374 /*
375 * If we've already updated the spinner once within
376 * the last 1/8th of a second, no point doing it
377 * again.
378 */
379 gettimeofday(&tv, NULL);
380 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
381 if ((tick == ctx->progress_last_time) &&
382 (percent != 0.0) && (percent != 100.0))
383 return 0;
384 ctx->progress_last_time = tick;
385
386 /*
387 * Advance the spinner, and note that the progress bar
388 * will be on the screen
389 */
390 ctx->progress_pos = (ctx->progress_pos+1) & 3;
391 ctx->flags |= E2F_FLAG_PROG_BAR;
392
393 dpywidth = 66 - strlen(label);
394 dpywidth = 8 * (dpywidth / 8);
395 if (dpynum)
396 dpywidth -= 8;
397
398 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400399 printf("%s%s: |%s%s", ctx->start_meta, label,
400 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500401 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
402 if (percent == 100.0)
403 fputc('|', stdout);
404 else
405 fputc(spinner[ctx->progress_pos & 3], stdout);
406 if (dpynum)
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400407 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500408 else
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400409 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500410
411 if (percent == 100.0)
412 e2fsck_clear_progbar(ctx);
413 fflush(stdout);
414
415 return 0;
416}
417
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000418static int e2fsck_update_progress(e2fsck_t ctx, int pass,
419 unsigned long cur, unsigned long max)
420{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000421 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000422 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000423
424 if (pass == 0)
425 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000426
427 if (ctx->progress_fd) {
428 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
429 write(ctx->progress_fd, buf, strlen(buf));
430 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000431 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500432 e2fsck_simple_progress(ctx, ctx->device_name,
433 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000434 }
435 return 0;
436}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000437
438#define PATH_SET "PATH=/sbin"
439
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000440static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000441{
442 int fd;
443
444 while (1) {
445 fd = open("/dev/null", O_RDWR);
446 if (fd > 2)
447 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000448 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000449 fprintf(stderr, _("ERROR: Couldn't open "
450 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000451 strerror(errno));
452 break;
453 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000454 }
455 close(fd);
456}
457
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000458#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500459static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000460{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000461 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000462
463 if (!ctx)
464 return;
465
466 ctx->progress = e2fsck_update_progress;
467 ctx->progress_fd = 0;
468}
469
Theodore Ts'o54434922003-12-07 01:28:50 -0500470static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000471{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000472 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000473
474 if (!ctx)
475 return;
476
477 e2fsck_clear_progbar(ctx);
478 ctx->progress = 0;
479}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400480
Theodore Ts'o54434922003-12-07 01:28:50 -0500481static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400482{
483 e2fsck_t ctx = e2fsck_global_ctx;
484
485 if (!ctx)
486 exit(FSCK_CANCELED);
487
488 ctx->flags |= E2F_FLAG_CANCEL;
489}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000490#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000491
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400492static void parse_extended_opts(e2fsck_t ctx, const char *opts)
493{
494 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500495 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400496 int extended_usage = 0;
497
Theodore Ts'of3640932003-03-01 19:47:44 -0500498 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400499 for (token = buf; token && *token; token = next) {
500 p = strchr(token, ',');
501 next = 0;
502 if (p) {
503 *p = 0;
504 next = p+1;
505 }
506 arg = strchr(token, '=');
507 if (arg) {
508 *arg = 0;
509 arg++;
510 }
511 if (strcmp(token, "ea_ver") == 0) {
512 if (!arg) {
513 extended_usage++;
514 continue;
515 }
516 ea_ver = strtoul(arg, &p, 0);
517 if (*p ||
518 ((ea_ver != 1) && (ea_ver != 2))) {
519 fprintf(stderr,
520 _("Invalid EA version.\n"));
521 extended_usage++;
522 continue;
523 }
524 ctx->ext_attr_ver = ea_ver;
525 } else
526 extended_usage++;
527 }
528 if (extended_usage) {
529 fprintf(stderr, _("Extended options are separated by commas, "
530 "and may take an argument which\n"
531 "is set off by an equals ('=') sign. "
532 "Valid raid options are:\n"
533 "\tea_ver=<ea_version (1 or 2)\n\n"));
534 exit(1);
535 }
536}
537
538
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000539static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
540{
541 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000542 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000543#ifdef MTRACE
544 extern void *mallwatch;
545#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000546 e2fsck_t ctx;
547 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000548#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000549 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000550#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400551 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000552
553 retval = e2fsck_allocate_context(&ctx);
554 if (retval)
555 return retval;
556
557 *ret_ctx = ctx;
558
Theodore Ts'o908b7852003-04-16 15:20:13 -0400559 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
560 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400561 if (isatty(0) && isatty(1)) {
562 ctx->interactive = 1;
563 } else {
564 ctx->start_meta[0] = '\001';
565 ctx->stop_meta[0] = '\002';
566 }
567 memset(bar, '=', sizeof(bar)-1);
568 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000569 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500570 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000571
572 if (argc && *argv)
573 ctx->program_name = *argv;
574 else
575 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500576 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 +0000577 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000578 case 'C':
579 ctx->progress = e2fsck_update_progress;
580 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000581 if (!ctx->progress_fd)
582 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000583 /* Validate the file descriptor to avoid disasters */
584 fd = dup(ctx->progress_fd);
585 if (fd < 0) {
586 fprintf(stderr,
587 _("Error validating file descriptor %d: %s\n"),
588 ctx->progress_fd,
589 error_message(errno));
590 fatal_error(ctx,
591 _("Invalid completion information file descriptor"));
592 } else
593 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000594 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400595 case 'D':
596 ctx->options |= E2F_OPT_COMPRESS_DIRS;
597 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400598 case 'E':
599 extended_opts = optarg;
600 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000601 case 'p':
602 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500603 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
604 conflict_opt:
605 fatal_error(ctx,
606 _("Only one the options -p/-a, -n or -y may be specified."));
607 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000608 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000609 break;
610 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500611 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
612 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000613 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000614 break;
615 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500616 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
617 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000618 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 break;
620 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000621#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000622 if (ctx->options & E2F_OPT_TIME)
623 ctx->options |= E2F_OPT_TIME2;
624 else
625 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000626#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000627 fprintf(stderr, _("The -t option is not "
628 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000629#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000630 break;
631 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500632 if (cflag++)
633 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000634 ctx->options |= E2F_OPT_CHECKBLOCKS;
635 break;
636 case 'r':
637 /* What we do by default, anyway! */
638 break;
639 case 'b':
640 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400641 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000642 break;
643 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500644 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000645 break;
646 case 'I':
647 ctx->inode_buffer_blocks = atoi(optarg);
648 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400649 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500650 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400651 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000652 case 'P':
653 ctx->process_inode_size = atoi(optarg);
654 break;
655 case 'L':
656 replace_bad_blocks++;
657 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500658 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000659 break;
660 case 'd':
661 ctx->options |= E2F_OPT_DEBUG;
662 break;
663 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500664 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665 break;
666 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000667 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 break;
669 case 'v':
670 verbose = 1;
671 break;
672 case 'V':
673 show_version_only = 1;
674 break;
675#ifdef MTRACE
676 case 'M':
677 mallwatch = (void *) strtol(optarg, NULL, 0);
678 break;
679#endif
680 case 'N':
681 ctx->device_name = optarg;
682 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000683#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684 case 's':
685 normalize_swapfs = 1;
686 case 'S':
687 swapfs = 1;
688 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000689#else
690 case 's':
691 case 'S':
692 fprintf(stderr, _("Byte-swapping filesystems "
693 "not compiled in this version "
694 "of e2fsck\n"));
695 exit(1);
696#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500697 case 'k':
698 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500699 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700 default:
701 usage(ctx);
702 }
703 if (show_version_only)
704 return 0;
705 if (optind != argc - 1)
706 usage(ctx);
707 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400708 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000709 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'of3640932003-03-01 19:47:44 -0500710 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500711 if (!ctx->filesystem_name) {
712 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
713 argv[optind]);
714 fatal_error(ctx, 0);
715 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400716 if (extended_opts)
717 parse_extended_opts(ctx, extended_opts);
718
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000719 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500720 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000721 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000722 com_err("open", errno,
723 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000724 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000725 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000726 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000727 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000728 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000729 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000730 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000731 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000732 }
733 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000734 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000735#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000736 if (swapfs) {
737 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000738 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500739 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000740 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000741 }
742 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000743#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500744 if (cflag && bad_blocks_file) {
745 fprintf(stderr, _("The -c and the -l/-L options may "
746 "not be both used at the same time.\n"));
747 exit(FSCK_USAGE);
748 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000749#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000750 /*
751 * Set up signal action
752 */
753 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400754 sa.sa_handler = signal_cancel;
755 sigaction(SIGINT, &sa, 0);
756 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000757#ifdef SA_RESTART
758 sa.sa_flags = SA_RESTART;
759#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000760 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000761 sa.sa_handler = signal_progress_on;
762 sigaction(SIGUSR1, &sa, 0);
763 sa.sa_handler = signal_progress_off;
764 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000765#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400766
767 /* Update our PATH to include /sbin if we need to run badblocks */
768 if (cflag) {
769 char *oldpath = getenv("PATH");
770 if (oldpath) {
771 char *newpath;
772
773 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
774 strlen (oldpath));
775 if (!newpath)
776 fatal_error(ctx, "Couldn't malloc() newpath");
777 strcpy (newpath, PATH_SET);
778 strcat (newpath, ":");
779 strcat (newpath, oldpath);
780 putenv (newpath);
781 } else
782 putenv (PATH_SET);
783 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500784#ifdef CONFIG_JBD_DEBUG
785 if (getenv("E2FSCK_JBD_DEBUG"))
786 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
787#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000788 return 0;
789}
790
791static const char *my_ver_string = E2FSPROGS_VERSION;
792static const char *my_ver_date = E2FSPROGS_DATE;
793
794int main (int argc, char *argv[])
795{
796 errcode_t retval = 0;
797 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000798 ext2_filsys fs = 0;
799 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000800 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000801 const char *lib_ver_date;
802 int my_ver, lib_ver;
803 e2fsck_t ctx;
804 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000805 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000806
807 clear_problem_context(&pctx);
808#ifdef MTRACE
809 mtrace();
810#endif
811#ifdef MCHECK
812 mcheck(0);
813#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000814#ifdef ENABLE_NLS
815 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500816 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000817 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
818 textdomain(NLS_CAT_NAME);
819#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000820 my_ver = ext2fs_parse_version_string(my_ver_string);
821 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
822 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000823 fprintf( stderr, _("Error: ext2fs library version "
824 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000825 show_version_only++;
826 }
827
828 retval = PRS(argc, argv, &ctx);
829 if (retval) {
830 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000831 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000832 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000833 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000834 reserve_stdio_fds();
835
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000836#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000837 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000838#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839
840 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400841 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400842 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000843
844 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000845 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000846 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000847 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000848 }
849
850 check_mount(ctx);
851
852 if (!(ctx->options & E2F_OPT_PREEN) &&
853 !(ctx->options & E2F_OPT_NO) &&
854 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400855 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000856 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000857 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000858 }
859 ctx->superblock = ctx->use_superblock;
860restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400861#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000862 io_ptr = test_io_manager;
863 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400864#else
865 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000866#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000867 flags = 0;
868 if ((ctx->options & E2F_OPT_READONLY) == 0)
869 flags |= EXT2_FLAG_RW;
870
Theodore Ts'of1a17612001-12-23 22:27:52 -0500871 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000872 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500873 ctx->superblock, ctx->blocksize,
874 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000875 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600876 int blocksize;
877 for (blocksize = EXT2_MIN_BLOCK_SIZE;
878 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000879 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600880 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000881 io_ptr, &fs);
882 if (!retval)
883 break;
884 }
885 } else
886 retval = ext2fs_open(ctx->filesystem_name, flags,
887 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400888 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
889 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000890 ((retval == EXT2_ET_BAD_MAGIC) ||
891 ((retval == 0) && ext2fs_check_desc(fs)))) {
892 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000893 printf(_("%s trying backup blocks...\n"),
894 retval ? _("Couldn't find ext2 superblock,") :
895 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500896 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000897 if (fs)
898 ext2fs_close(fs);
899 goto restart;
900 }
901 }
902 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000903 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000904 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000905 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000906 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000907 "too high for this version of e2fsck.\n"
908 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000909 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000910 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
911 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000912 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000913 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000914 printf(_("You must have %s access to the "
915 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000916 (ctx->options & E2F_OPT_READONLY) ?
917 "r/o" : "r/w");
918 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000919 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000920#ifdef EROFS
921 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000922 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000923 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000924 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000925#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000926 else
927 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000928 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000929 }
930 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000931 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000932 sb = fs->super;
933 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000934 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000935 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000936 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000937 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000938 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000939 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000940
Theodore Ts'o17390c02000-07-07 04:13:21 +0000941 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000942 * Set the device name, which is used whenever we print error
943 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000944 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000945 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000946 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500947 ctx->device_name = string_copy(ctx, sb->s_volume_name,
948 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000949 }
950 if (ctx->device_name == 0)
951 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000952
953 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000954 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000955 */
956 retval = e2fsck_check_ext3_journal(ctx);
957 if (retval) {
958 com_err(ctx->program_name, retval,
959 _("while checking ext3 journal for %s"),
960 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000961 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000962 }
963
Theodore Ts'o9b565752000-12-13 18:50:22 +0000964 /*
965 * Check to see if we need to do ext3-style recovery. If so,
966 * do it, and then restart the fsck.
967 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000968 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000969 if (ctx->options & E2F_OPT_READONLY) {
970 printf(_("Warning: skipping journal recovery "
971 "because doing a read-only filesystem "
972 "check.\n"));
973 io_channel_flush(ctx->fs->io);
974 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500975 if (ctx->flags & E2F_FLAG_RESTARTED) {
976 /*
977 * Whoops, we attempted to run the
978 * journal twice. This should never
979 * happen, unless the hardware or
980 * device driver is being bogus.
981 */
982 com_err(ctx->program_name, 0,
983 _("unable to set superblock flags on %s\n"), ctx->device_name);
984 fatal_error(ctx, 0);
985 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000986 retval = e2fsck_run_ext3_journal(ctx);
987 if (retval) {
988 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000989 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000990 ctx->device_name);
991 fatal_error(ctx, 0);
992 }
993 ext2fs_close(ctx->fs);
994 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500995 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000996 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000997 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000998 }
999
1000 /*
1001 * Check for compatibility with the feature sets. We need to
1002 * be more stringent than ext2fs_open().
1003 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001004 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1005 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001006 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1007 "(%s)", ctx->device_name);
1008 goto get_newer;
1009 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001010 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001011 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1012 "(%s)", ctx->device_name);
1013 goto get_newer;
1014 }
1015#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001016 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001017 com_err(ctx->program_name, 0,
1018 _("Warning: compression support is experimental.\n"));
1019#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001020#ifndef ENABLE_HTREE
1021 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1022 com_err(ctx->program_name, 0,
1023 _("E2fsck not compiled with HTREE support,\n\t"
1024 "but filesystem %s has HTREE directories.\n"),
1025 ctx->device_name);
1026 goto get_newer;
1027 }
1028#endif
1029
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001030 /*
1031 * If the user specified a specific superblock, presumably the
1032 * master superblock has been trashed. So we mark the
1033 * superblock as dirty, so it can be written out.
1034 */
1035 if (ctx->superblock &&
1036 !(ctx->options & E2F_OPT_READONLY))
1037 ext2fs_mark_super_dirty(fs);
1038
1039 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001040 * We only update the master superblock because (a) paranoia;
1041 * we don't want to corrupt the backup superblocks, and (b) we
1042 * don't need to update the mount count and last checked
1043 * fields in the backup superblock (the kernel doesn't
1044 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001045 */
1046 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1047
1048 ehandler_init(fs->io);
1049
1050 if (ctx->superblock)
1051 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1052 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001053 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001054 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001055 check_if_skip(ctx);
1056 if (bad_blocks_file)
1057 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1058 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001059 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001060 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001061 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001062#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001063 if (normalize_swapfs) {
1064 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1065 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001066 fprintf(stderr, _("%s: Filesystem byte order "
1067 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001068 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001069 }
1070 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001071 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001072 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001073 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001074 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001075 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001076#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001077
1078 /*
1079 * Mark the system as valid, 'til proven otherwise
1080 */
1081 ext2fs_mark_valid(fs);
1082
1083 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1084 if (retval) {
1085 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001086 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001087 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001088 printf(_("This doesn't bode well,"
1089 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001090 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001091
1092 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001093 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001094 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001095 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001096 retval = e2fsck_reset_context(ctx);
1097 if (retval) {
1098 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001099 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001100 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001101 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001102 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001103 goto restart;
1104 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001105 if (run_result & E2F_FLAG_CANCEL) {
1106 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1107 ctx->device_name : ctx->filesystem_name);
1108 exit_value |= FSCK_CANCELED;
1109 }
1110 if (run_result & E2F_FLAG_ABORT)
1111 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001112
1113#ifdef MTRACE
1114 mtrace_print("Cleanup");
1115#endif
1116 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001117 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001118 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001119 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001120 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001121 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001122 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001123 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001124 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001125 }
1126 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001127 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001128 printf(_("\n%s: ********** WARNING: Filesystem still has "
1129 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001130 exit_value |= FSCK_UNCORRECTED;
1131 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001132 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001133 if (exit_value & FSCK_CANCELED)
1134 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001135 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001136 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001137 if (!(ctx->options & E2F_OPT_READONLY)) {
1138 if (ext2fs_test_valid(fs)) {
1139 if (!(sb->s_state & EXT2_VALID_FS))
1140 exit_value |= FSCK_NONDESTRUCT;
1141 sb->s_state = EXT2_VALID_FS;
1142 } else
1143 sb->s_state &= ~EXT2_VALID_FS;
1144 sb->s_mnt_count = 0;
1145 sb->s_lastcheck = time(NULL);
1146 ext2fs_mark_super_dirty(fs);
1147 }
1148 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001149
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001150 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001151
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001152 ext2fs_close(fs);
1153 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001154 free(ctx->filesystem_name);
1155 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001156 e2fsck_free_context(ctx);
1157
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001158#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001159 if (ctx->options & E2F_OPT_TIME)
1160 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001161#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001162
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001163 return exit_value;
1164}