blob: 211a12736d46f1c8a9c1f8895ece673aa2c3edd0 [file] [log] [blame]
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001/*
2 * unix.c - The unix-specific code for e2fsck
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#ifdef HAVE_STDLIB_H
14#include <stdlib.h>
15#endif
16#include <string.h>
17#include <fcntl.h>
18#include <ctype.h>
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000019#include <time.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000020#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +000021#include <signal.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000022#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000023#ifdef HAVE_GETOPT_H
24#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000025#else
26extern char *optarg;
27extern int optind;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000028#endif
29#include <unistd.h>
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33#ifdef HAVE_MNTENT_H
34#include <mntent.h>
35#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040036#ifdef HAVE_SYS_IOCTL_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000037#include <sys/ioctl.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040038#endif
Theodore Ts'oe71d8732003-03-14 02:13:48 -050039#ifdef HAVE_MALLOC_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000040#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050041#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000042
43#include "et/com_err.h"
44#include "e2fsck.h"
45#include "problem.h"
46#include "../version.h"
47
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000048/* Command line options */
Theodore Ts'o54a31a32003-08-19 10:08:34 -040049static int swapfs;
50static int normalize_swapfs;
51static int cflag; /* check disk */
52static int show_version_only;
53static int verbose;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054
Theodore Ts'o54a31a32003-08-19 10:08:34 -040055static int replace_bad_blocks;
56static char *bad_blocks_file;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000057
Theodore Ts'o243dc312000-08-22 21:37:47 +000058e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
59
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -050060#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
61int journal_enable_debug = -1;
62#endif
63
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000064static void usage(e2fsck_t ctx)
65{
66 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050067 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000068 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040069 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
70 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000071 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000072
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000073 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000074 " -p Automatic repair (no questions)\n"
75 " -n Make no changes to the filesystem\n"
76 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050077 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000078 " -f Force checking even if filesystem is marked clean\n"));
79 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000080 " -v Be verbose\n"
81 " -b superblock Use alternative superblock\n"
82 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'oadee8d72001-07-23 00:17:49 -040083 " -j external-journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000084 " -l bad_blocks_file Add to badblocks list\n"
85 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000086 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000087
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000088 exit(FSCK_USAGE);
89}
90
91static void show_stats(e2fsck_t ctx)
92{
93 ext2_filsys fs = ctx->fs;
94 int inodes, inodes_used, blocks, blocks_used;
95 int dir_links;
96 int num_files, num_links;
97 int frag_percent;
98
99 dir_links = 2 * ctx->fs_directory_count - 1;
100 num_files = ctx->fs_total_count - dir_links;
101 num_links = ctx->fs_links_count - dir_links;
102 inodes = fs->super->s_inodes_count;
103 inodes_used = (fs->super->s_inodes_count -
104 fs->super->s_free_inodes_count);
105 blocks = fs->super->s_blocks_count;
106 blocks_used = (fs->super->s_blocks_count -
107 fs->super->s_free_blocks_count);
108
109 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
110 frag_percent = (frag_percent + 5) / 10;
111
112 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000113 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000114 ctx->device_name, inodes_used, inodes,
115 frag_percent / 10, frag_percent % 10,
116 blocks_used, blocks);
117 return;
118 }
Theodore Ts'o113e4052003-05-17 21:00:46 -0400119 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
120 inodes_used), inodes_used, 100 * inodes_used / inodes);
121 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
122 "%8d non-contiguous inodes (%0d.%d%%)\n",
123 ctx->fs_fragmented),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000124 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
125 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
126 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Theodore Ts'o113e4052003-05-17 21:00:46 -0400127 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
128 blocks_used),
129 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
130 printf (P_("%8d bad block\n", "%8d bad blocks\n",
131 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
132 printf (P_("%8d large file\n", "%8d large files\n",
133 ctx->large_files), ctx->large_files);
134 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
135 ctx->fs_regular_count), ctx->fs_regular_count);
136 printf (P_("%8d directory\n", "%8d directories\n",
137 ctx->fs_directory_count), ctx->fs_directory_count);
138 printf (P_("%8d character device file\n",
139 "%8d character device files\n", ctx->fs_chardev_count),
140 ctx->fs_chardev_count);
141 printf (P_("%8d block device file\n", "%8d block device files\n",
142 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
143 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
144 ctx->fs_fifo_count);
145 printf (P_("%8d link\n", "%8d links\n",
146 ctx->fs_links_count - dir_links),
147 ctx->fs_links_count - dir_links);
148 printf (P_("%8d symbolic link", "%8d symbolic links",
149 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
150 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
151 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
152 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
153 ctx->fs_sockets_count);
154 printf ("--------\n");
155 printf (P_("%8d file\n", "%8d files\n",
156 ctx->fs_total_count - dir_links),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000157 ctx->fs_total_count - dir_links);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000158}
159
160static void check_mount(e2fsck_t ctx)
161{
162 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500163 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000164
Theodore Ts'oee895132002-11-07 16:16:55 -0500165 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
166 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000167 if (retval) {
168 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000169 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000170 ctx->filesystem_name);
171 return;
172 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400173
174 /*
175 * If the filesystem isn't mounted, or it's the root filesystem
176 * and it's mounted read-only, then everything's fine.
177 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500178 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
179 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
180 (ctx->mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000181 return;
182
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000183 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000184 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000185 return;
186 }
187
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000188 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400189 if (!ctx->interactive)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000190 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000191 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000192 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000193 "SEVERE filesystem damage.\007\007\007\n\n"));
194 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000195 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000196 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000197 exit (0);
198 }
199 return;
200}
201
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202/*
203 * This routine checks to see if a filesystem can be skipped; if so,
204 * it will exit with E2FSCK_OK. Under some conditions it will print a
205 * message explaining why a check is being forced.
206 */
207static void check_if_skip(e2fsck_t ctx)
208{
209 ext2_filsys fs = ctx->fs;
210 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000211 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500212 long next_check;
213 time_t now = time(0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000214
Theodore Ts'od37066a2001-12-21 23:28:54 -0500215 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
216 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000217 return;
218
219 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000220 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000221 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000222 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000223 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000224 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000225 (unsigned) fs->super->s_max_mnt_count)) {
226 reason = _(" has been mounted %u times without being checked");
227 reason_arg = fs->super->s_mnt_count;
228 } else if (fs->super->s_checkinterval &&
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500229 now >= (fs->super->s_lastcheck +
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000230 fs->super->s_checkinterval)) {
231 reason = _(" has gone %u days without being checked");
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500232 reason_arg = (now - fs->super->s_lastcheck)/(3600*24);
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000233 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000234 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000235 fputs(ctx->device_name, stdout);
236 printf(reason, reason_arg);
237 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000238 return;
239 }
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500240 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000241 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
242 fs->super->s_inodes_count,
243 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
244 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500245 next_check = 100000;
246 if (fs->super->s_max_mnt_count > 0) {
247 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
248 if (next_check <= 0)
249 next_check = 1;
250 }
251 if (now >= (fs->super->s_lastcheck + fs->super->s_checkinterval))
252 next_check = 1;
253 if (next_check <= 5) {
254 if (next_check == 1)
255 fputs(_(" (check after next mount)"), stdout);
256 else
257 printf(_(" (check in %d mounts)"), next_check);
258 }
259 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000260 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400261 ctx->fs = NULL;
262 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000263 exit(FSCK_OK);
264}
265
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000266/*
267 * For completion notice
268 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000269struct percent_tbl {
270 int max_pass;
271 int table[32];
272};
273struct percent_tbl e2fsck_tbl = {
274 5, { 0, 70, 90, 92, 95, 100 }
275};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400276static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000277
278static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
279 int max)
280{
281 float percent;
282
283 if (pass <= 0)
284 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000285 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000286 return 100.0;
287 percent = ((float) curr) / ((float) max);
288 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
289 + tbl->table[pass-1]);
290}
291
292extern void e2fsck_clear_progbar(e2fsck_t ctx)
293{
294 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
295 return;
296
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400297 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
298 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400299 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000300 ctx->flags &= ~E2F_FLAG_PROG_BAR;
301}
302
Theodore Ts'o520ead32003-04-19 13:48:27 -0400303int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500304 unsigned int dpynum)
305{
306 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500307 int i;
308 int tick;
309 struct timeval tv;
310 int dpywidth;
311
312 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
313 return 0;
314
315 /*
316 * Calculate the new progress position. If the
317 * percentage hasn't changed, then we skip out right
318 * away.
319 */
320 if (ctx->progress_last_percent == (int) 10 * percent)
321 return 0;
322 ctx->progress_last_percent = (int) 10 * percent;
323
324 /*
325 * If we've already updated the spinner once within
326 * the last 1/8th of a second, no point doing it
327 * again.
328 */
329 gettimeofday(&tv, NULL);
330 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
331 if ((tick == ctx->progress_last_time) &&
332 (percent != 0.0) && (percent != 100.0))
333 return 0;
334 ctx->progress_last_time = tick;
335
336 /*
337 * Advance the spinner, and note that the progress bar
338 * will be on the screen
339 */
340 ctx->progress_pos = (ctx->progress_pos+1) & 3;
341 ctx->flags |= E2F_FLAG_PROG_BAR;
342
343 dpywidth = 66 - strlen(label);
344 dpywidth = 8 * (dpywidth / 8);
345 if (dpynum)
346 dpywidth -= 8;
347
348 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400349 printf("%s%s: |%s%s", ctx->start_meta, label,
350 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500351 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
352 if (percent == 100.0)
353 fputc('|', stdout);
354 else
355 fputc(spinner[ctx->progress_pos & 3], stdout);
356 if (dpynum)
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400357 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500358 else
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400359 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500360
361 if (percent == 100.0)
362 e2fsck_clear_progbar(ctx);
363 fflush(stdout);
364
365 return 0;
366}
367
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000368static int e2fsck_update_progress(e2fsck_t ctx, int pass,
369 unsigned long cur, unsigned long max)
370{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000371 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000372 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000373
374 if (pass == 0)
375 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000376
377 if (ctx->progress_fd) {
378 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
379 write(ctx->progress_fd, buf, strlen(buf));
380 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000381 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500382 e2fsck_simple_progress(ctx, ctx->device_name,
383 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000384 }
385 return 0;
386}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000387
388#define PATH_SET "PATH=/sbin"
389
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000390static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000391{
392 int fd;
393
394 while (1) {
395 fd = open("/dev/null", O_RDWR);
396 if (fd > 2)
397 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000398 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000399 fprintf(stderr, _("ERROR: Couldn't open "
400 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000401 strerror(errno));
402 break;
403 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000404 }
405 close(fd);
406}
407
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000408#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000409static void signal_progress_on(int sig)
410{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000411 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000412
413 if (!ctx)
414 return;
415
416 ctx->progress = e2fsck_update_progress;
417 ctx->progress_fd = 0;
418}
419
420static void signal_progress_off(int sig)
421{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000422 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000423
424 if (!ctx)
425 return;
426
427 e2fsck_clear_progbar(ctx);
428 ctx->progress = 0;
429}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400430
431static void signal_cancel(int sig)
432{
433 e2fsck_t ctx = e2fsck_global_ctx;
434
435 if (!ctx)
436 exit(FSCK_CANCELED);
437
438 ctx->flags |= E2F_FLAG_CANCEL;
439}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000440#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000441
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400442static void parse_extended_opts(e2fsck_t ctx, const char *opts)
443{
444 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500445 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400446 int extended_usage = 0;
447
Theodore Ts'of3640932003-03-01 19:47:44 -0500448 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400449 for (token = buf; token && *token; token = next) {
450 p = strchr(token, ',');
451 next = 0;
452 if (p) {
453 *p = 0;
454 next = p+1;
455 }
456 arg = strchr(token, '=');
457 if (arg) {
458 *arg = 0;
459 arg++;
460 }
461 if (strcmp(token, "ea_ver") == 0) {
462 if (!arg) {
463 extended_usage++;
464 continue;
465 }
466 ea_ver = strtoul(arg, &p, 0);
467 if (*p ||
468 ((ea_ver != 1) && (ea_ver != 2))) {
469 fprintf(stderr,
470 _("Invalid EA version.\n"));
471 extended_usage++;
472 continue;
473 }
474 ctx->ext_attr_ver = ea_ver;
475 } else
476 extended_usage++;
477 }
478 if (extended_usage) {
479 fprintf(stderr, _("Extended options are separated by commas, "
480 "and may take an argument which\n"
481 "is set off by an equals ('=') sign. "
482 "Valid raid options are:\n"
483 "\tea_ver=<ea_version (1 or 2)\n\n"));
484 exit(1);
485 }
486}
487
488
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000489static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
490{
491 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000492 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000493#ifdef MTRACE
494 extern void *mallwatch;
495#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000496 e2fsck_t ctx;
497 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000498#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000499 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000500#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400501 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502
503 retval = e2fsck_allocate_context(&ctx);
504 if (retval)
505 return retval;
506
507 *ret_ctx = ctx;
508
Theodore Ts'o908b7852003-04-16 15:20:13 -0400509 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
510 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400511 if (isatty(0) && isatty(1)) {
512 ctx->interactive = 1;
513 } else {
514 ctx->start_meta[0] = '\001';
515 ctx->stop_meta[0] = '\002';
516 }
517 memset(bar, '=', sizeof(bar)-1);
518 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000519 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500520 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000521
522 if (argc && *argv)
523 ctx->program_name = *argv;
524 else
525 ctx->program_name = "e2fsck";
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400526 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsD")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000527 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000528 case 'C':
529 ctx->progress = e2fsck_update_progress;
530 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000531 if (!ctx->progress_fd)
532 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000533 /* Validate the file descriptor to avoid disasters */
534 fd = dup(ctx->progress_fd);
535 if (fd < 0) {
536 fprintf(stderr,
537 _("Error validating file descriptor %d: %s\n"),
538 ctx->progress_fd,
539 error_message(errno));
540 fatal_error(ctx,
541 _("Invalid completion information file descriptor"));
542 } else
543 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000544 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400545 case 'D':
546 ctx->options |= E2F_OPT_COMPRESS_DIRS;
547 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400548 case 'E':
549 extended_opts = optarg;
550 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551 case 'p':
552 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500553 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
554 conflict_opt:
555 fatal_error(ctx,
556 _("Only one the options -p/-a, -n or -y may be specified."));
557 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000558 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000559 break;
560 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500561 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
562 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000563 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000564 break;
565 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500566 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
567 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000568 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000569 break;
570 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000571#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000572 if (ctx->options & E2F_OPT_TIME)
573 ctx->options |= E2F_OPT_TIME2;
574 else
575 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000576#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000577 fprintf(stderr, _("The -t option is not "
578 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000579#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000580 break;
581 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500582 if (cflag++)
583 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 ctx->options |= E2F_OPT_CHECKBLOCKS;
585 break;
586 case 'r':
587 /* What we do by default, anyway! */
588 break;
589 case 'b':
590 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400591 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000592 break;
593 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500594 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000595 break;
596 case 'I':
597 ctx->inode_buffer_blocks = atoi(optarg);
598 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400599 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500600 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400601 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000602 case 'P':
603 ctx->process_inode_size = atoi(optarg);
604 break;
605 case 'L':
606 replace_bad_blocks++;
607 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500608 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000609 break;
610 case 'd':
611 ctx->options |= E2F_OPT_DEBUG;
612 break;
613 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500614 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000615 break;
616 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000618 break;
619 case 'v':
620 verbose = 1;
621 break;
622 case 'V':
623 show_version_only = 1;
624 break;
625#ifdef MTRACE
626 case 'M':
627 mallwatch = (void *) strtol(optarg, NULL, 0);
628 break;
629#endif
630 case 'N':
631 ctx->device_name = optarg;
632 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000633#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000634 case 's':
635 normalize_swapfs = 1;
636 case 'S':
637 swapfs = 1;
638 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000639#else
640 case 's':
641 case 'S':
642 fprintf(stderr, _("Byte-swapping filesystems "
643 "not compiled in this version "
644 "of e2fsck\n"));
645 exit(1);
646#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000647 default:
648 usage(ctx);
649 }
650 if (show_version_only)
651 return 0;
652 if (optind != argc - 1)
653 usage(ctx);
654 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400655 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000656 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'of3640932003-03-01 19:47:44 -0500657 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500658 if (!ctx->filesystem_name) {
659 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
660 argv[optind]);
661 fatal_error(ctx, 0);
662 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400663 if (extended_opts)
664 parse_extended_opts(ctx, extended_opts);
665
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000666 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500667 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000669 com_err("open", errno,
670 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000671 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000672 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000673 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000674 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000675 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000676 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000677 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000678 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000679 }
680 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000681 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000682#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000683 if (swapfs) {
684 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000685 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500686 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000687 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000688 }
689 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000690#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500691 if (cflag && bad_blocks_file) {
692 fprintf(stderr, _("The -c and the -l/-L options may "
693 "not be both used at the same time.\n"));
694 exit(FSCK_USAGE);
695 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000696#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000697 /*
698 * Set up signal action
699 */
700 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400701 sa.sa_handler = signal_cancel;
702 sigaction(SIGINT, &sa, 0);
703 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000704#ifdef SA_RESTART
705 sa.sa_flags = SA_RESTART;
706#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000707 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000708 sa.sa_handler = signal_progress_on;
709 sigaction(SIGUSR1, &sa, 0);
710 sa.sa_handler = signal_progress_off;
711 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000712#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400713
714 /* Update our PATH to include /sbin if we need to run badblocks */
715 if (cflag) {
716 char *oldpath = getenv("PATH");
717 if (oldpath) {
718 char *newpath;
719
720 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
721 strlen (oldpath));
722 if (!newpath)
723 fatal_error(ctx, "Couldn't malloc() newpath");
724 strcpy (newpath, PATH_SET);
725 strcat (newpath, ":");
726 strcat (newpath, oldpath);
727 putenv (newpath);
728 } else
729 putenv (PATH_SET);
730 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500731#ifdef CONFIG_JBD_DEBUG
732 if (getenv("E2FSCK_JBD_DEBUG"))
733 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
734#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735 return 0;
736}
737
738static const char *my_ver_string = E2FSPROGS_VERSION;
739static const char *my_ver_date = E2FSPROGS_DATE;
740
741int main (int argc, char *argv[])
742{
743 errcode_t retval = 0;
744 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000745 ext2_filsys fs = 0;
746 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000747 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000748 const char *lib_ver_date;
749 int my_ver, lib_ver;
750 e2fsck_t ctx;
751 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000752 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000753
754 clear_problem_context(&pctx);
755#ifdef MTRACE
756 mtrace();
757#endif
758#ifdef MCHECK
759 mcheck(0);
760#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000761#ifdef ENABLE_NLS
762 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500763 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000764 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
765 textdomain(NLS_CAT_NAME);
766#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000767 my_ver = ext2fs_parse_version_string(my_ver_string);
768 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
769 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000770 fprintf( stderr, _("Error: ext2fs library version "
771 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000772 show_version_only++;
773 }
774
775 retval = PRS(argc, argv, &ctx);
776 if (retval) {
777 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000778 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000779 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000780 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000781 reserve_stdio_fds();
782
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000783#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000784 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000785#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000786
787 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400788 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400789 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000790
791 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000792 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000793 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000794 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000795 }
796
797 check_mount(ctx);
798
799 if (!(ctx->options & E2F_OPT_PREEN) &&
800 !(ctx->options & E2F_OPT_NO) &&
801 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400802 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000803 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000804 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000805 }
806 ctx->superblock = ctx->use_superblock;
807restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400808#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000809 io_ptr = test_io_manager;
810 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400811#else
812 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000813#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000814 flags = 0;
815 if ((ctx->options & E2F_OPT_READONLY) == 0)
816 flags |= EXT2_FLAG_RW;
817
Theodore Ts'of1a17612001-12-23 22:27:52 -0500818 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000819 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500820 ctx->superblock, ctx->blocksize,
821 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000822 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600823 int blocksize;
824 for (blocksize = EXT2_MIN_BLOCK_SIZE;
825 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000826 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600827 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000828 io_ptr, &fs);
829 if (!retval)
830 break;
831 }
832 } else
833 retval = ext2fs_open(ctx->filesystem_name, flags,
834 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400835 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
836 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000837 ((retval == EXT2_ET_BAD_MAGIC) ||
838 ((retval == 0) && ext2fs_check_desc(fs)))) {
839 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000840 printf(_("%s trying backup blocks...\n"),
841 retval ? _("Couldn't find ext2 superblock,") :
842 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500843 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000844 if (fs)
845 ext2fs_close(fs);
846 goto restart;
847 }
848 }
849 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000850 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000851 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000852 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000853 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000854 "too high for this version of e2fsck.\n"
855 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000856 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000857 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
858 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000859 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000860 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000861 printf(_("You must have %s access to the "
862 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000863 (ctx->options & E2F_OPT_READONLY) ?
864 "r/o" : "r/w");
865 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000866 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000867#ifdef EROFS
868 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000869 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000870 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000871 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000872#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000873 else
874 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000875 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000876 }
877 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000878 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000879 sb = fs->super;
880 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000881 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000882 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000883 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000884 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000885 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000886 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000887
Theodore Ts'o17390c02000-07-07 04:13:21 +0000888 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000889 * Set the device name, which is used whenever we print error
890 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000891 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000892 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000893 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500894 ctx->device_name = string_copy(ctx, sb->s_volume_name,
895 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000896 }
897 if (ctx->device_name == 0)
898 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000899
900 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000901 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000902 */
903 retval = e2fsck_check_ext3_journal(ctx);
904 if (retval) {
905 com_err(ctx->program_name, retval,
906 _("while checking ext3 journal for %s"),
907 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000908 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000909 }
910
Theodore Ts'o9b565752000-12-13 18:50:22 +0000911 /*
912 * Check to see if we need to do ext3-style recovery. If so,
913 * do it, and then restart the fsck.
914 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000915 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000916 if (ctx->options & E2F_OPT_READONLY) {
917 printf(_("Warning: skipping journal recovery "
918 "because doing a read-only filesystem "
919 "check.\n"));
920 io_channel_flush(ctx->fs->io);
921 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500922 if (ctx->flags & E2F_FLAG_RESTARTED) {
923 /*
924 * Whoops, we attempted to run the
925 * journal twice. This should never
926 * happen, unless the hardware or
927 * device driver is being bogus.
928 */
929 com_err(ctx->program_name, 0,
930 _("unable to set superblock flags on %s\n"), ctx->device_name);
931 fatal_error(ctx, 0);
932 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000933 retval = e2fsck_run_ext3_journal(ctx);
934 if (retval) {
935 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000936 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000937 ctx->device_name);
938 fatal_error(ctx, 0);
939 }
940 ext2fs_close(ctx->fs);
941 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500942 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000943 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000944 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000945 }
946
947 /*
948 * Check for compatibility with the feature sets. We need to
949 * be more stringent than ext2fs_open().
950 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000951 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
952 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000953 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
954 "(%s)", ctx->device_name);
955 goto get_newer;
956 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000957 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000958 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
959 "(%s)", ctx->device_name);
960 goto get_newer;
961 }
962#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000963 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000964 com_err(ctx->program_name, 0,
965 _("Warning: compression support is experimental.\n"));
966#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400967#ifndef ENABLE_HTREE
968 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
969 com_err(ctx->program_name, 0,
970 _("E2fsck not compiled with HTREE support,\n\t"
971 "but filesystem %s has HTREE directories.\n"),
972 ctx->device_name);
973 goto get_newer;
974 }
975#endif
976
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000977 /*
978 * If the user specified a specific superblock, presumably the
979 * master superblock has been trashed. So we mark the
980 * superblock as dirty, so it can be written out.
981 */
982 if (ctx->superblock &&
983 !(ctx->options & E2F_OPT_READONLY))
984 ext2fs_mark_super_dirty(fs);
985
986 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400987 * We only update the master superblock because (a) paranoia;
988 * we don't want to corrupt the backup superblocks, and (b) we
989 * don't need to update the mount count and last checked
990 * fields in the backup superblock (the kernel doesn't
991 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000992 */
993 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
994
995 ehandler_init(fs->io);
996
997 if (ctx->superblock)
998 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
999 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001000 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001001 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001002 check_if_skip(ctx);
1003 if (bad_blocks_file)
1004 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1005 else if (cflag)
1006 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001007 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001008 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001009#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001010 if (normalize_swapfs) {
1011 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1012 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001013 fprintf(stderr, _("%s: Filesystem byte order "
1014 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001015 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001016 }
1017 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001018 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001019 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001020 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001021 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001022 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001023#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001024
1025 /*
1026 * Mark the system as valid, 'til proven otherwise
1027 */
1028 ext2fs_mark_valid(fs);
1029
1030 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1031 if (retval) {
1032 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001033 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001034 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001035 printf(_("This doesn't bode well,"
1036 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001037 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001038
1039 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001040 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001041 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001042 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001043 retval = e2fsck_reset_context(ctx);
1044 if (retval) {
1045 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001046 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001047 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001048 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001049 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001050 goto restart;
1051 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001052 if (run_result & E2F_FLAG_CANCEL) {
1053 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1054 ctx->device_name : ctx->filesystem_name);
1055 exit_value |= FSCK_CANCELED;
1056 }
1057 if (run_result & E2F_FLAG_ABORT)
1058 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001059
1060#ifdef MTRACE
1061 mtrace_print("Cleanup");
1062#endif
1063 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001064 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001065 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001066 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001067 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001068 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001069 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001070 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001071 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001072 }
1073 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001074 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001075 printf(_("\n%s: ********** WARNING: Filesystem still has "
1076 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001077 exit_value |= FSCK_UNCORRECTED;
1078 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001079 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001080 if (exit_value & FSCK_CANCELED)
1081 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001082 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001083 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001084 if (!(ctx->options & E2F_OPT_READONLY)) {
1085 if (ext2fs_test_valid(fs)) {
1086 if (!(sb->s_state & EXT2_VALID_FS))
1087 exit_value |= FSCK_NONDESTRUCT;
1088 sb->s_state = EXT2_VALID_FS;
1089 } else
1090 sb->s_state &= ~EXT2_VALID_FS;
1091 sb->s_mnt_count = 0;
1092 sb->s_lastcheck = time(NULL);
1093 ext2fs_mark_super_dirty(fs);
1094 }
1095 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001096
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001097 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001098
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001099 ext2fs_close(fs);
1100 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001101 free(ctx->filesystem_name);
1102 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001103 e2fsck_free_context(ctx);
1104
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001105#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001106 if (ctx->options & E2F_OPT_TIME)
1107 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001108#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001109
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001110 return exit_value;
1111}