blob: 1adc18310c8aab63ae26956d6c129fa22c617a6b [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");
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500225 if (d) {
226 while (de=readdir(d)) {
227 if (!strncmp(".", de->d_name, 1))
228 continue;
229 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
230 de->d_name);
231 f = fopen(fname, "r");
232 if (!f)
233 continue;
234 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
235 tmp[0] = 0;
236 fclose(f);
237 if (strncmp(tmp, "off-line", 8) == 0) {
238 closedir(d);
239 return 1;
240 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400241 }
Matthias Andree4b137042005-01-13 03:35:29 +0100242 closedir(d);
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500243 }
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500244 return 0;
245}
246
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000247/*
248 * This routine checks to see if a filesystem can be skipped; if so,
249 * it will exit with E2FSCK_OK. Under some conditions it will print a
250 * message explaining why a check is being forced.
251 */
252static void check_if_skip(e2fsck_t ctx)
253{
254 ext2_filsys fs = ctx->fs;
255 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000256 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500257 long next_check;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500258 int batt = is_on_batt();
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500259 time_t now = time(0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000260
Theodore Ts'od37066a2001-12-21 23:28:54 -0500261 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
262 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000263 return;
264
265 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000266 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000267 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000268 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000269 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000270 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000271 (unsigned) fs->super->s_max_mnt_count)) {
272 reason = _(" has been mounted %u times without being checked");
273 reason_arg = fs->super->s_mnt_count;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500274 if (batt && (fs->super->s_mnt_count <
275 (unsigned) fs->super->s_max_mnt_count*2))
276 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000277 } else if (fs->super->s_checkinterval &&
Theodore Ts'o54434922003-12-07 01:28:50 -0500278 ((now - fs->super->s_lastcheck) >=
279 fs->super->s_checkinterval)) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000280 reason = _(" has gone %u days without being checked");
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500281 reason_arg = (now - fs->super->s_lastcheck)/(3600*24);
Theodore Ts'o54434922003-12-07 01:28:50 -0500282 if (batt && ((now - fs->super->s_lastcheck) <
283 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500284 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000285 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000286 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000287 fputs(ctx->device_name, stdout);
288 printf(reason, reason_arg);
289 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000290 return;
291 }
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500292 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000293 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
294 fs->super->s_inodes_count,
295 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
296 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500297 next_check = 100000;
298 if (fs->super->s_max_mnt_count > 0) {
299 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
300 if (next_check <= 0)
301 next_check = 1;
302 }
Theodore Ts'o66fbee82004-05-04 20:38:17 -0400303 if (fs->super->s_checkinterval &&
304 ((now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500305 next_check = 1;
306 if (next_check <= 5) {
307 if (next_check == 1)
308 fputs(_(" (check after next mount)"), stdout);
309 else
Theodore Ts'o54434922003-12-07 01:28:50 -0500310 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500311 }
312 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000313 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400314 ctx->fs = NULL;
315 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000316 exit(FSCK_OK);
317}
318
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000319/*
320 * For completion notice
321 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000322struct percent_tbl {
323 int max_pass;
324 int table[32];
325};
326struct percent_tbl e2fsck_tbl = {
327 5, { 0, 70, 90, 92, 95, 100 }
328};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400329static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000330
331static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
332 int max)
333{
334 float percent;
335
336 if (pass <= 0)
337 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000338 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000339 return 100.0;
340 percent = ((float) curr) / ((float) max);
341 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
342 + tbl->table[pass-1]);
343}
344
345extern void e2fsck_clear_progbar(e2fsck_t ctx)
346{
347 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
348 return;
349
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400350 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
351 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400352 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000353 ctx->flags &= ~E2F_FLAG_PROG_BAR;
354}
355
Theodore Ts'o520ead32003-04-19 13:48:27 -0400356int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500357 unsigned int dpynum)
358{
359 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500360 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500361 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500362 struct timeval tv;
363 int dpywidth;
364
365 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
366 return 0;
367
368 /*
369 * Calculate the new progress position. If the
370 * percentage hasn't changed, then we skip out right
371 * away.
372 */
373 if (ctx->progress_last_percent == (int) 10 * percent)
374 return 0;
375 ctx->progress_last_percent = (int) 10 * percent;
376
377 /*
378 * If we've already updated the spinner once within
379 * the last 1/8th of a second, no point doing it
380 * again.
381 */
382 gettimeofday(&tv, NULL);
383 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
384 if ((tick == ctx->progress_last_time) &&
385 (percent != 0.0) && (percent != 100.0))
386 return 0;
387 ctx->progress_last_time = tick;
388
389 /*
390 * Advance the spinner, and note that the progress bar
391 * will be on the screen
392 */
393 ctx->progress_pos = (ctx->progress_pos+1) & 3;
394 ctx->flags |= E2F_FLAG_PROG_BAR;
395
396 dpywidth = 66 - strlen(label);
397 dpywidth = 8 * (dpywidth / 8);
398 if (dpynum)
399 dpywidth -= 8;
400
401 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400402 printf("%s%s: |%s%s", ctx->start_meta, label,
403 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500404 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
405 if (percent == 100.0)
406 fputc('|', stdout);
407 else
408 fputc(spinner[ctx->progress_pos & 3], stdout);
409 if (dpynum)
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400410 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500411 else
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400412 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500413
414 if (percent == 100.0)
415 e2fsck_clear_progbar(ctx);
416 fflush(stdout);
417
418 return 0;
419}
420
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000421static int e2fsck_update_progress(e2fsck_t ctx, int pass,
422 unsigned long cur, unsigned long max)
423{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000424 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000425 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000426
427 if (pass == 0)
428 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000429
430 if (ctx->progress_fd) {
431 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
432 write(ctx->progress_fd, buf, strlen(buf));
433 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000434 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500435 e2fsck_simple_progress(ctx, ctx->device_name,
436 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000437 }
438 return 0;
439}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000440
441#define PATH_SET "PATH=/sbin"
442
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000443static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000444{
445 int fd;
446
447 while (1) {
448 fd = open("/dev/null", O_RDWR);
449 if (fd > 2)
450 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000451 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000452 fprintf(stderr, _("ERROR: Couldn't open "
453 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000454 strerror(errno));
455 break;
456 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000457 }
458 close(fd);
459}
460
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000461#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500462static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000463{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000464 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000465
466 if (!ctx)
467 return;
468
469 ctx->progress = e2fsck_update_progress;
470 ctx->progress_fd = 0;
471}
472
Theodore Ts'o54434922003-12-07 01:28:50 -0500473static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000474{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000475 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000476
477 if (!ctx)
478 return;
479
480 e2fsck_clear_progbar(ctx);
481 ctx->progress = 0;
482}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400483
Theodore Ts'o54434922003-12-07 01:28:50 -0500484static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400485{
486 e2fsck_t ctx = e2fsck_global_ctx;
487
488 if (!ctx)
489 exit(FSCK_CANCELED);
490
491 ctx->flags |= E2F_FLAG_CANCEL;
492}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000493#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000494
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400495static void parse_extended_opts(e2fsck_t ctx, const char *opts)
496{
497 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500498 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400499 int extended_usage = 0;
500
Theodore Ts'of3640932003-03-01 19:47:44 -0500501 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400502 for (token = buf; token && *token; token = next) {
503 p = strchr(token, ',');
504 next = 0;
505 if (p) {
506 *p = 0;
507 next = p+1;
508 }
509 arg = strchr(token, '=');
510 if (arg) {
511 *arg = 0;
512 arg++;
513 }
514 if (strcmp(token, "ea_ver") == 0) {
515 if (!arg) {
516 extended_usage++;
517 continue;
518 }
519 ea_ver = strtoul(arg, &p, 0);
520 if (*p ||
521 ((ea_ver != 1) && (ea_ver != 2))) {
522 fprintf(stderr,
523 _("Invalid EA version.\n"));
524 extended_usage++;
525 continue;
526 }
527 ctx->ext_attr_ver = ea_ver;
528 } else
529 extended_usage++;
530 }
531 if (extended_usage) {
532 fprintf(stderr, _("Extended options are separated by commas, "
533 "and may take an argument which\n"
534 "is set off by an equals ('=') sign. "
535 "Valid raid options are:\n"
536 "\tea_ver=<ea_version (1 or 2)\n\n"));
537 exit(1);
538 }
539}
540
541
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000542static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
543{
544 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000545 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000546#ifdef MTRACE
547 extern void *mallwatch;
548#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000549 e2fsck_t ctx;
550 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000551#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000552 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000553#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400554 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000555
556 retval = e2fsck_allocate_context(&ctx);
557 if (retval)
558 return retval;
559
560 *ret_ctx = ctx;
561
Theodore Ts'o908b7852003-04-16 15:20:13 -0400562 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
563 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400564 if (isatty(0) && isatty(1)) {
565 ctx->interactive = 1;
566 } else {
567 ctx->start_meta[0] = '\001';
568 ctx->stop_meta[0] = '\002';
569 }
570 memset(bar, '=', sizeof(bar)-1);
571 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000572 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500573 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000574
575 if (argc && *argv)
576 ctx->program_name = *argv;
577 else
578 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500579 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 +0000580 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000581 case 'C':
582 ctx->progress = e2fsck_update_progress;
583 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000584 if (!ctx->progress_fd)
585 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000586 /* Validate the file descriptor to avoid disasters */
587 fd = dup(ctx->progress_fd);
588 if (fd < 0) {
589 fprintf(stderr,
590 _("Error validating file descriptor %d: %s\n"),
591 ctx->progress_fd,
592 error_message(errno));
593 fatal_error(ctx,
594 _("Invalid completion information file descriptor"));
595 } else
596 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000597 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400598 case 'D':
599 ctx->options |= E2F_OPT_COMPRESS_DIRS;
600 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400601 case 'E':
602 extended_opts = optarg;
603 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000604 case 'p':
605 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500606 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
607 conflict_opt:
608 fatal_error(ctx,
609 _("Only one the options -p/-a, -n or -y may be specified."));
610 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000611 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 break;
613 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500614 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
615 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000616 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 break;
618 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500619 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
620 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000621 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000622 break;
623 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000624#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 if (ctx->options & E2F_OPT_TIME)
626 ctx->options |= E2F_OPT_TIME2;
627 else
628 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000629#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000630 fprintf(stderr, _("The -t option is not "
631 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000632#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000633 break;
634 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500635 if (cflag++)
636 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000637 ctx->options |= E2F_OPT_CHECKBLOCKS;
638 break;
639 case 'r':
640 /* What we do by default, anyway! */
641 break;
642 case 'b':
643 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400644 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000645 break;
646 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500647 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000648 break;
649 case 'I':
650 ctx->inode_buffer_blocks = atoi(optarg);
651 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400652 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500653 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400654 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000655 case 'P':
656 ctx->process_inode_size = atoi(optarg);
657 break;
658 case 'L':
659 replace_bad_blocks++;
660 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500661 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 break;
663 case 'd':
664 ctx->options |= E2F_OPT_DEBUG;
665 break;
666 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500667 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 break;
669 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000671 break;
672 case 'v':
673 verbose = 1;
674 break;
675 case 'V':
676 show_version_only = 1;
677 break;
678#ifdef MTRACE
679 case 'M':
680 mallwatch = (void *) strtol(optarg, NULL, 0);
681 break;
682#endif
683 case 'N':
684 ctx->device_name = optarg;
685 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000686#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000687 case 's':
688 normalize_swapfs = 1;
689 case 'S':
690 swapfs = 1;
691 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000692#else
693 case 's':
694 case 'S':
695 fprintf(stderr, _("Byte-swapping filesystems "
696 "not compiled in this version "
697 "of e2fsck\n"));
698 exit(1);
699#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500700 case 'k':
701 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500702 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000703 default:
704 usage(ctx);
705 }
706 if (show_version_only)
707 return 0;
708 if (optind != argc - 1)
709 usage(ctx);
710 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400711 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000712 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500713 ctx->io_options = strchr(argv[optind], '?');
714 if (ctx->io_options)
715 *ctx->io_options++ = 0;
Theodore Ts'of3640932003-03-01 19:47:44 -0500716 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500717 if (!ctx->filesystem_name) {
718 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
719 argv[optind]);
720 fatal_error(ctx, 0);
721 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400722 if (extended_opts)
723 parse_extended_opts(ctx, extended_opts);
724
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000725 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500726 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000727 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000728 com_err("open", errno,
729 _("while opening %s for flushing"),
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 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000733 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000734 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000735 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000736 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000737 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000738 }
739 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000740 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000741#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000742 if (swapfs) {
743 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000744 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500745 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000746 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000747 }
748 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000749#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500750 if (cflag && bad_blocks_file) {
751 fprintf(stderr, _("The -c and the -l/-L options may "
752 "not be both used at the same time.\n"));
753 exit(FSCK_USAGE);
754 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000755#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000756 /*
757 * Set up signal action
758 */
759 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400760 sa.sa_handler = signal_cancel;
761 sigaction(SIGINT, &sa, 0);
762 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000763#ifdef SA_RESTART
764 sa.sa_flags = SA_RESTART;
765#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000766 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000767 sa.sa_handler = signal_progress_on;
768 sigaction(SIGUSR1, &sa, 0);
769 sa.sa_handler = signal_progress_off;
770 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000771#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400772
773 /* Update our PATH to include /sbin if we need to run badblocks */
774 if (cflag) {
775 char *oldpath = getenv("PATH");
776 if (oldpath) {
777 char *newpath;
778
779 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
780 strlen (oldpath));
781 if (!newpath)
782 fatal_error(ctx, "Couldn't malloc() newpath");
783 strcpy (newpath, PATH_SET);
784 strcat (newpath, ":");
785 strcat (newpath, oldpath);
786 putenv (newpath);
787 } else
788 putenv (PATH_SET);
789 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500790#ifdef CONFIG_JBD_DEBUG
791 if (getenv("E2FSCK_JBD_DEBUG"))
792 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
793#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000794 return 0;
795}
796
797static const char *my_ver_string = E2FSPROGS_VERSION;
798static const char *my_ver_date = E2FSPROGS_DATE;
799
800int main (int argc, char *argv[])
801{
802 errcode_t retval = 0;
803 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000804 ext2_filsys fs = 0;
805 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000806 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000807 const char *lib_ver_date;
808 int my_ver, lib_ver;
809 e2fsck_t ctx;
810 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000811 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000812
813 clear_problem_context(&pctx);
814#ifdef MTRACE
815 mtrace();
816#endif
817#ifdef MCHECK
818 mcheck(0);
819#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000820#ifdef ENABLE_NLS
821 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500822 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000823 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
824 textdomain(NLS_CAT_NAME);
825#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000826 my_ver = ext2fs_parse_version_string(my_ver_string);
827 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
828 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000829 fprintf( stderr, _("Error: ext2fs library version "
830 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000831 show_version_only++;
832 }
833
834 retval = PRS(argc, argv, &ctx);
835 if (retval) {
836 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000837 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000838 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000840 reserve_stdio_fds();
841
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000842#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000843 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000844#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000845
846 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400847 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400848 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000849
850 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000851 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000852 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000853 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000854 }
855
856 check_mount(ctx);
857
858 if (!(ctx->options & E2F_OPT_PREEN) &&
859 !(ctx->options & E2F_OPT_NO) &&
860 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400861 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000862 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000863 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000864 }
865 ctx->superblock = ctx->use_superblock;
866restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400867#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000868 io_ptr = test_io_manager;
869 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400870#else
871 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000872#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000873 flags = 0;
874 if ((ctx->options & E2F_OPT_READONLY) == 0)
875 flags |= EXT2_FLAG_RW;
876
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500877 if (ctx->io_options) {
878 int len = strlen(ctx->filesystem_name) +
879 strlen(ctx->io_options) + 2;
880 }
881
Theodore Ts'of1a17612001-12-23 22:27:52 -0500882 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500883 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
884 flags, ctx->superblock, ctx->blocksize,
885 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000886 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600887 int blocksize;
888 for (blocksize = EXT2_MIN_BLOCK_SIZE;
889 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500890 retval = ext2fs_open2(ctx->filesystem_name,
891 ctx->io_options, flags,
892 ctx->superblock, blocksize,
893 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000894 if (!retval)
895 break;
896 }
897 } else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500898 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
899 flags, 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400900 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
901 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000902 ((retval == EXT2_ET_BAD_MAGIC) ||
903 ((retval == 0) && ext2fs_check_desc(fs)))) {
904 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000905 printf(_("%s trying backup blocks...\n"),
906 retval ? _("Couldn't find ext2 superblock,") :
907 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500908 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000909 if (fs)
910 ext2fs_close(fs);
911 goto restart;
912 }
913 }
914 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000915 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000916 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000917 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000918 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000919 "too high for this version of e2fsck.\n"
920 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000921 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000922 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
923 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000924 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000925 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000926 printf(_("You must have %s access to the "
927 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000928 (ctx->options & E2F_OPT_READONLY) ?
929 "r/o" : "r/w");
930 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000931 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000932#ifdef EROFS
933 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000934 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000935 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000936 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000937#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000938 else
939 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000940 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000941 }
942 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000943 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000944 sb = fs->super;
945 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000946 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000947 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000948 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000949 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000950 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000951 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000952
Theodore Ts'o17390c02000-07-07 04:13:21 +0000953 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000954 * Set the device name, which is used whenever we print error
955 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000956 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000957 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000958 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500959 ctx->device_name = string_copy(ctx, sb->s_volume_name,
960 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000961 }
962 if (ctx->device_name == 0)
963 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000964
965 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000966 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000967 */
968 retval = e2fsck_check_ext3_journal(ctx);
969 if (retval) {
970 com_err(ctx->program_name, retval,
971 _("while checking ext3 journal for %s"),
972 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000973 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000974 }
975
Theodore Ts'o9b565752000-12-13 18:50:22 +0000976 /*
977 * Check to see if we need to do ext3-style recovery. If so,
978 * do it, and then restart the fsck.
979 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000980 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000981 if (ctx->options & E2F_OPT_READONLY) {
982 printf(_("Warning: skipping journal recovery "
983 "because doing a read-only filesystem "
984 "check.\n"));
985 io_channel_flush(ctx->fs->io);
986 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500987 if (ctx->flags & E2F_FLAG_RESTARTED) {
988 /*
989 * Whoops, we attempted to run the
990 * journal twice. This should never
991 * happen, unless the hardware or
992 * device driver is being bogus.
993 */
994 com_err(ctx->program_name, 0,
995 _("unable to set superblock flags on %s\n"), ctx->device_name);
996 fatal_error(ctx, 0);
997 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000998 retval = e2fsck_run_ext3_journal(ctx);
999 if (retval) {
1000 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001001 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001002 ctx->device_name);
1003 fatal_error(ctx, 0);
1004 }
1005 ext2fs_close(ctx->fs);
1006 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001007 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001008 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001009 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001010 }
1011
1012 /*
1013 * Check for compatibility with the feature sets. We need to
1014 * be more stringent than ext2fs_open().
1015 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001016 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1017 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001018 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1019 "(%s)", ctx->device_name);
1020 goto get_newer;
1021 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001022 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001023 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1024 "(%s)", ctx->device_name);
1025 goto get_newer;
1026 }
1027#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001028 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001029 com_err(ctx->program_name, 0,
1030 _("Warning: compression support is experimental.\n"));
1031#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001032#ifndef ENABLE_HTREE
1033 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1034 com_err(ctx->program_name, 0,
1035 _("E2fsck not compiled with HTREE support,\n\t"
1036 "but filesystem %s has HTREE directories.\n"),
1037 ctx->device_name);
1038 goto get_newer;
1039 }
1040#endif
1041
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001042 /*
1043 * If the user specified a specific superblock, presumably the
1044 * master superblock has been trashed. So we mark the
1045 * superblock as dirty, so it can be written out.
1046 */
1047 if (ctx->superblock &&
1048 !(ctx->options & E2F_OPT_READONLY))
1049 ext2fs_mark_super_dirty(fs);
1050
1051 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001052 * We only update the master superblock because (a) paranoia;
1053 * we don't want to corrupt the backup superblocks, and (b) we
1054 * don't need to update the mount count and last checked
1055 * fields in the backup superblock (the kernel doesn't
1056 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001057 */
1058 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1059
1060 ehandler_init(fs->io);
1061
1062 if (ctx->superblock)
1063 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1064 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001065 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001066 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001067 check_if_skip(ctx);
1068 if (bad_blocks_file)
1069 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1070 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001071 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001072 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001073 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001074#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001075 if (normalize_swapfs) {
1076 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1077 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001078 fprintf(stderr, _("%s: Filesystem byte order "
1079 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001080 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001081 }
1082 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001083 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001084 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001085 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001086 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001087 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001088#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001089
1090 /*
1091 * Mark the system as valid, 'til proven otherwise
1092 */
1093 ext2fs_mark_valid(fs);
1094
1095 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1096 if (retval) {
1097 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001098 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001099 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001100 printf(_("This doesn't bode well,"
1101 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001102 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001103
1104 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001105 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001106 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001107 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001108 retval = e2fsck_reset_context(ctx);
1109 if (retval) {
1110 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001111 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001112 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001113 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001114 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001115 goto restart;
1116 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001117 if (run_result & E2F_FLAG_CANCEL) {
1118 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1119 ctx->device_name : ctx->filesystem_name);
1120 exit_value |= FSCK_CANCELED;
1121 }
1122 if (run_result & E2F_FLAG_ABORT)
1123 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001124
1125#ifdef MTRACE
1126 mtrace_print("Cleanup");
1127#endif
1128 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001129 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001130 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001131 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001132 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001133 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001134 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001135 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001136 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001137 }
1138 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001139 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001140 printf(_("\n%s: ********** WARNING: Filesystem still has "
1141 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001142 exit_value |= FSCK_UNCORRECTED;
1143 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001144 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001145 if (exit_value & FSCK_CANCELED)
1146 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001147 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001148 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001149 if (!(ctx->options & E2F_OPT_READONLY)) {
1150 if (ext2fs_test_valid(fs)) {
1151 if (!(sb->s_state & EXT2_VALID_FS))
1152 exit_value |= FSCK_NONDESTRUCT;
1153 sb->s_state = EXT2_VALID_FS;
1154 } else
1155 sb->s_state &= ~EXT2_VALID_FS;
1156 sb->s_mnt_count = 0;
1157 sb->s_lastcheck = time(NULL);
1158 ext2fs_mark_super_dirty(fs);
1159 }
1160 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001161
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001162 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001163
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001164 ext2fs_close(fs);
1165 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001166 free(ctx->filesystem_name);
1167 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001168 e2fsck_free_context(ctx);
1169
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001170#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001171 if (ctx->options & E2F_OPT_TIME)
1172 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001173#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001174
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001175 return exit_value;
1176}