blob: e25bbf39a9419d9b4c78c51e967436af9a1ad20c [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>
19#include <termios.h>
20#include <time.h>
21#ifdef HAVE_GETOPT_H
22#include <getopt.h>
23#endif
24#include <unistd.h>
25#ifdef HAVE_ERRNO_H
26#include <errno.h>
27#endif
28#ifdef HAVE_MNTENT_H
29#include <mntent.h>
30#endif
31#include <sys/ioctl.h>
32#include <malloc.h>
33
34#include "et/com_err.h"
35#include "e2fsck.h"
36#include "problem.h"
37#include "../version.h"
38
39extern int isatty(int);
40
41/* 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"
63 "\t\t[-l|-L bad_blocks_file] device\n", ctx->program_name);
64 exit(FSCK_USAGE);
65}
66
67static void show_stats(e2fsck_t ctx)
68{
69 ext2_filsys fs = ctx->fs;
70 int inodes, inodes_used, blocks, blocks_used;
71 int dir_links;
72 int num_files, num_links;
73 int frag_percent;
74
75 dir_links = 2 * ctx->fs_directory_count - 1;
76 num_files = ctx->fs_total_count - dir_links;
77 num_links = ctx->fs_links_count - dir_links;
78 inodes = fs->super->s_inodes_count;
79 inodes_used = (fs->super->s_inodes_count -
80 fs->super->s_free_inodes_count);
81 blocks = fs->super->s_blocks_count;
82 blocks_used = (fs->super->s_blocks_count -
83 fs->super->s_free_blocks_count);
84
85 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
86 frag_percent = (frag_percent + 5) / 10;
87
88 if (!verbose) {
89 printf("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n",
90 ctx->device_name, inodes_used, inodes,
91 frag_percent / 10, frag_percent % 10,
92 blocks_used, blocks);
93 return;
94 }
95 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
96 (inodes_used != 1) ? "s" : "",
97 100 * inodes_used / inodes);
98 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
99 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
100 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
101 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
102 printf ("%8d block%s used (%d%%)\n"
103 "%8d bad block%s\n", blocks_used,
104 (blocks_used != 1) ? "s" : "",
105 100 * blocks_used / blocks, ctx->fs_badblocks_count,
106 ctx->fs_badblocks_count != 1 ? "s" : "");
107 printf ("\n%8d regular file%s\n"
108 "%8d director%s\n"
109 "%8d character device file%s\n"
110 "%8d block device file%s\n"
111 "%8d fifo%s\n"
112 "%8d link%s\n"
113 "%8d symbolic link%s (%d fast symbolic link%s)\n"
114 "%8d socket%s\n"
115 "--------\n"
116 "%8d file%s\n",
117 ctx->fs_regular_count,
118 (ctx->fs_regular_count != 1) ? "s" : "",
119 ctx->fs_directory_count,
120 (ctx->fs_directory_count != 1) ? "ies" : "y",
121 ctx->fs_chardev_count,
122 (ctx->fs_chardev_count != 1) ? "s" : "",
123 ctx->fs_blockdev_count,
124 (ctx->fs_blockdev_count != 1) ? "s" : "",
125 ctx->fs_fifo_count,
126 (ctx->fs_fifo_count != 1) ? "s" : "",
127 ctx->fs_links_count - dir_links,
128 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
129 ctx->fs_symlinks_count,
130 (ctx->fs_symlinks_count != 1) ? "s" : "",
131 ctx->fs_fast_symlinks_count,
132 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
133 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
134 ctx->fs_total_count - dir_links,
135 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
136}
137
138static void check_mount(e2fsck_t ctx)
139{
140 errcode_t retval;
141 int mount_flags, cont, fd;
142
143 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
144 if (retval) {
145 com_err("ext2fs_check_if_mount", retval,
146 "while determining whether %s is mounted.",
147 ctx->filesystem_name);
148 return;
149 }
150 if (!(mount_flags & EXT2_MF_MOUNTED))
151 return;
152
153#if (defined(__linux__) && defined(HAVE_MNTENT_H))
154 /*
155 * If the root is mounted read-only, then /etc/mtab is
156 * probably not correct; so we won't issue a warning based on
157 * it.
158 */
159 fd = open(MOUNTED, O_RDWR);
160 if (fd < 0) {
161 if (errno == EROFS)
162 return;
163 } else
164 close(fd);
165#endif
166
167 if (ctx->options & E2F_OPT_READONLY) {
168 printf("Warning! %s is mounted.\n", ctx->device_name);
169 return;
170 }
171
172 printf("%s is mounted.\n\n", ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000173 printf("\007\007\007\007WARNING!!! Running e2fsck on a mounted filesystem "
174 "may cause\nSEVERE filesystem damage.\007\007\007\n\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000175 if (isatty (0) && isatty (1))
176 cont = ask_yn("Do you really want to continue", -1);
177 else
178 cont = 0;
179 if (!cont) {
180 printf ("check aborted.\n");
181 exit (0);
182 }
183 return;
184}
185
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000186/*
187 * This routine checks to see if a filesystem can be skipped; if so,
188 * it will exit with E2FSCK_OK. Under some conditions it will print a
189 * message explaining why a check is being forced.
190 */
191static void check_if_skip(e2fsck_t ctx)
192{
193 ext2_filsys fs = ctx->fs;
194 const char *reason = NULL;
195
196 if (force || bad_blocks_file || cflag || swapfs)
197 return;
198
199 if (fs->super->s_state & EXT2_ERROR_FS)
200 reason = "contains a file system with errors";
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000201 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
202 reason = "was not cleanly unmounted";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000203 else if (fs->super->s_mnt_count >=
204 (unsigned) fs->super->s_max_mnt_count)
205 reason = "has reached maximal mount count";
206 else if (fs->super->s_checkinterval &&
207 time(0) >= (fs->super->s_lastcheck +
208 fs->super->s_checkinterval))
209 reason = "has gone too long without being checked";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000210 if (reason) {
211 printf("%s %s, check forced.\n", ctx->device_name, reason);
212 return;
213 }
214 printf("%s: clean, %d/%d files, %d/%d blocks\n", ctx->device_name,
215 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
216 fs->super->s_inodes_count,
217 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
218 fs->super->s_blocks_count);
219 ext2fs_close(fs);
220 exit(FSCK_OK);
221}
222
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000223/*
224 * For completion notice
225 */
226static int e2fsck_update_progress(e2fsck_t ctx, int pass,
227 unsigned long cur, unsigned long max)
228{
229 const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000230 char buf[80];
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000231
232 if (pass == 0)
233 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000234
235 if (ctx->progress_fd) {
236 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
237 write(ctx->progress_fd, buf, strlen(buf));
238 } else {
239 ctx->progress_pos = (ctx->progress_pos+1) & 3;
240 fputc(spinner[ctx->progress_pos], stdout);
241 fputc('\b', stdout);
242 fflush(stdout);
243 }
244 return 0;
245}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000246
247#define PATH_SET "PATH=/sbin"
248
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000249static void reserve_stdio_fds(NOARGS)
250{
251 int fd;
252
253 while (1) {
254 fd = open("/dev/null", O_RDWR);
255 if (fd > 2)
256 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000257 if (fd < 0) {
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000258 fprintf(stderr, "ERROR: Couldn't open "
259 "/dev/null (%s)\n",
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000260 strerror(errno));
261 break;
262 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000263 }
264 close(fd);
265}
266
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000267static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
268{
269 int flush = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000270 int c;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000271#ifdef MTRACE
272 extern void *mallwatch;
273#endif
274 char *oldpath = getenv("PATH");
275 e2fsck_t ctx;
276 errcode_t retval;
277
278 retval = e2fsck_allocate_context(&ctx);
279 if (retval)
280 return retval;
281
282 *ret_ctx = ctx;
283
284 /* Update our PATH to include /sbin */
285 if (oldpath) {
286 char *newpath;
287
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000288 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
289 strlen (oldpath));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000290 if (!newpath)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000291 fatal_error(ctx, "Couldn't malloc() newpath");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000292 strcpy (newpath, PATH_SET);
293 strcat (newpath, ":");
294 strcat (newpath, oldpath);
295 putenv (newpath);
296 } else
297 putenv (PATH_SET);
298
299 setbuf(stdout, NULL);
300 setbuf(stderr, NULL);
301 initialize_ext2_error_table();
302
303 if (argc && *argv)
304 ctx->program_name = *argv;
305 else
306 ctx->program_name = "e2fsck";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000307 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000308 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000309 case 'C':
310 ctx->progress = e2fsck_update_progress;
311 ctx->progress_fd = atoi(optarg);
312 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000313 case 'p':
314 case 'a':
315 ctx->options |= E2F_OPT_PREEN;
316 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
317 break;
318 case 'n':
319 ctx->options |= E2F_OPT_NO;
320 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
321 break;
322 case 'y':
323 ctx->options |= E2F_OPT_YES;
324 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
325 break;
326 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000327#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000328 if (ctx->options & E2F_OPT_TIME)
329 ctx->options |= E2F_OPT_TIME2;
330 else
331 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000332#else
333 fprintf(stderr, "The -t option is not "
334 "supported on this version of e2fsck.\n");
335#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000336 break;
337 case 'c':
338 cflag++;
339 ctx->options |= E2F_OPT_CHECKBLOCKS;
340 break;
341 case 'r':
342 /* What we do by default, anyway! */
343 break;
344 case 'b':
345 ctx->use_superblock = atoi(optarg);
346 break;
347 case 'B':
348 blocksize = atoi(optarg);
349 break;
350 case 'I':
351 ctx->inode_buffer_blocks = atoi(optarg);
352 break;
353 case 'P':
354 ctx->process_inode_size = atoi(optarg);
355 break;
356 case 'L':
357 replace_bad_blocks++;
358 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000359 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000360 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000361 fatal_error(ctx,
362 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000363 strcpy(bad_blocks_file, optarg);
364 break;
365 case 'd':
366 ctx->options |= E2F_OPT_DEBUG;
367 break;
368 case 'f':
369 force = 1;
370 break;
371 case 'F':
372#ifdef BLKFLSBUF
373 flush = 1;
374#else
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000375 fatal_error(ctx, "-F not supported");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000376#endif
377 break;
378 case 'v':
379 verbose = 1;
380 break;
381 case 'V':
382 show_version_only = 1;
383 break;
384#ifdef MTRACE
385 case 'M':
386 mallwatch = (void *) strtol(optarg, NULL, 0);
387 break;
388#endif
389 case 'N':
390 ctx->device_name = optarg;
391 break;
392 case 's':
393 normalize_swapfs = 1;
394 case 'S':
395 swapfs = 1;
396 break;
397 default:
398 usage(ctx);
399 }
400 if (show_version_only)
401 return 0;
402 if (optind != argc - 1)
403 usage(ctx);
404 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
405 !cflag && !swapfs)
406 ctx->options |= E2F_OPT_READONLY;
407 ctx->filesystem_name = argv[optind];
408 if (ctx->device_name == 0)
409 ctx->device_name = ctx->filesystem_name;
410 if (flush) {
411#ifdef BLKFLSBUF
412 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
413
414 if (fd < 0) {
415 com_err("open", errno, "while opening %s for flushing",
416 ctx->filesystem_name);
417 exit(FSCK_ERROR);
418 }
419 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
420 com_err("BLKFLSBUF", errno, "while trying to flush %s",
421 ctx->filesystem_name);
422 exit(FSCK_ERROR);
423 }
424 close(fd);
425#else
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000426 fatal_error(ctx, "BLKFLSBUF not supported");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000427#endif /* BLKFLSBUF */
428 }
429 if (swapfs) {
430 if (cflag || bad_blocks_file) {
431 fprintf(stderr, "Incompatible options not "
432 "allowed when byte-swapping.\n");
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000433 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000434 }
435 }
436 return 0;
437}
438
439static const char *my_ver_string = E2FSPROGS_VERSION;
440static const char *my_ver_date = E2FSPROGS_DATE;
441
442int main (int argc, char *argv[])
443{
444 errcode_t retval = 0;
445 int exit_value = FSCK_OK;
446 int i;
447 ext2_filsys fs = 0;
448 io_manager io_ptr;
449 struct ext2fs_sb *s;
450 const char *lib_ver_date;
451 int my_ver, lib_ver;
452 e2fsck_t ctx;
453 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000454 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000455
456 clear_problem_context(&pctx);
457#ifdef MTRACE
458 mtrace();
459#endif
460#ifdef MCHECK
461 mcheck(0);
462#endif
463 my_ver = ext2fs_parse_version_string(my_ver_string);
464 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
465 if (my_ver > lib_ver) {
466 fprintf( stderr, "Error: ext2fs library version "
467 "out of date!\n");
468 show_version_only++;
469 }
470
471 retval = PRS(argc, argv, &ctx);
472 if (retval) {
473 com_err("e2fsck", retval,
474 "while trying to initialize program");
475 exit(1);
476 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000477 reserve_stdio_fds();
478
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000479#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000480 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000481#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000482
483 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
484 fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n",
485 my_ver_string, my_ver_date, EXT2FS_VERSION,
486 EXT2FS_DATE);
487
488 if (show_version_only) {
489 fprintf(stderr, "\tUsing %s, %s\n",
490 error_message(EXT2_ET_BASE), lib_ver_date);
491 exit(0);
492 }
493
494 check_mount(ctx);
495
496 if (!(ctx->options & E2F_OPT_PREEN) &&
497 !(ctx->options & E2F_OPT_NO) &&
498 !(ctx->options & E2F_OPT_YES)) {
499 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000500 fatal_error(ctx,
501 "need terminal for interactive repairs");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502 }
503 ctx->superblock = ctx->use_superblock;
504restart:
505#if 1
506 io_ptr = unix_io_manager;
507#else
508 io_ptr = test_io_manager;
509 test_io_backing_manager = unix_io_manager;
510#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000511 flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW;
512 if (ctx->superblock && blocksize) {
513 retval = ext2fs_open(ctx->filesystem_name, flags,
514 ctx->superblock, blocksize, io_ptr, &fs);
515 } else if (ctx->superblock) {
516 for (i=0; possible_block_sizes[i]; i++) {
517 retval = ext2fs_open(ctx->filesystem_name, flags,
518 ctx->superblock,
519 possible_block_sizes[i],
520 io_ptr, &fs);
521 if (!retval)
522 break;
523 }
524 } else
525 retval = ext2fs_open(ctx->filesystem_name, flags,
526 0, 0, io_ptr, &fs);
527 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
528 ((retval == EXT2_ET_BAD_MAGIC) ||
529 ((retval == 0) && ext2fs_check_desc(fs)))) {
530 if (!fs || (fs->group_desc_count > 1)) {
531 printf("%s trying backup blocks...\n",
532 retval ? "Couldn't find ext2 superblock," :
533 "Group descriptors look bad...");
534 ctx->superblock = get_backup_sb(fs);
535 if (fs)
536 ext2fs_close(fs);
537 goto restart;
538 }
539 }
540 if (retval) {
541 com_err(ctx->program_name, retval, "while trying to open %s",
542 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000543 if (retval == EXT2_ET_REV_TOO_HIGH) {
544 printf("The filesystem revision is apparently "
545 "too high for this version of e2fsck.\n"
546 "(Or the filesystem superblock "
547 "is corrupt)\n\n");
548 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
549 } else if (retval == EXT2_ET_SHORT_READ)
550 printf("Could this be a zero-length partition?\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551 else if ((retval == EPERM) || (retval == EACCES))
552 printf("You must have %s access to the "
553 "filesystem or be root\n",
554 (ctx->options & E2F_OPT_READONLY) ?
555 "r/o" : "r/w");
556 else if (retval == ENXIO)
557 printf("Possibly non-existent or swap device?\n");
Theodore Ts'o68227541997-11-04 04:25:22 +0000558#ifdef EROFS
559 else if (retval == EROFS)
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000560 printf("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000561 "to do a read-only\n"
562 "check of the device.\n");
Theodore Ts'o68227541997-11-04 04:25:22 +0000563#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000564 else
565 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000566 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000567 }
568 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000569 fs->priv_data = ctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000570#ifdef EXT2_CURRENT_REV
571 if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) {
572 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
573 "while trying to open %s",
574 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000575 get_newer:
576 fatal_error(ctx, "Get a newer version of e2fsck!");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000577 }
578#endif
579 /*
580 * Check for compatibility with the feature sets. We need to
581 * be more stringent than ext2fs_open().
582 */
583 s = (struct ext2fs_sb *) fs->super;
584 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
Theodore Ts'o8144d671998-07-09 05:33:18 +0000585 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000586 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
587 "(%s)", ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000588 goto get_newer;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000589 }
590 if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
591 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
592 "(%s)", ctx->filesystem_name);
593 goto get_newer;
594 }
595
596 /*
597 * If the user specified a specific superblock, presumably the
598 * master superblock has been trashed. So we mark the
599 * superblock as dirty, so it can be written out.
600 */
601 if (ctx->superblock &&
602 !(ctx->options & E2F_OPT_READONLY))
603 ext2fs_mark_super_dirty(fs);
604
605 /*
606 * Don't overwrite the backup superblock and block
607 * descriptors, until we're sure the filesystem is OK....
608 */
609 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
610
611 ehandler_init(fs->io);
612
613 if (ctx->superblock)
614 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
615 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000616 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000617 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000618 check_if_skip(ctx);
619 if (bad_blocks_file)
620 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
621 else if (cflag)
622 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000623 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000624 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625
626 if (normalize_swapfs) {
627 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
628 ext2fs_native_flag()) {
629 fprintf(stderr, "%s: Filesystem byte order "
630 "already normalized.\n", ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000631 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000632 }
633 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000634 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000635 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000636 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000637 exit(FSCK_ERROR);
638 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000639
640 /*
641 * Mark the system as valid, 'til proven otherwise
642 */
643 ext2fs_mark_valid(fs);
644
645 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
646 if (retval) {
647 com_err(ctx->program_name, retval,
648 "while reading bad blocks inode");
649 preenhalt(ctx);
650 printf("This doesn't bode well, but we'll try to go on...\n");
651 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000652
653 run_result = e2fsck_run(ctx);
654 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000655 printf("Restarting e2fsck from the beginning...\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000656 retval = e2fsck_reset_context(ctx);
657 if (retval) {
658 com_err(ctx->program_name, retval,
659 "while resetting context");
660 exit(1);
661 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000662 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000663 goto restart;
664 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000665 if (run_result & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000666 exit(FSCK_ERROR);
667 if (run_result & E2F_FLAG_CANCEL)
668 ext2fs_unmark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000669
670#ifdef MTRACE
671 mtrace_print("Cleanup");
672#endif
673 if (ext2fs_test_changed(fs)) {
674 exit_value = FSCK_NONDESTRUCT;
675 if (!(ctx->options & E2F_OPT_PREEN))
676 printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n",
677 ctx->device_name);
678 if (root_filesystem && !read_only_root) {
679 printf("%s: ***** REBOOT LINUX *****\n",
680 ctx->device_name);
681 exit_value = FSCK_REBOOT;
682 }
683 }
684 if (ext2fs_test_valid(fs))
685 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
686 else
687 exit_value = FSCK_UNCORRECTED;
688 if (!(ctx->options & E2F_OPT_READONLY)) {
689 if (ext2fs_test_valid(fs)) {
690 if (!(fs->super->s_state & EXT2_VALID_FS))
691 exit_value = FSCK_NONDESTRUCT;
692 fs->super->s_state = EXT2_VALID_FS;
693 } else
694 fs->super->s_state &= ~EXT2_VALID_FS;
695 fs->super->s_mnt_count = 0;
696 fs->super->s_lastcheck = time(NULL);
697 ext2fs_mark_super_dirty(fs);
698 }
699 show_stats(ctx);
700
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000701 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000702
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000703#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000704 if (ctx->options & E2F_OPT_TIME)
705 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000706#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000707
708 e2fsck_free_context(ctx);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000709 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000710
711 return exit_value;
712}