blob: 33722e387dfd6cec859dd7821ad9e23ccb6ef000 [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'o1b6bf171997-10-03 17:48:10 +0000212
Theodore Ts'od37066a2001-12-21 23:28:54 -0500213 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
214 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000215 return;
216
217 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000218 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000219 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000220 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000221 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000222 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000223 (unsigned) fs->super->s_max_mnt_count)) {
224 reason = _(" has been mounted %u times without being checked");
225 reason_arg = fs->super->s_mnt_count;
226 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000227 time(0) >= (fs->super->s_lastcheck +
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000228 fs->super->s_checkinterval)) {
229 reason = _(" has gone %u days without being checked");
230 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
231 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000232 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000233 fputs(ctx->device_name, stdout);
234 printf(reason, reason_arg);
235 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000236 return;
237 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000238 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000239 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
240 fs->super->s_inodes_count,
241 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
242 fs->super->s_blocks_count);
243 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400244 ctx->fs = NULL;
245 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000246 exit(FSCK_OK);
247}
248
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000249/*
250 * For completion notice
251 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000252struct percent_tbl {
253 int max_pass;
254 int table[32];
255};
256struct percent_tbl e2fsck_tbl = {
257 5, { 0, 70, 90, 92, 95, 100 }
258};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400259static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000260
261static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
262 int max)
263{
264 float percent;
265
266 if (pass <= 0)
267 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000268 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000269 return 100.0;
270 percent = ((float) curr) / ((float) max);
271 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
272 + tbl->table[pass-1]);
273}
274
275extern void e2fsck_clear_progbar(e2fsck_t ctx)
276{
277 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
278 return;
279
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400280 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
281 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400282 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000283 ctx->flags &= ~E2F_FLAG_PROG_BAR;
284}
285
Theodore Ts'o520ead32003-04-19 13:48:27 -0400286int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500287 unsigned int dpynum)
288{
289 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500290 int i;
291 int tick;
292 struct timeval tv;
293 int dpywidth;
294
295 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
296 return 0;
297
298 /*
299 * Calculate the new progress position. If the
300 * percentage hasn't changed, then we skip out right
301 * away.
302 */
303 if (ctx->progress_last_percent == (int) 10 * percent)
304 return 0;
305 ctx->progress_last_percent = (int) 10 * percent;
306
307 /*
308 * If we've already updated the spinner once within
309 * the last 1/8th of a second, no point doing it
310 * again.
311 */
312 gettimeofday(&tv, NULL);
313 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
314 if ((tick == ctx->progress_last_time) &&
315 (percent != 0.0) && (percent != 100.0))
316 return 0;
317 ctx->progress_last_time = tick;
318
319 /*
320 * Advance the spinner, and note that the progress bar
321 * will be on the screen
322 */
323 ctx->progress_pos = (ctx->progress_pos+1) & 3;
324 ctx->flags |= E2F_FLAG_PROG_BAR;
325
326 dpywidth = 66 - strlen(label);
327 dpywidth = 8 * (dpywidth / 8);
328 if (dpynum)
329 dpywidth -= 8;
330
331 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400332 printf("%s%s: |%s%s", ctx->start_meta, label,
333 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500334 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
335 if (percent == 100.0)
336 fputc('|', stdout);
337 else
338 fputc(spinner[ctx->progress_pos & 3], stdout);
339 if (dpynum)
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400340 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500341 else
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400342 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500343
344 if (percent == 100.0)
345 e2fsck_clear_progbar(ctx);
346 fflush(stdout);
347
348 return 0;
349}
350
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000351static int e2fsck_update_progress(e2fsck_t ctx, int pass,
352 unsigned long cur, unsigned long max)
353{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000354 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000355 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000356
357 if (pass == 0)
358 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000359
360 if (ctx->progress_fd) {
361 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
362 write(ctx->progress_fd, buf, strlen(buf));
363 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000364 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500365 e2fsck_simple_progress(ctx, ctx->device_name,
366 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000367 }
368 return 0;
369}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000370
371#define PATH_SET "PATH=/sbin"
372
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000373static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000374{
375 int fd;
376
377 while (1) {
378 fd = open("/dev/null", O_RDWR);
379 if (fd > 2)
380 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000381 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000382 fprintf(stderr, _("ERROR: Couldn't open "
383 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000384 strerror(errno));
385 break;
386 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000387 }
388 close(fd);
389}
390
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000391#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000392static void signal_progress_on(int sig)
393{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000394 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000395
396 if (!ctx)
397 return;
398
399 ctx->progress = e2fsck_update_progress;
400 ctx->progress_fd = 0;
401}
402
403static void signal_progress_off(int sig)
404{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000405 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000406
407 if (!ctx)
408 return;
409
410 e2fsck_clear_progbar(ctx);
411 ctx->progress = 0;
412}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400413
414static void signal_cancel(int sig)
415{
416 e2fsck_t ctx = e2fsck_global_ctx;
417
418 if (!ctx)
419 exit(FSCK_CANCELED);
420
421 ctx->flags |= E2F_FLAG_CANCEL;
422}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000423#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000424
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400425static void parse_extended_opts(e2fsck_t ctx, const char *opts)
426{
427 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500428 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400429 int extended_usage = 0;
430
Theodore Ts'of3640932003-03-01 19:47:44 -0500431 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400432 for (token = buf; token && *token; token = next) {
433 p = strchr(token, ',');
434 next = 0;
435 if (p) {
436 *p = 0;
437 next = p+1;
438 }
439 arg = strchr(token, '=');
440 if (arg) {
441 *arg = 0;
442 arg++;
443 }
444 if (strcmp(token, "ea_ver") == 0) {
445 if (!arg) {
446 extended_usage++;
447 continue;
448 }
449 ea_ver = strtoul(arg, &p, 0);
450 if (*p ||
451 ((ea_ver != 1) && (ea_ver != 2))) {
452 fprintf(stderr,
453 _("Invalid EA version.\n"));
454 extended_usage++;
455 continue;
456 }
457 ctx->ext_attr_ver = ea_ver;
458 } else
459 extended_usage++;
460 }
461 if (extended_usage) {
462 fprintf(stderr, _("Extended options are separated by commas, "
463 "and may take an argument which\n"
464 "is set off by an equals ('=') sign. "
465 "Valid raid options are:\n"
466 "\tea_ver=<ea_version (1 or 2)\n\n"));
467 exit(1);
468 }
469}
470
471
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000472static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
473{
474 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000475 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000476#ifdef MTRACE
477 extern void *mallwatch;
478#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000479 e2fsck_t ctx;
480 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000481#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000482 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000483#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400484 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000485
486 retval = e2fsck_allocate_context(&ctx);
487 if (retval)
488 return retval;
489
490 *ret_ctx = ctx;
491
Theodore Ts'o908b7852003-04-16 15:20:13 -0400492 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
493 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400494 if (isatty(0) && isatty(1)) {
495 ctx->interactive = 1;
496 } else {
497 ctx->start_meta[0] = '\001';
498 ctx->stop_meta[0] = '\002';
499 }
500 memset(bar, '=', sizeof(bar)-1);
501 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500503 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000504
505 if (argc && *argv)
506 ctx->program_name = *argv;
507 else
508 ctx->program_name = "e2fsck";
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400509 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 +0000510 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000511 case 'C':
512 ctx->progress = e2fsck_update_progress;
513 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000514 if (!ctx->progress_fd)
515 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000516 /* Validate the file descriptor to avoid disasters */
517 fd = dup(ctx->progress_fd);
518 if (fd < 0) {
519 fprintf(stderr,
520 _("Error validating file descriptor %d: %s\n"),
521 ctx->progress_fd,
522 error_message(errno));
523 fatal_error(ctx,
524 _("Invalid completion information file descriptor"));
525 } else
526 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000527 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400528 case 'D':
529 ctx->options |= E2F_OPT_COMPRESS_DIRS;
530 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400531 case 'E':
532 extended_opts = optarg;
533 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000534 case 'p':
535 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500536 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
537 conflict_opt:
538 fatal_error(ctx,
539 _("Only one the options -p/-a, -n or -y may be specified."));
540 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000541 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000542 break;
543 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500544 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
545 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000546 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000547 break;
548 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500549 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
550 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000552 break;
553 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000554#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000555 if (ctx->options & E2F_OPT_TIME)
556 ctx->options |= E2F_OPT_TIME2;
557 else
558 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000559#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000560 fprintf(stderr, _("The -t option is not "
561 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000562#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000563 break;
564 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500565 if (cflag++)
566 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000567 ctx->options |= E2F_OPT_CHECKBLOCKS;
568 break;
569 case 'r':
570 /* What we do by default, anyway! */
571 break;
572 case 'b':
573 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400574 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 break;
576 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500577 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 break;
579 case 'I':
580 ctx->inode_buffer_blocks = atoi(optarg);
581 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400582 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500583 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400584 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000585 case 'P':
586 ctx->process_inode_size = atoi(optarg);
587 break;
588 case 'L':
589 replace_bad_blocks++;
590 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500591 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000592 break;
593 case 'd':
594 ctx->options |= E2F_OPT_DEBUG;
595 break;
596 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500597 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000598 break;
599 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000600 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000601 break;
602 case 'v':
603 verbose = 1;
604 break;
605 case 'V':
606 show_version_only = 1;
607 break;
608#ifdef MTRACE
609 case 'M':
610 mallwatch = (void *) strtol(optarg, NULL, 0);
611 break;
612#endif
613 case 'N':
614 ctx->device_name = optarg;
615 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000616#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 case 's':
618 normalize_swapfs = 1;
619 case 'S':
620 swapfs = 1;
621 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000622#else
623 case 's':
624 case 'S':
625 fprintf(stderr, _("Byte-swapping filesystems "
626 "not compiled in this version "
627 "of e2fsck\n"));
628 exit(1);
629#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000630 default:
631 usage(ctx);
632 }
633 if (show_version_only)
634 return 0;
635 if (optind != argc - 1)
636 usage(ctx);
637 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400638 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000639 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'of3640932003-03-01 19:47:44 -0500640 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400641 if (extended_opts)
642 parse_extended_opts(ctx, extended_opts);
643
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000644 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500645 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000646 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000647 com_err("open", errno,
648 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000649 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000650 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000651 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000652 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000653 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000654 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000655 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000656 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000657 }
658 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000659 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000660#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000661 if (swapfs) {
662 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000663 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500664 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000665 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000666 }
667 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000668#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500669 if (cflag && bad_blocks_file) {
670 fprintf(stderr, _("The -c and the -l/-L options may "
671 "not be both used at the same time.\n"));
672 exit(FSCK_USAGE);
673 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000674#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000675 /*
676 * Set up signal action
677 */
678 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400679 sa.sa_handler = signal_cancel;
680 sigaction(SIGINT, &sa, 0);
681 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000682#ifdef SA_RESTART
683 sa.sa_flags = SA_RESTART;
684#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000685 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000686 sa.sa_handler = signal_progress_on;
687 sigaction(SIGUSR1, &sa, 0);
688 sa.sa_handler = signal_progress_off;
689 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000690#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400691
692 /* Update our PATH to include /sbin if we need to run badblocks */
693 if (cflag) {
694 char *oldpath = getenv("PATH");
695 if (oldpath) {
696 char *newpath;
697
698 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
699 strlen (oldpath));
700 if (!newpath)
701 fatal_error(ctx, "Couldn't malloc() newpath");
702 strcpy (newpath, PATH_SET);
703 strcat (newpath, ":");
704 strcat (newpath, oldpath);
705 putenv (newpath);
706 } else
707 putenv (PATH_SET);
708 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500709#ifdef CONFIG_JBD_DEBUG
710 if (getenv("E2FSCK_JBD_DEBUG"))
711 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
712#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000713 return 0;
714}
715
716static const char *my_ver_string = E2FSPROGS_VERSION;
717static const char *my_ver_date = E2FSPROGS_DATE;
718
719int main (int argc, char *argv[])
720{
721 errcode_t retval = 0;
722 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000723 ext2_filsys fs = 0;
724 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000725 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000726 const char *lib_ver_date;
727 int my_ver, lib_ver;
728 e2fsck_t ctx;
729 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000730 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000731
732 clear_problem_context(&pctx);
733#ifdef MTRACE
734 mtrace();
735#endif
736#ifdef MCHECK
737 mcheck(0);
738#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000739#ifdef ENABLE_NLS
740 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500741 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000742 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
743 textdomain(NLS_CAT_NAME);
744#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000745 my_ver = ext2fs_parse_version_string(my_ver_string);
746 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
747 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000748 fprintf( stderr, _("Error: ext2fs library version "
749 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000750 show_version_only++;
751 }
752
753 retval = PRS(argc, argv, &ctx);
754 if (retval) {
755 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000756 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000757 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000758 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000759 reserve_stdio_fds();
760
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000761#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000762 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000763#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000764
765 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400766 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400767 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000768
769 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000770 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000771 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000772 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000773 }
774
775 check_mount(ctx);
776
777 if (!(ctx->options & E2F_OPT_PREEN) &&
778 !(ctx->options & E2F_OPT_NO) &&
779 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400780 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000781 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000782 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000783 }
784 ctx->superblock = ctx->use_superblock;
785restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400786#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000787 io_ptr = test_io_manager;
788 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400789#else
790 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000791#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000792 flags = 0;
793 if ((ctx->options & E2F_OPT_READONLY) == 0)
794 flags |= EXT2_FLAG_RW;
795
Theodore Ts'of1a17612001-12-23 22:27:52 -0500796 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000797 retval = ext2fs_open(ctx->filesystem_name, flags,
Theodore Ts'of1a17612001-12-23 22:27:52 -0500798 ctx->superblock, ctx->blocksize,
799 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000800 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600801 int blocksize;
802 for (blocksize = EXT2_MIN_BLOCK_SIZE;
803 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000804 retval = ext2fs_open(ctx->filesystem_name, flags,
Andreas Dilger932a4892002-05-16 03:20:07 -0600805 ctx->superblock, blocksize,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000806 io_ptr, &fs);
807 if (!retval)
808 break;
809 }
810 } else
811 retval = ext2fs_open(ctx->filesystem_name, flags,
812 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400813 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
814 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000815 ((retval == EXT2_ET_BAD_MAGIC) ||
816 ((retval == 0) && ext2fs_check_desc(fs)))) {
817 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000818 printf(_("%s trying backup blocks...\n"),
819 retval ? _("Couldn't find ext2 superblock,") :
820 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500821 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000822 if (fs)
823 ext2fs_close(fs);
824 goto restart;
825 }
826 }
827 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000828 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000829 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000830 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000831 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000832 "too high for this version of e2fsck.\n"
833 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000834 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000835 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
836 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000837 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000838 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000839 printf(_("You must have %s access to the "
840 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000841 (ctx->options & E2F_OPT_READONLY) ?
842 "r/o" : "r/w");
843 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000844 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000845#ifdef EROFS
846 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000847 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000848 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000849 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000850#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000851 else
852 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000853 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000854 }
855 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000856 fs->priv_data = ctx;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000857 sb = fs->super;
858 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000859 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000860 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000861 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000862 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000863 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000864 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000865
Theodore Ts'o17390c02000-07-07 04:13:21 +0000866 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000867 * Set the device name, which is used whenever we print error
868 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000869 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000870 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000871 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500872 ctx->device_name = string_copy(ctx, sb->s_volume_name,
873 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000874 }
875 if (ctx->device_name == 0)
876 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000877
878 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000879 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000880 */
881 retval = e2fsck_check_ext3_journal(ctx);
882 if (retval) {
883 com_err(ctx->program_name, retval,
884 _("while checking ext3 journal for %s"),
885 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000886 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000887 }
888
Theodore Ts'o9b565752000-12-13 18:50:22 +0000889 /*
890 * Check to see if we need to do ext3-style recovery. If so,
891 * do it, and then restart the fsck.
892 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000893 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000894 if (ctx->options & E2F_OPT_READONLY) {
895 printf(_("Warning: skipping journal recovery "
896 "because doing a read-only filesystem "
897 "check.\n"));
898 io_channel_flush(ctx->fs->io);
899 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500900 if (ctx->flags & E2F_FLAG_RESTARTED) {
901 /*
902 * Whoops, we attempted to run the
903 * journal twice. This should never
904 * happen, unless the hardware or
905 * device driver is being bogus.
906 */
907 com_err(ctx->program_name, 0,
908 _("unable to set superblock flags on %s\n"), ctx->device_name);
909 fatal_error(ctx, 0);
910 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000911 retval = e2fsck_run_ext3_journal(ctx);
912 if (retval) {
913 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000914 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000915 ctx->device_name);
916 fatal_error(ctx, 0);
917 }
918 ext2fs_close(ctx->fs);
919 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500920 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000921 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000922 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000923 }
924
925 /*
926 * Check for compatibility with the feature sets. We need to
927 * be more stringent than ext2fs_open().
928 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000929 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
930 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000931 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
932 "(%s)", ctx->device_name);
933 goto get_newer;
934 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000935 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000936 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
937 "(%s)", ctx->device_name);
938 goto get_newer;
939 }
940#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000941 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000942 com_err(ctx->program_name, 0,
943 _("Warning: compression support is experimental.\n"));
944#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400945#ifndef ENABLE_HTREE
946 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
947 com_err(ctx->program_name, 0,
948 _("E2fsck not compiled with HTREE support,\n\t"
949 "but filesystem %s has HTREE directories.\n"),
950 ctx->device_name);
951 goto get_newer;
952 }
953#endif
954
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000955 /*
956 * If the user specified a specific superblock, presumably the
957 * master superblock has been trashed. So we mark the
958 * superblock as dirty, so it can be written out.
959 */
960 if (ctx->superblock &&
961 !(ctx->options & E2F_OPT_READONLY))
962 ext2fs_mark_super_dirty(fs);
963
964 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400965 * We only update the master superblock because (a) paranoia;
966 * we don't want to corrupt the backup superblocks, and (b) we
967 * don't need to update the mount count and last checked
968 * fields in the backup superblock (the kernel doesn't
969 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000970 */
971 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
972
973 ehandler_init(fs->io);
974
975 if (ctx->superblock)
976 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
977 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000978 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000979 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000980 check_if_skip(ctx);
981 if (bad_blocks_file)
982 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
983 else if (cflag)
984 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000985 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000986 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000987#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000988 if (normalize_swapfs) {
989 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
990 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000991 fprintf(stderr, _("%s: Filesystem byte order "
992 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000993 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000994 }
995 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000996 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000997 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000998 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000999 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001000 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001001#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001002
1003 /*
1004 * Mark the system as valid, 'til proven otherwise
1005 */
1006 ext2fs_mark_valid(fs);
1007
1008 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1009 if (retval) {
1010 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001011 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001012 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001013 printf(_("This doesn't bode well,"
1014 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001015 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001016
1017 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001018 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001019 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001020 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001021 retval = e2fsck_reset_context(ctx);
1022 if (retval) {
1023 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001024 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001025 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001026 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001027 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001028 goto restart;
1029 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001030 if (run_result & E2F_FLAG_CANCEL) {
1031 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1032 ctx->device_name : ctx->filesystem_name);
1033 exit_value |= FSCK_CANCELED;
1034 }
1035 if (run_result & E2F_FLAG_ABORT)
1036 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001037
1038#ifdef MTRACE
1039 mtrace_print("Cleanup");
1040#endif
1041 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001042 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001043 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001044 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001045 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001046 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001047 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001048 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001049 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001050 }
1051 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001052 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001053 printf(_("\n%s: ********** WARNING: Filesystem still has "
1054 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001055 exit_value |= FSCK_UNCORRECTED;
1056 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001057 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001058 if (exit_value & FSCK_CANCELED)
1059 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001060 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001061 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001062 if (!(ctx->options & E2F_OPT_READONLY)) {
1063 if (ext2fs_test_valid(fs)) {
1064 if (!(sb->s_state & EXT2_VALID_FS))
1065 exit_value |= FSCK_NONDESTRUCT;
1066 sb->s_state = EXT2_VALID_FS;
1067 } else
1068 sb->s_state &= ~EXT2_VALID_FS;
1069 sb->s_mnt_count = 0;
1070 sb->s_lastcheck = time(NULL);
1071 ext2fs_mark_super_dirty(fs);
1072 }
1073 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001074
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001075 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001076
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001077 ext2fs_close(fs);
1078 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001079 free(ctx->filesystem_name);
1080 free(ctx->journal_name);
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001081 e2fsck_free_context(ctx);
1082
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001083#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001084 if (ctx->options & E2F_OPT_TIME)
1085 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001086#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001087
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001088 return exit_value;
1089}