blob: 150bd5176f22fb2d256fa26d80c0119f9e7656d7 [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,
61 "Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
62 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o5596def1999-07-19 15:27:37 +000063 "\t\t[-l|-L bad_blocks_file] [-C fd] device\n",
64 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000065
66 fprintf(stderr, "\nEmergency help:\n"
67 " -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"
77 );
78
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) {
104 printf("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n",
105 ctx->device_name, inodes_used, inodes,
106 frag_percent / 10, frag_percent % 10,
107 blocks_used, blocks);
108 return;
109 }
110 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
111 (inodes_used != 1) ? "s" : "",
112 100 * inodes_used / inodes);
113 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
114 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
115 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
116 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
117 printf ("%8d block%s used (%d%%)\n"
118 "%8d bad block%s\n", blocks_used,
119 (blocks_used != 1) ? "s" : "",
120 100 * blocks_used / blocks, ctx->fs_badblocks_count,
121 ctx->fs_badblocks_count != 1 ? "s" : "");
122 printf ("\n%8d regular file%s\n"
123 "%8d director%s\n"
124 "%8d character device file%s\n"
125 "%8d block device file%s\n"
126 "%8d fifo%s\n"
127 "%8d link%s\n"
128 "%8d symbolic link%s (%d fast symbolic link%s)\n"
129 "%8d socket%s\n"
130 "--------\n"
131 "%8d file%s\n",
132 ctx->fs_regular_count,
133 (ctx->fs_regular_count != 1) ? "s" : "",
134 ctx->fs_directory_count,
135 (ctx->fs_directory_count != 1) ? "ies" : "y",
136 ctx->fs_chardev_count,
137 (ctx->fs_chardev_count != 1) ? "s" : "",
138 ctx->fs_blockdev_count,
139 (ctx->fs_blockdev_count != 1) ? "s" : "",
140 ctx->fs_fifo_count,
141 (ctx->fs_fifo_count != 1) ? "s" : "",
142 ctx->fs_links_count - dir_links,
143 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
144 ctx->fs_symlinks_count,
145 (ctx->fs_symlinks_count != 1) ? "s" : "",
146 ctx->fs_fast_symlinks_count,
147 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
148 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
149 ctx->fs_total_count - dir_links,
150 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
151}
152
153static void check_mount(e2fsck_t ctx)
154{
155 errcode_t retval;
156 int mount_flags, cont, fd;
157
158 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
159 if (retval) {
160 com_err("ext2fs_check_if_mount", retval,
161 "while determining whether %s is mounted.",
162 ctx->filesystem_name);
163 return;
164 }
165 if (!(mount_flags & EXT2_MF_MOUNTED))
166 return;
167
168#if (defined(__linux__) && defined(HAVE_MNTENT_H))
169 /*
170 * If the root is mounted read-only, then /etc/mtab is
171 * probably not correct; so we won't issue a warning based on
172 * it.
173 */
174 fd = open(MOUNTED, O_RDWR);
175 if (fd < 0) {
176 if (errno == EROFS)
177 return;
178 } else
179 close(fd);
180#endif
181
182 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000183 printf("Warning! %s is mounted.\n", ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000184 return;
185 }
186
Theodore Ts'o5596def1999-07-19 15:27:37 +0000187 printf("%s is mounted. ", ctx->filesystem_name);
Theodore Ts'o74033351999-07-01 03:00:47 +0000188 if (!isatty(0) || !isatty(1)) {
189 printf("Cannot continue, aborting.\n\n");
190 exit(FSCK_ERROR);
191 }
192 printf("\n\n\007\007\007\007WARNING!!! "
193 "Running e2fsck on a mounted filesystem may cause\n"
194 "SEVERE filesystem damage.\007\007\007\n\n");
195 cont = ask_yn("Do you really want to continue", -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000196 if (!cont) {
197 printf ("check aborted.\n");
198 exit (0);
199 }
200 return;
201}
202
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000203/*
204 * This routine checks to see if a filesystem can be skipped; if so,
205 * it will exit with E2FSCK_OK. Under some conditions it will print a
206 * message explaining why a check is being forced.
207 */
208static void check_if_skip(e2fsck_t ctx)
209{
210 ext2_filsys fs = ctx->fs;
211 const char *reason = NULL;
212
213 if (force || bad_blocks_file || cflag || swapfs)
214 return;
215
216 if (fs->super->s_state & EXT2_ERROR_FS)
217 reason = "contains a file system with errors";
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000218 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
219 reason = "was not cleanly unmounted";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000220 else if (fs->super->s_mnt_count >=
221 (unsigned) fs->super->s_max_mnt_count)
222 reason = "has reached maximal mount count";
223 else if (fs->super->s_checkinterval &&
224 time(0) >= (fs->super->s_lastcheck +
225 fs->super->s_checkinterval))
226 reason = "has gone too long without being checked";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000227 if (reason) {
228 printf("%s %s, check forced.\n", ctx->device_name, reason);
229 return;
230 }
231 printf("%s: clean, %d/%d files, %d/%d blocks\n", ctx->device_name,
232 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
233 fs->super->s_inodes_count,
234 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
235 fs->super->s_blocks_count);
236 ext2fs_close(fs);
237 exit(FSCK_OK);
238}
239
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000240/*
241 * For completion notice
242 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000243struct percent_tbl {
244 int max_pass;
245 int table[32];
246};
247struct percent_tbl e2fsck_tbl = {
248 5, { 0, 70, 90, 92, 95, 100 }
249};
250static int dpywidth = 50;
251static char bar[] =
252 "==============================================================="
253 "===============================================================";
254static char spaces[] =
255 " "
256 " ";
257
258static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
259 int max)
260{
261 float percent;
262
263 if (pass <= 0)
264 return 0.0;
265 if (pass > tbl->max_pass)
266 return 100.0;
267 percent = ((float) curr) / ((float) max);
268 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
269 + tbl->table[pass-1]);
270}
271
272extern void e2fsck_clear_progbar(e2fsck_t ctx)
273{
274 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
275 return;
276
277 printf("%s\r", spaces + (sizeof(spaces) - 80));
278 ctx->flags &= ~E2F_FLAG_PROG_BAR;
279}
280
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000281static int e2fsck_update_progress(e2fsck_t ctx, int pass,
282 unsigned long cur, unsigned long max)
283{
284 const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000285 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000286 int i;
287 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000288
289 if (pass == 0)
290 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000291
292 if (ctx->progress_fd) {
293 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
294 write(ctx->progress_fd, buf, strlen(buf));
295 } else {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000296 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
297 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000298 ctx->progress_pos = (ctx->progress_pos+1) & 3;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000299 ctx->flags |= E2F_FLAG_PROG_BAR;
300 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
301 if (ctx->progress_last_percent == (int) 1000 * percent)
302 return 0;
303 ctx->progress_last_percent = (int) 1000 * percent;
304 i = ((percent * dpywidth) + 50) / 100;
305 printf("%s: |%s%s", ctx->device_name,
306 bar + (sizeof(bar) - (i+1)),
307 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
308 if (percent == 100.0)
309 fputc('|', stdout);
310 else
311 fputc(spinner[ctx->progress_pos & 3], stdout);
312 printf(" %4.1f%% \r", percent);
313 if (percent == 100.0)
314 e2fsck_clear_progbar(ctx);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000315 fflush(stdout);
316 }
317 return 0;
318}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000319
320#define PATH_SET "PATH=/sbin"
321
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000322static void reserve_stdio_fds(NOARGS)
323{
324 int fd;
325
326 while (1) {
327 fd = open("/dev/null", O_RDWR);
328 if (fd > 2)
329 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000330 if (fd < 0) {
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000331 fprintf(stderr, "ERROR: Couldn't open "
332 "/dev/null (%s)\n",
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000333 strerror(errno));
334 break;
335 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000336 }
337 close(fd);
338}
339
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000340#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000341static e2fsck_t global_signal_ctx;
342
343static void signal_progress_on(int sig)
344{
345 e2fsck_t ctx = global_signal_ctx;
346
347 if (!ctx)
348 return;
349
350 ctx->progress = e2fsck_update_progress;
351 ctx->progress_fd = 0;
352}
353
354static void signal_progress_off(int sig)
355{
356 e2fsck_t ctx = global_signal_ctx;
357
358 if (!ctx)
359 return;
360
361 e2fsck_clear_progbar(ctx);
362 ctx->progress = 0;
363}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000364#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000365
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000366static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
367{
368 int flush = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000369 int c;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000370#ifdef MTRACE
371 extern void *mallwatch;
372#endif
373 char *oldpath = getenv("PATH");
374 e2fsck_t ctx;
375 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000376#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000377 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000378#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000379
380 retval = e2fsck_allocate_context(&ctx);
381 if (retval)
382 return retval;
383
384 *ret_ctx = ctx;
385
386 /* Update our PATH to include /sbin */
387 if (oldpath) {
388 char *newpath;
389
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000390 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
391 strlen (oldpath));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000392 if (!newpath)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000393 fatal_error(ctx, "Couldn't malloc() newpath");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000394 strcpy (newpath, PATH_SET);
395 strcat (newpath, ":");
396 strcat (newpath, oldpath);
397 putenv (newpath);
398 } else
399 putenv (PATH_SET);
400
401 setbuf(stdout, NULL);
402 setbuf(stderr, NULL);
403 initialize_ext2_error_table();
404
405 if (argc && *argv)
406 ctx->program_name = *argv;
407 else
408 ctx->program_name = "e2fsck";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000409 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000410 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000411 case 'C':
412 ctx->progress = e2fsck_update_progress;
413 ctx->progress_fd = atoi(optarg);
414 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000415 case 'p':
416 case 'a':
417 ctx->options |= E2F_OPT_PREEN;
418 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
419 break;
420 case 'n':
421 ctx->options |= E2F_OPT_NO;
422 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
423 break;
424 case 'y':
425 ctx->options |= E2F_OPT_YES;
426 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
427 break;
428 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000429#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000430 if (ctx->options & E2F_OPT_TIME)
431 ctx->options |= E2F_OPT_TIME2;
432 else
433 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000434#else
435 fprintf(stderr, "The -t option is not "
436 "supported on this version of e2fsck.\n");
437#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000438 break;
439 case 'c':
440 cflag++;
441 ctx->options |= E2F_OPT_CHECKBLOCKS;
442 break;
443 case 'r':
444 /* What we do by default, anyway! */
445 break;
446 case 'b':
447 ctx->use_superblock = atoi(optarg);
448 break;
449 case 'B':
450 blocksize = atoi(optarg);
451 break;
452 case 'I':
453 ctx->inode_buffer_blocks = atoi(optarg);
454 break;
455 case 'P':
456 ctx->process_inode_size = atoi(optarg);
457 break;
458 case 'L':
459 replace_bad_blocks++;
460 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000461 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000462 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000463 fatal_error(ctx,
464 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000465 strcpy(bad_blocks_file, optarg);
466 break;
467 case 'd':
468 ctx->options |= E2F_OPT_DEBUG;
469 break;
470 case 'f':
471 force = 1;
472 break;
473 case 'F':
474#ifdef BLKFLSBUF
475 flush = 1;
476#else
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000477 fatal_error(ctx, "-F not supported");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000478#endif
479 break;
480 case 'v':
481 verbose = 1;
482 break;
483 case 'V':
484 show_version_only = 1;
485 break;
486#ifdef MTRACE
487 case 'M':
488 mallwatch = (void *) strtol(optarg, NULL, 0);
489 break;
490#endif
491 case 'N':
492 ctx->device_name = optarg;
493 break;
494 case 's':
495 normalize_swapfs = 1;
496 case 'S':
497 swapfs = 1;
498 break;
499 default:
500 usage(ctx);
501 }
502 if (show_version_only)
503 return 0;
504 if (optind != argc - 1)
505 usage(ctx);
506 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
507 !cflag && !swapfs)
508 ctx->options |= E2F_OPT_READONLY;
509 ctx->filesystem_name = argv[optind];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510 if (flush) {
511#ifdef BLKFLSBUF
512 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
513
514 if (fd < 0) {
515 com_err("open", errno, "while opening %s for flushing",
516 ctx->filesystem_name);
517 exit(FSCK_ERROR);
518 }
519 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
520 com_err("BLKFLSBUF", errno, "while trying to flush %s",
521 ctx->filesystem_name);
522 exit(FSCK_ERROR);
523 }
524 close(fd);
525#else
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000526 fatal_error(ctx, "BLKFLSBUF not supported");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000527#endif /* BLKFLSBUF */
528 }
529 if (swapfs) {
530 if (cflag || bad_blocks_file) {
531 fprintf(stderr, "Incompatible options not "
532 "allowed when byte-swapping.\n");
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000533 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000534 }
535 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000536#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000537 /*
538 * Set up signal action
539 */
540 memset(&sa, 0, sizeof(struct sigaction));
541#ifdef SA_RESTART
542 sa.sa_flags = SA_RESTART;
543#endif
544 global_signal_ctx = ctx;
545 sa.sa_handler = signal_progress_on;
546 sigaction(SIGUSR1, &sa, 0);
547 sa.sa_handler = signal_progress_off;
548 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000549#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000550 return 0;
551}
552
553static const char *my_ver_string = E2FSPROGS_VERSION;
554static const char *my_ver_date = E2FSPROGS_DATE;
555
556int main (int argc, char *argv[])
557{
558 errcode_t retval = 0;
559 int exit_value = FSCK_OK;
560 int i;
561 ext2_filsys fs = 0;
562 io_manager io_ptr;
563 struct ext2fs_sb *s;
564 const char *lib_ver_date;
565 int my_ver, lib_ver;
566 e2fsck_t ctx;
567 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000568 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000569
570 clear_problem_context(&pctx);
571#ifdef MTRACE
572 mtrace();
573#endif
574#ifdef MCHECK
575 mcheck(0);
576#endif
577 my_ver = ext2fs_parse_version_string(my_ver_string);
578 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
579 if (my_ver > lib_ver) {
580 fprintf( stderr, "Error: ext2fs library version "
581 "out of date!\n");
582 show_version_only++;
583 }
584
585 retval = PRS(argc, argv, &ctx);
586 if (retval) {
587 com_err("e2fsck", retval,
588 "while trying to initialize program");
589 exit(1);
590 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000591 reserve_stdio_fds();
592
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000593#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000594 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000595#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000596
597 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
598 fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n",
599 my_ver_string, my_ver_date, EXT2FS_VERSION,
600 EXT2FS_DATE);
601
602 if (show_version_only) {
603 fprintf(stderr, "\tUsing %s, %s\n",
604 error_message(EXT2_ET_BASE), lib_ver_date);
605 exit(0);
606 }
607
608 check_mount(ctx);
609
610 if (!(ctx->options & E2F_OPT_PREEN) &&
611 !(ctx->options & E2F_OPT_NO) &&
612 !(ctx->options & E2F_OPT_YES)) {
613 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000614 fatal_error(ctx,
615 "need terminal for interactive repairs");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000616 }
617 ctx->superblock = ctx->use_superblock;
618restart:
619#if 1
620 io_ptr = unix_io_manager;
621#else
622 io_ptr = test_io_manager;
623 test_io_backing_manager = unix_io_manager;
624#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW;
626 if (ctx->superblock && blocksize) {
627 retval = ext2fs_open(ctx->filesystem_name, flags,
628 ctx->superblock, blocksize, io_ptr, &fs);
629 } else if (ctx->superblock) {
630 for (i=0; possible_block_sizes[i]; i++) {
631 retval = ext2fs_open(ctx->filesystem_name, flags,
632 ctx->superblock,
633 possible_block_sizes[i],
634 io_ptr, &fs);
635 if (!retval)
636 break;
637 }
638 } else
639 retval = ext2fs_open(ctx->filesystem_name, flags,
640 0, 0, io_ptr, &fs);
641 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
642 ((retval == EXT2_ET_BAD_MAGIC) ||
643 ((retval == 0) && ext2fs_check_desc(fs)))) {
644 if (!fs || (fs->group_desc_count > 1)) {
645 printf("%s trying backup blocks...\n",
646 retval ? "Couldn't find ext2 superblock," :
647 "Group descriptors look bad...");
648 ctx->superblock = get_backup_sb(fs);
649 if (fs)
650 ext2fs_close(fs);
651 goto restart;
652 }
653 }
654 if (retval) {
655 com_err(ctx->program_name, retval, "while trying to open %s",
656 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000657 if (retval == EXT2_ET_REV_TOO_HIGH) {
658 printf("The filesystem revision is apparently "
659 "too high for this version of e2fsck.\n"
660 "(Or the filesystem superblock "
661 "is corrupt)\n\n");
662 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
663 } else if (retval == EXT2_ET_SHORT_READ)
664 printf("Could this be a zero-length partition?\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665 else if ((retval == EPERM) || (retval == EACCES))
666 printf("You must have %s access to the "
667 "filesystem or be root\n",
668 (ctx->options & E2F_OPT_READONLY) ?
669 "r/o" : "r/w");
670 else if (retval == ENXIO)
671 printf("Possibly non-existent or swap device?\n");
Theodore Ts'o68227541997-11-04 04:25:22 +0000672#ifdef EROFS
673 else if (retval == EROFS)
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000674 printf("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000675 "to do a read-only\n"
676 "check of the device.\n");
Theodore Ts'o68227541997-11-04 04:25:22 +0000677#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000678 else
679 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000680 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000681 }
682 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000683 fs->priv_data = ctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684#ifdef EXT2_CURRENT_REV
685 if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) {
686 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
687 "while trying to open %s",
688 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000689 get_newer:
690 fatal_error(ctx, "Get a newer version of e2fsck!");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000691 }
692#endif
693 /*
694 * Check for compatibility with the feature sets. We need to
695 * be more stringent than ext2fs_open().
696 */
697 s = (struct ext2fs_sb *) fs->super;
698 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
Theodore Ts'o8144d671998-07-09 05:33:18 +0000699 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
701 "(%s)", ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000702 goto get_newer;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000703 }
704 if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
705 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
706 "(%s)", ctx->filesystem_name);
707 goto get_newer;
708 }
Theodore Ts'o5596def1999-07-19 15:27:37 +0000709 if (ctx->device_name == 0 &&
710 (s->s_volume_name[0] != 0)) {
711 char *cp = malloc(sizeof(s->s_volume_name)+1);
712 if (cp) {
713 strncpy(cp, s->s_volume_name,
714 sizeof(s->s_volume_name));
715 cp[sizeof(s->s_volume_name)] = 0;
716 ctx->device_name = cp;
717 }
718 }
719 if (ctx->device_name == 0)
720 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000721
722 /*
723 * If the user specified a specific superblock, presumably the
724 * master superblock has been trashed. So we mark the
725 * superblock as dirty, so it can be written out.
726 */
727 if (ctx->superblock &&
728 !(ctx->options & E2F_OPT_READONLY))
729 ext2fs_mark_super_dirty(fs);
730
731 /*
732 * Don't overwrite the backup superblock and block
733 * descriptors, until we're sure the filesystem is OK....
734 */
735 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
736
737 ehandler_init(fs->io);
738
739 if (ctx->superblock)
740 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
741 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000742 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000743 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000744 check_if_skip(ctx);
745 if (bad_blocks_file)
746 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
747 else if (cflag)
748 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000749 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000750 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000751
752 if (normalize_swapfs) {
753 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
754 ext2fs_native_flag()) {
755 fprintf(stderr, "%s: Filesystem byte order "
756 "already normalized.\n", ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000757 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000758 }
759 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000760 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000761 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000762 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000763 exit(FSCK_ERROR);
764 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000765
766 /*
767 * Mark the system as valid, 'til proven otherwise
768 */
769 ext2fs_mark_valid(fs);
770
771 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
772 if (retval) {
773 com_err(ctx->program_name, retval,
774 "while reading bad blocks inode");
775 preenhalt(ctx);
776 printf("This doesn't bode well, but we'll try to go on...\n");
777 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000778
779 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000780 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000781 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000782 printf("Restarting e2fsck from the beginning...\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000783 retval = e2fsck_reset_context(ctx);
784 if (retval) {
785 com_err(ctx->program_name, retval,
786 "while resetting context");
787 exit(1);
788 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000789 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000790 goto restart;
791 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000792 if (run_result & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000793 exit(FSCK_ERROR);
794 if (run_result & E2F_FLAG_CANCEL)
795 ext2fs_unmark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000796
797#ifdef MTRACE
798 mtrace_print("Cleanup");
799#endif
800 if (ext2fs_test_changed(fs)) {
801 exit_value = FSCK_NONDESTRUCT;
802 if (!(ctx->options & E2F_OPT_PREEN))
803 printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n",
804 ctx->device_name);
805 if (root_filesystem && !read_only_root) {
806 printf("%s: ***** REBOOT LINUX *****\n",
807 ctx->device_name);
808 exit_value = FSCK_REBOOT;
809 }
810 }
811 if (ext2fs_test_valid(fs))
812 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
813 else
814 exit_value = FSCK_UNCORRECTED;
815 if (!(ctx->options & E2F_OPT_READONLY)) {
816 if (ext2fs_test_valid(fs)) {
817 if (!(fs->super->s_state & EXT2_VALID_FS))
818 exit_value = FSCK_NONDESTRUCT;
819 fs->super->s_state = EXT2_VALID_FS;
820 } else
821 fs->super->s_state &= ~EXT2_VALID_FS;
822 fs->super->s_mnt_count = 0;
823 fs->super->s_lastcheck = time(NULL);
824 ext2fs_mark_super_dirty(fs);
825 }
826 show_stats(ctx);
827
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000828 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000829
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000830#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000831 if (ctx->options & E2F_OPT_TIME)
832 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000833#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000834
835 e2fsck_free_context(ctx);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000836 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000837
838 return exit_value;
839}