blob: fbe598044e12578133ee815373fbc8f0807b6ee0 [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>
25#endif
26#include <unistd.h>
27#ifdef HAVE_ERRNO_H
28#include <errno.h>
29#endif
30#ifdef HAVE_MNTENT_H
31#include <mntent.h>
32#endif
33#include <sys/ioctl.h>
34#include <malloc.h>
35
36#include "et/com_err.h"
37#include "e2fsck.h"
38#include "problem.h"
39#include "../version.h"
40
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000041/* Command line options */
42static int blocksize = 0;
43static int swapfs = 0;
44static int normalize_swapfs = 0;
45static int cflag = 0; /* check disk */
46static int show_version_only = 0;
47static int force = 0;
48static int verbose = 0;
49
50static int replace_bad_blocks = 0;
51static char *bad_blocks_file = 0;
52
53static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
54
55static int root_filesystem = 0;
56static int read_only_root = 0;
57
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000058static void usage(e2fsck_t ctx)
59{
60 fprintf(stderr,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000061 _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000062 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000063 "\t\t[-l|-L bad_blocks_file] [-C fd] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000064 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000065
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000066 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000067 " -p Automatic repair (no questions)\n"
68 " -n Make no changes to the filesystem\n"
69 " -y Assume \"yes\" to all questions\n"
70 " -c Check for bad blocks\n"
71 " -f Force checking even if filesystem is marked clean\n"
72 " -v Be verbose\n"
73 " -b superblock Use alternative superblock\n"
74 " -B blocksize Force blocksize when looking for superblock\n"
75 " -l bad_blocks_file Add to badblocks list\n"
76 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000077 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000078
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000079 exit(FSCK_USAGE);
80}
81
82static void show_stats(e2fsck_t ctx)
83{
84 ext2_filsys fs = ctx->fs;
85 int inodes, inodes_used, blocks, blocks_used;
86 int dir_links;
87 int num_files, num_links;
88 int frag_percent;
89
90 dir_links = 2 * ctx->fs_directory_count - 1;
91 num_files = ctx->fs_total_count - dir_links;
92 num_links = ctx->fs_links_count - dir_links;
93 inodes = fs->super->s_inodes_count;
94 inodes_used = (fs->super->s_inodes_count -
95 fs->super->s_free_inodes_count);
96 blocks = fs->super->s_blocks_count;
97 blocks_used = (fs->super->s_blocks_count -
98 fs->super->s_free_blocks_count);
99
100 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
101 frag_percent = (frag_percent + 5) / 10;
102
103 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000104 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000105 ctx->device_name, inodes_used, inodes,
106 frag_percent / 10, frag_percent % 10,
107 blocks_used, blocks);
108 return;
109 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000110 /*
111 * This is a bit ugly. But I think there will nearly always be more
112 * than one "thing" to report about, so I won't try writing complex
113 * code to handle one/two/many forms of all words.
114 * Some languages (Italian, at least) never uses the plural form
115 * of foreign words, so in real life this could not be a problem.
116 * md@linux.it - 2000-1-15
117 */
118#ifdef ENABLE_NLS
119 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
120 (inodes_used != 1), 100 * inodes_used / inodes);
121 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
122 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
123 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
124 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
125 printf (_("%8d blocks used (%d%%)\n"
126 "%8d bad blocks\n"), blocks_used,
127 100 * blocks_used / blocks, ctx->fs_badblocks_count);
128 printf (_("\n%8d regular files\n"
129 "%8d directories\n"
130 "%8d character device files\n"
131 "%8d block device files\n"
132 "%8d fifos\n"
133 "%8d links\n"
134 "%8d symbolic links (%d fast symbolic links)\n"
135 "%8d sockets\n"
136 "--------\n"
137 "%8d files\n"),
138 ctx->fs_regular_count,
139 ctx->fs_directory_count,
140 ctx->fs_chardev_count,
141 ctx->fs_blockdev_count,
142 ctx->fs_fifo_count,
143 ctx->fs_links_count - dir_links,
144 ctx->fs_symlinks_count,
145 ctx->fs_fast_symlinks_count,
146 ctx->fs_sockets_count,
147 ctx->fs_total_count - dir_links);
148#else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000149 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
150 (inodes_used != 1) ? "s" : "",
151 100 * inodes_used / inodes);
152 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
153 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
154 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
155 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
156 printf ("%8d block%s used (%d%%)\n"
157 "%8d bad block%s\n", blocks_used,
158 (blocks_used != 1) ? "s" : "",
159 100 * blocks_used / blocks, ctx->fs_badblocks_count,
160 ctx->fs_badblocks_count != 1 ? "s" : "");
161 printf ("\n%8d regular file%s\n"
162 "%8d director%s\n"
163 "%8d character device file%s\n"
164 "%8d block device file%s\n"
165 "%8d fifo%s\n"
166 "%8d link%s\n"
167 "%8d symbolic link%s (%d fast symbolic link%s)\n"
168 "%8d socket%s\n"
169 "--------\n"
170 "%8d file%s\n",
171 ctx->fs_regular_count,
172 (ctx->fs_regular_count != 1) ? "s" : "",
173 ctx->fs_directory_count,
174 (ctx->fs_directory_count != 1) ? "ies" : "y",
175 ctx->fs_chardev_count,
176 (ctx->fs_chardev_count != 1) ? "s" : "",
177 ctx->fs_blockdev_count,
178 (ctx->fs_blockdev_count != 1) ? "s" : "",
179 ctx->fs_fifo_count,
180 (ctx->fs_fifo_count != 1) ? "s" : "",
181 ctx->fs_links_count - dir_links,
182 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
183 ctx->fs_symlinks_count,
184 (ctx->fs_symlinks_count != 1) ? "s" : "",
185 ctx->fs_fast_symlinks_count,
186 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
187 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
188 ctx->fs_total_count - dir_links,
189 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000190#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000191}
192
193static void check_mount(e2fsck_t ctx)
194{
195 errcode_t retval;
196 int mount_flags, cont, fd;
197
198 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
199 if (retval) {
200 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000201 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202 ctx->filesystem_name);
203 return;
204 }
205 if (!(mount_flags & EXT2_MF_MOUNTED))
206 return;
207
208#if (defined(__linux__) && defined(HAVE_MNTENT_H))
209 /*
210 * If the root is mounted read-only, then /etc/mtab is
211 * probably not correct; so we won't issue a warning based on
212 * it.
213 */
214 fd = open(MOUNTED, O_RDWR);
215 if (fd < 0) {
216 if (errno == EROFS)
217 return;
218 } else
219 close(fd);
220#endif
221
222 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000223 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000224 return;
225 }
226
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000227 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o74033351999-07-01 03:00:47 +0000228 if (!isatty(0) || !isatty(1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000229 printf(_("Cannot continue, aborting.\n\n"));
Theodore Ts'o74033351999-07-01 03:00:47 +0000230 exit(FSCK_ERROR);
231 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000232 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000233 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000234 "SEVERE filesystem damage.\007\007\007\n\n"));
235 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000236 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000237 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000238 exit (0);
239 }
240 return;
241}
242
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000243/*
244 * This routine checks to see if a filesystem can be skipped; if so,
245 * it will exit with E2FSCK_OK. Under some conditions it will print a
246 * message explaining why a check is being forced.
247 */
248static void check_if_skip(e2fsck_t ctx)
249{
250 ext2_filsys fs = ctx->fs;
251 const char *reason = NULL;
252
253 if (force || bad_blocks_file || cflag || swapfs)
254 return;
255
256 if (fs->super->s_state & EXT2_ERROR_FS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000257 reason = _("contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000258 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000259 reason = _("was not cleanly unmounted");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000260 else if (fs->super->s_mnt_count >=
261 (unsigned) fs->super->s_max_mnt_count)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000262 reason = _("has reached maximal mount count");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000263 else if (fs->super->s_checkinterval &&
264 time(0) >= (fs->super->s_lastcheck +
265 fs->super->s_checkinterval))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000266 reason = _("has gone too long without being checked");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000267 if (reason) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000268 printf(_("%s %s, check forced.\n"), ctx->device_name, reason);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000269 return;
270 }
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000271 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000272 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
273 fs->super->s_inodes_count,
274 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
275 fs->super->s_blocks_count);
276 ext2fs_close(fs);
277 exit(FSCK_OK);
278}
279
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000280/*
281 * For completion notice
282 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000283struct percent_tbl {
284 int max_pass;
285 int table[32];
286};
287struct percent_tbl e2fsck_tbl = {
288 5, { 0, 70, 90, 92, 95, 100 }
289};
290static int dpywidth = 50;
291static char bar[] =
292 "==============================================================="
293 "===============================================================";
294static char spaces[] =
295 " "
296 " ";
297
298static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
299 int max)
300{
301 float percent;
302
303 if (pass <= 0)
304 return 0.0;
305 if (pass > tbl->max_pass)
306 return 100.0;
307 percent = ((float) curr) / ((float) max);
308 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
309 + tbl->table[pass-1]);
310}
311
312extern void e2fsck_clear_progbar(e2fsck_t ctx)
313{
314 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
315 return;
316
317 printf("%s\r", spaces + (sizeof(spaces) - 80));
318 ctx->flags &= ~E2F_FLAG_PROG_BAR;
319}
320
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000321static int e2fsck_update_progress(e2fsck_t ctx, int pass,
322 unsigned long cur, unsigned long max)
323{
324 const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000325 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000326 int i;
327 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000328
329 if (pass == 0)
330 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000331
332 if (ctx->progress_fd) {
333 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
334 write(ctx->progress_fd, buf, strlen(buf));
335 } else {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000336 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
337 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000338 ctx->progress_pos = (ctx->progress_pos+1) & 3;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000339 ctx->flags |= E2F_FLAG_PROG_BAR;
340 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
341 if (ctx->progress_last_percent == (int) 1000 * percent)
342 return 0;
343 ctx->progress_last_percent = (int) 1000 * percent;
344 i = ((percent * dpywidth) + 50) / 100;
345 printf("%s: |%s%s", ctx->device_name,
346 bar + (sizeof(bar) - (i+1)),
347 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
348 if (percent == 100.0)
349 fputc('|', stdout);
350 else
351 fputc(spinner[ctx->progress_pos & 3], stdout);
352 printf(" %4.1f%% \r", percent);
353 if (percent == 100.0)
354 e2fsck_clear_progbar(ctx);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000355 fflush(stdout);
356 }
357 return 0;
358}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000359
360#define PATH_SET "PATH=/sbin"
361
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000362static void reserve_stdio_fds(NOARGS)
363{
364 int fd;
365
366 while (1) {
367 fd = open("/dev/null", O_RDWR);
368 if (fd > 2)
369 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000370 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000371 fprintf(stderr, _("ERROR: Couldn't open "
372 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000373 strerror(errno));
374 break;
375 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000376 }
377 close(fd);
378}
379
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000380#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000381static e2fsck_t global_signal_ctx;
382
383static void signal_progress_on(int sig)
384{
385 e2fsck_t ctx = global_signal_ctx;
386
387 if (!ctx)
388 return;
389
390 ctx->progress = e2fsck_update_progress;
391 ctx->progress_fd = 0;
392}
393
394static void signal_progress_off(int sig)
395{
396 e2fsck_t ctx = global_signal_ctx;
397
398 if (!ctx)
399 return;
400
401 e2fsck_clear_progbar(ctx);
402 ctx->progress = 0;
403}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000404#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000405
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000406static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
407{
408 int flush = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000409 int c;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000410#ifdef MTRACE
411 extern void *mallwatch;
412#endif
413 char *oldpath = getenv("PATH");
414 e2fsck_t ctx;
415 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000416#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000417 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000418#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000419
420 retval = e2fsck_allocate_context(&ctx);
421 if (retval)
422 return retval;
423
424 *ret_ctx = ctx;
425
426 /* Update our PATH to include /sbin */
427 if (oldpath) {
428 char *newpath;
429
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000430 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
431 strlen (oldpath));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000432 if (!newpath)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000433 fatal_error(ctx, "Couldn't malloc() newpath");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000434 strcpy (newpath, PATH_SET);
435 strcat (newpath, ":");
436 strcat (newpath, oldpath);
437 putenv (newpath);
438 } else
439 putenv (PATH_SET);
440
441 setbuf(stdout, NULL);
442 setbuf(stderr, NULL);
443 initialize_ext2_error_table();
444
445 if (argc && *argv)
446 ctx->program_name = *argv;
447 else
448 ctx->program_name = "e2fsck";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000449 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000450 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000451 case 'C':
452 ctx->progress = e2fsck_update_progress;
453 ctx->progress_fd = atoi(optarg);
454 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000455 case 'p':
456 case 'a':
457 ctx->options |= E2F_OPT_PREEN;
458 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
459 break;
460 case 'n':
461 ctx->options |= E2F_OPT_NO;
462 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
463 break;
464 case 'y':
465 ctx->options |= E2F_OPT_YES;
466 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
467 break;
468 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000469#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000470 if (ctx->options & E2F_OPT_TIME)
471 ctx->options |= E2F_OPT_TIME2;
472 else
473 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000474#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000475 fprintf(stderr, _("The -t option is not "
476 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000477#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000478 break;
479 case 'c':
480 cflag++;
481 ctx->options |= E2F_OPT_CHECKBLOCKS;
482 break;
483 case 'r':
484 /* What we do by default, anyway! */
485 break;
486 case 'b':
487 ctx->use_superblock = atoi(optarg);
488 break;
489 case 'B':
490 blocksize = atoi(optarg);
491 break;
492 case 'I':
493 ctx->inode_buffer_blocks = atoi(optarg);
494 break;
495 case 'P':
496 ctx->process_inode_size = atoi(optarg);
497 break;
498 case 'L':
499 replace_bad_blocks++;
500 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000501 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000503 fatal_error(ctx,
504 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000505 strcpy(bad_blocks_file, optarg);
506 break;
507 case 'd':
508 ctx->options |= E2F_OPT_DEBUG;
509 break;
510 case 'f':
511 force = 1;
512 break;
513 case 'F':
514#ifdef BLKFLSBUF
515 flush = 1;
516#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000517 fatal_error(ctx, _("-F not supported"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000518#endif
519 break;
520 case 'v':
521 verbose = 1;
522 break;
523 case 'V':
524 show_version_only = 1;
525 break;
526#ifdef MTRACE
527 case 'M':
528 mallwatch = (void *) strtol(optarg, NULL, 0);
529 break;
530#endif
531 case 'N':
532 ctx->device_name = optarg;
533 break;
534 case 's':
535 normalize_swapfs = 1;
536 case 'S':
537 swapfs = 1;
538 break;
539 default:
540 usage(ctx);
541 }
542 if (show_version_only)
543 return 0;
544 if (optind != argc - 1)
545 usage(ctx);
546 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
547 !cflag && !swapfs)
548 ctx->options |= E2F_OPT_READONLY;
549 ctx->filesystem_name = argv[optind];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000550#ifdef BLKFLSBUF
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000551 if (flush) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000552 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
553
554 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000555 com_err("open", errno,
556 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000557 ctx->filesystem_name);
558 exit(FSCK_ERROR);
559 }
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000560 if (fsync(fd) < 0) {
561 com_err("fsync", errno,
562 _("while trying to flush %s"),
563 ctx->filesystem_name);
564 exit(FSCK_ERROR);
565 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000566 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000567 com_err("BLKFLSBUF", errno,
568 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000569 ctx->filesystem_name);
570 exit(FSCK_ERROR);
571 }
572 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000573 }
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000574#endif /* BLKFLSBUF */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 if (swapfs) {
576 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000577 fprintf(stderr, _("Incompatible options not "
578 "allowed when byte-swapping.\n"));
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000579 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000580 }
581 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000582#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000583 /*
584 * Set up signal action
585 */
586 memset(&sa, 0, sizeof(struct sigaction));
587#ifdef SA_RESTART
588 sa.sa_flags = SA_RESTART;
589#endif
590 global_signal_ctx = ctx;
591 sa.sa_handler = signal_progress_on;
592 sigaction(SIGUSR1, &sa, 0);
593 sa.sa_handler = signal_progress_off;
594 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000595#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000596 return 0;
597}
598
599static const char *my_ver_string = E2FSPROGS_VERSION;
600static const char *my_ver_date = E2FSPROGS_DATE;
601
602int main (int argc, char *argv[])
603{
604 errcode_t retval = 0;
605 int exit_value = FSCK_OK;
606 int i;
607 ext2_filsys fs = 0;
608 io_manager io_ptr;
609 struct ext2fs_sb *s;
610 const char *lib_ver_date;
611 int my_ver, lib_ver;
612 e2fsck_t ctx;
613 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000614 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000615
616 clear_problem_context(&pctx);
617#ifdef MTRACE
618 mtrace();
619#endif
620#ifdef MCHECK
621 mcheck(0);
622#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000623#ifdef ENABLE_NLS
624 setlocale(LC_MESSAGES, "");
625 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
626 textdomain(NLS_CAT_NAME);
627#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000628 my_ver = ext2fs_parse_version_string(my_ver_string);
629 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
630 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000631 fprintf( stderr, _("Error: ext2fs library version "
632 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000633 show_version_only++;
634 }
635
636 retval = PRS(argc, argv, &ctx);
637 if (retval) {
638 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000639 _("while trying to initialize program"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000640 exit(1);
641 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000642 reserve_stdio_fds();
643
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000644#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000645 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000646#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000647
648 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000649 fprintf (stderr, _("e2fsck %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000650 my_ver_string, my_ver_date, EXT2FS_VERSION,
651 EXT2FS_DATE);
652
653 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000654 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000655 error_message(EXT2_ET_BASE), lib_ver_date);
656 exit(0);
657 }
658
659 check_mount(ctx);
660
661 if (!(ctx->options & E2F_OPT_PREEN) &&
662 !(ctx->options & E2F_OPT_NO) &&
663 !(ctx->options & E2F_OPT_YES)) {
664 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000665 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000666 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000667 }
668 ctx->superblock = ctx->use_superblock;
669restart:
670#if 1
671 io_ptr = unix_io_manager;
672#else
673 io_ptr = test_io_manager;
674 test_io_backing_manager = unix_io_manager;
675#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000676 flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW;
677 if (ctx->superblock && blocksize) {
678 retval = ext2fs_open(ctx->filesystem_name, flags,
679 ctx->superblock, blocksize, io_ptr, &fs);
680 } else if (ctx->superblock) {
681 for (i=0; possible_block_sizes[i]; i++) {
682 retval = ext2fs_open(ctx->filesystem_name, flags,
683 ctx->superblock,
684 possible_block_sizes[i],
685 io_ptr, &fs);
686 if (!retval)
687 break;
688 }
689 } else
690 retval = ext2fs_open(ctx->filesystem_name, flags,
691 0, 0, io_ptr, &fs);
692 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
693 ((retval == EXT2_ET_BAD_MAGIC) ||
694 ((retval == 0) && ext2fs_check_desc(fs)))) {
695 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000696 printf(_("%s trying backup blocks...\n"),
697 retval ? _("Couldn't find ext2 superblock,") :
698 _("Group descriptors look bad..."));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000699 ctx->superblock = get_backup_sb(fs);
700 if (fs)
701 ext2fs_close(fs);
702 goto restart;
703 }
704 }
705 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000706 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000707 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000708 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000709 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000710 "too high for this version of e2fsck.\n"
711 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000712 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000713 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
714 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000715 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000716 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000717 printf(_("You must have %s access to the "
718 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000719 (ctx->options & E2F_OPT_READONLY) ?
720 "r/o" : "r/w");
721 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000722 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000723#ifdef EROFS
724 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000725 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000726 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000727 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000728#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000729 else
730 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000731 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000732 }
733 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000734 fs->priv_data = ctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735#ifdef EXT2_CURRENT_REV
736 if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) {
737 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000738 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000739 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000740 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000741 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000742 }
743#endif
744 /*
745 * Check for compatibility with the feature sets. We need to
746 * be more stringent than ext2fs_open().
747 */
748 s = (struct ext2fs_sb *) fs->super;
749 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
Theodore Ts'o8144d671998-07-09 05:33:18 +0000750 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000751 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
752 "(%s)", ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000753 goto get_newer;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000754 }
755 if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
756 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
757 "(%s)", ctx->filesystem_name);
758 goto get_newer;
759 }
Theodore Ts'o5596def1999-07-19 15:27:37 +0000760 if (ctx->device_name == 0 &&
761 (s->s_volume_name[0] != 0)) {
762 char *cp = malloc(sizeof(s->s_volume_name)+1);
763 if (cp) {
764 strncpy(cp, s->s_volume_name,
765 sizeof(s->s_volume_name));
766 cp[sizeof(s->s_volume_name)] = 0;
767 ctx->device_name = cp;
768 }
769 }
770 if (ctx->device_name == 0)
771 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000772
773 /*
774 * If the user specified a specific superblock, presumably the
775 * master superblock has been trashed. So we mark the
776 * superblock as dirty, so it can be written out.
777 */
778 if (ctx->superblock &&
779 !(ctx->options & E2F_OPT_READONLY))
780 ext2fs_mark_super_dirty(fs);
781
782 /*
783 * Don't overwrite the backup superblock and block
784 * descriptors, until we're sure the filesystem is OK....
785 */
786 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
787
788 ehandler_init(fs->io);
789
790 if (ctx->superblock)
791 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
792 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000793 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000794 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000795 check_if_skip(ctx);
796 if (bad_blocks_file)
797 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
798 else if (cflag)
799 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000800 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000801 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000802
803 if (normalize_swapfs) {
804 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
805 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000806 fprintf(stderr, _("%s: Filesystem byte order "
807 "already normalized.\n"), ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000808 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000809 }
810 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000811 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000812 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000813 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000814 exit(FSCK_ERROR);
815 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000816
817 /*
818 * Mark the system as valid, 'til proven otherwise
819 */
820 ext2fs_mark_valid(fs);
821
822 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
823 if (retval) {
824 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000825 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000826 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000827 printf(_("This doesn't bode well,"
828 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000829 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000830
831 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000832 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000833 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000834 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000835 retval = e2fsck_reset_context(ctx);
836 if (retval) {
837 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000838 _("while resetting context"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839 exit(1);
840 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000841 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000842 goto restart;
843 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000844 if (run_result & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000845 exit(FSCK_ERROR);
846 if (run_result & E2F_FLAG_CANCEL)
847 ext2fs_unmark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000848
849#ifdef MTRACE
850 mtrace_print("Cleanup");
851#endif
852 if (ext2fs_test_changed(fs)) {
853 exit_value = FSCK_NONDESTRUCT;
854 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000855 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000856 ctx->device_name);
857 if (root_filesystem && !read_only_root) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000858 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000859 ctx->device_name);
860 exit_value = FSCK_REBOOT;
861 }
862 }
863 if (ext2fs_test_valid(fs))
864 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
865 else
866 exit_value = FSCK_UNCORRECTED;
867 if (!(ctx->options & E2F_OPT_READONLY)) {
868 if (ext2fs_test_valid(fs)) {
869 if (!(fs->super->s_state & EXT2_VALID_FS))
870 exit_value = FSCK_NONDESTRUCT;
871 fs->super->s_state = EXT2_VALID_FS;
872 } else
873 fs->super->s_state &= ~EXT2_VALID_FS;
874 fs->super->s_mnt_count = 0;
875 fs->super->s_lastcheck = time(NULL);
876 ext2fs_mark_super_dirty(fs);
877 }
878 show_stats(ctx);
879
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000880 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000881
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000882#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000883 if (ctx->options & E2F_OPT_TIME)
884 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000885#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000886
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000887 ext2fs_close(fs);
Theodore Ts'o7142db01999-11-08 18:46:54 +0000888 ctx->fs = NULL;
889 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000890
891 return exit_value;
892}