blob: e33a814ecd245b6b1a2c15cf2a084b536e41d1ad [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>
Theodore Ts'o5596def1999-07-19 15:27:37 +000021#include <signal.h>
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000022#ifdef HAVE_GETOPT_H
23#include <getopt.h>
24#endif
25#include <unistd.h>
26#ifdef HAVE_ERRNO_H
27#include <errno.h>
28#endif
29#ifdef HAVE_MNTENT_H
30#include <mntent.h>
31#endif
32#include <sys/ioctl.h>
33#include <malloc.h>
34
35#include "et/com_err.h"
36#include "e2fsck.h"
37#include "problem.h"
38#include "../version.h"
39
40extern int isatty(int);
41
42/* Command line options */
43static int blocksize = 0;
44static int swapfs = 0;
45static int normalize_swapfs = 0;
46static int cflag = 0; /* check disk */
47static int show_version_only = 0;
48static int force = 0;
49static int verbose = 0;
50
51static int replace_bad_blocks = 0;
52static char *bad_blocks_file = 0;
53
54static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
55
56static int root_filesystem = 0;
57static int read_only_root = 0;
58
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000059static void usage(e2fsck_t ctx)
60{
61 fprintf(stderr,
62 "Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
63 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'o5596def1999-07-19 15:27:37 +000064 "\t\t[-l|-L bad_blocks_file] [-C fd] device\n",
65 ctx->program_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 exit(FSCK_USAGE);
67}
68
69static void show_stats(e2fsck_t ctx)
70{
71 ext2_filsys fs = ctx->fs;
72 int inodes, inodes_used, blocks, blocks_used;
73 int dir_links;
74 int num_files, num_links;
75 int frag_percent;
76
77 dir_links = 2 * ctx->fs_directory_count - 1;
78 num_files = ctx->fs_total_count - dir_links;
79 num_links = ctx->fs_links_count - dir_links;
80 inodes = fs->super->s_inodes_count;
81 inodes_used = (fs->super->s_inodes_count -
82 fs->super->s_free_inodes_count);
83 blocks = fs->super->s_blocks_count;
84 blocks_used = (fs->super->s_blocks_count -
85 fs->super->s_free_blocks_count);
86
87 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
88 frag_percent = (frag_percent + 5) / 10;
89
90 if (!verbose) {
91 printf("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n",
92 ctx->device_name, inodes_used, inodes,
93 frag_percent / 10, frag_percent % 10,
94 blocks_used, blocks);
95 return;
96 }
97 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
98 (inodes_used != 1) ? "s" : "",
99 100 * inodes_used / inodes);
100 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
101 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
102 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
103 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
104 printf ("%8d block%s used (%d%%)\n"
105 "%8d bad block%s\n", blocks_used,
106 (blocks_used != 1) ? "s" : "",
107 100 * blocks_used / blocks, ctx->fs_badblocks_count,
108 ctx->fs_badblocks_count != 1 ? "s" : "");
109 printf ("\n%8d regular file%s\n"
110 "%8d director%s\n"
111 "%8d character device file%s\n"
112 "%8d block device file%s\n"
113 "%8d fifo%s\n"
114 "%8d link%s\n"
115 "%8d symbolic link%s (%d fast symbolic link%s)\n"
116 "%8d socket%s\n"
117 "--------\n"
118 "%8d file%s\n",
119 ctx->fs_regular_count,
120 (ctx->fs_regular_count != 1) ? "s" : "",
121 ctx->fs_directory_count,
122 (ctx->fs_directory_count != 1) ? "ies" : "y",
123 ctx->fs_chardev_count,
124 (ctx->fs_chardev_count != 1) ? "s" : "",
125 ctx->fs_blockdev_count,
126 (ctx->fs_blockdev_count != 1) ? "s" : "",
127 ctx->fs_fifo_count,
128 (ctx->fs_fifo_count != 1) ? "s" : "",
129 ctx->fs_links_count - dir_links,
130 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
131 ctx->fs_symlinks_count,
132 (ctx->fs_symlinks_count != 1) ? "s" : "",
133 ctx->fs_fast_symlinks_count,
134 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
135 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
136 ctx->fs_total_count - dir_links,
137 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
138}
139
140static void check_mount(e2fsck_t ctx)
141{
142 errcode_t retval;
143 int mount_flags, cont, fd;
144
145 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
146 if (retval) {
147 com_err("ext2fs_check_if_mount", retval,
148 "while determining whether %s is mounted.",
149 ctx->filesystem_name);
150 return;
151 }
152 if (!(mount_flags & EXT2_MF_MOUNTED))
153 return;
154
155#if (defined(__linux__) && defined(HAVE_MNTENT_H))
156 /*
157 * If the root is mounted read-only, then /etc/mtab is
158 * probably not correct; so we won't issue a warning based on
159 * it.
160 */
161 fd = open(MOUNTED, O_RDWR);
162 if (fd < 0) {
163 if (errno == EROFS)
164 return;
165 } else
166 close(fd);
167#endif
168
169 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000170 printf("Warning! %s is mounted.\n", ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000171 return;
172 }
173
Theodore Ts'o5596def1999-07-19 15:27:37 +0000174 printf("%s is mounted. ", ctx->filesystem_name);
Theodore Ts'o74033351999-07-01 03:00:47 +0000175 if (!isatty(0) || !isatty(1)) {
176 printf("Cannot continue, aborting.\n\n");
177 exit(FSCK_ERROR);
178 }
179 printf("\n\n\007\007\007\007WARNING!!! "
180 "Running e2fsck on a mounted filesystem may cause\n"
181 "SEVERE filesystem damage.\007\007\007\n\n");
182 cont = ask_yn("Do you really want to continue", -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000183 if (!cont) {
184 printf ("check aborted.\n");
185 exit (0);
186 }
187 return;
188}
189
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000190/*
191 * This routine checks to see if a filesystem can be skipped; if so,
192 * it will exit with E2FSCK_OK. Under some conditions it will print a
193 * message explaining why a check is being forced.
194 */
195static void check_if_skip(e2fsck_t ctx)
196{
197 ext2_filsys fs = ctx->fs;
198 const char *reason = NULL;
199
200 if (force || bad_blocks_file || cflag || swapfs)
201 return;
202
203 if (fs->super->s_state & EXT2_ERROR_FS)
204 reason = "contains a file system with errors";
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000205 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
206 reason = "was not cleanly unmounted";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000207 else if (fs->super->s_mnt_count >=
208 (unsigned) fs->super->s_max_mnt_count)
209 reason = "has reached maximal mount count";
210 else if (fs->super->s_checkinterval &&
211 time(0) >= (fs->super->s_lastcheck +
212 fs->super->s_checkinterval))
213 reason = "has gone too long without being checked";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000214 if (reason) {
215 printf("%s %s, check forced.\n", ctx->device_name, reason);
216 return;
217 }
218 printf("%s: clean, %d/%d files, %d/%d blocks\n", ctx->device_name,
219 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
220 fs->super->s_inodes_count,
221 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
222 fs->super->s_blocks_count);
223 ext2fs_close(fs);
224 exit(FSCK_OK);
225}
226
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000227/*
228 * For completion notice
229 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000230struct percent_tbl {
231 int max_pass;
232 int table[32];
233};
234struct percent_tbl e2fsck_tbl = {
235 5, { 0, 70, 90, 92, 95, 100 }
236};
237static int dpywidth = 50;
238static char bar[] =
239 "==============================================================="
240 "===============================================================";
241static char spaces[] =
242 " "
243 " ";
244
245static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
246 int max)
247{
248 float percent;
249
250 if (pass <= 0)
251 return 0.0;
252 if (pass > tbl->max_pass)
253 return 100.0;
254 percent = ((float) curr) / ((float) max);
255 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
256 + tbl->table[pass-1]);
257}
258
259extern void e2fsck_clear_progbar(e2fsck_t ctx)
260{
261 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
262 return;
263
264 printf("%s\r", spaces + (sizeof(spaces) - 80));
265 ctx->flags &= ~E2F_FLAG_PROG_BAR;
266}
267
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000268static int e2fsck_update_progress(e2fsck_t ctx, int pass,
269 unsigned long cur, unsigned long max)
270{
271 const char spinner[] = "\\|/-";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000272 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000273 int i;
274 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000275
276 if (pass == 0)
277 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000278
279 if (ctx->progress_fd) {
280 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
281 write(ctx->progress_fd, buf, strlen(buf));
282 } else {
Theodore Ts'o5596def1999-07-19 15:27:37 +0000283 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
284 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000285 ctx->progress_pos = (ctx->progress_pos+1) & 3;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000286 ctx->flags |= E2F_FLAG_PROG_BAR;
287 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
288 if (ctx->progress_last_percent == (int) 1000 * percent)
289 return 0;
290 ctx->progress_last_percent = (int) 1000 * percent;
291 i = ((percent * dpywidth) + 50) / 100;
292 printf("%s: |%s%s", ctx->device_name,
293 bar + (sizeof(bar) - (i+1)),
294 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
295 if (percent == 100.0)
296 fputc('|', stdout);
297 else
298 fputc(spinner[ctx->progress_pos & 3], stdout);
299 printf(" %4.1f%% \r", percent);
300 if (percent == 100.0)
301 e2fsck_clear_progbar(ctx);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000302 fflush(stdout);
303 }
304 return 0;
305}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000306
307#define PATH_SET "PATH=/sbin"
308
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000309static void reserve_stdio_fds(NOARGS)
310{
311 int fd;
312
313 while (1) {
314 fd = open("/dev/null", O_RDWR);
315 if (fd > 2)
316 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000317 if (fd < 0) {
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000318 fprintf(stderr, "ERROR: Couldn't open "
319 "/dev/null (%s)\n",
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000320 strerror(errno));
321 break;
322 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000323 }
324 close(fd);
325}
326
Theodore Ts'o5596def1999-07-19 15:27:37 +0000327static e2fsck_t global_signal_ctx;
328
329static void signal_progress_on(int sig)
330{
331 e2fsck_t ctx = global_signal_ctx;
332
333 if (!ctx)
334 return;
335
336 ctx->progress = e2fsck_update_progress;
337 ctx->progress_fd = 0;
338}
339
340static void signal_progress_off(int sig)
341{
342 e2fsck_t ctx = global_signal_ctx;
343
344 if (!ctx)
345 return;
346
347 e2fsck_clear_progbar(ctx);
348 ctx->progress = 0;
349}
350
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000351static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
352{
353 int flush = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000354 int c;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000355#ifdef MTRACE
356 extern void *mallwatch;
357#endif
358 char *oldpath = getenv("PATH");
359 e2fsck_t ctx;
360 errcode_t retval;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000361 struct sigaction sa;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000362
363 retval = e2fsck_allocate_context(&ctx);
364 if (retval)
365 return retval;
366
367 *ret_ctx = ctx;
368
369 /* Update our PATH to include /sbin */
370 if (oldpath) {
371 char *newpath;
372
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000373 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
374 strlen (oldpath));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000375 if (!newpath)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000376 fatal_error(ctx, "Couldn't malloc() newpath");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000377 strcpy (newpath, PATH_SET);
378 strcat (newpath, ":");
379 strcat (newpath, oldpath);
380 putenv (newpath);
381 } else
382 putenv (PATH_SET);
383
384 setbuf(stdout, NULL);
385 setbuf(stderr, NULL);
386 initialize_ext2_error_table();
387
388 if (argc && *argv)
389 ctx->program_name = *argv;
390 else
391 ctx->program_name = "e2fsck";
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000392 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000393 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000394 case 'C':
395 ctx->progress = e2fsck_update_progress;
396 ctx->progress_fd = atoi(optarg);
397 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000398 case 'p':
399 case 'a':
400 ctx->options |= E2F_OPT_PREEN;
401 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
402 break;
403 case 'n':
404 ctx->options |= E2F_OPT_NO;
405 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
406 break;
407 case 'y':
408 ctx->options |= E2F_OPT_YES;
409 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
410 break;
411 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000412#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000413 if (ctx->options & E2F_OPT_TIME)
414 ctx->options |= E2F_OPT_TIME2;
415 else
416 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000417#else
418 fprintf(stderr, "The -t option is not "
419 "supported on this version of e2fsck.\n");
420#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000421 break;
422 case 'c':
423 cflag++;
424 ctx->options |= E2F_OPT_CHECKBLOCKS;
425 break;
426 case 'r':
427 /* What we do by default, anyway! */
428 break;
429 case 'b':
430 ctx->use_superblock = atoi(optarg);
431 break;
432 case 'B':
433 blocksize = atoi(optarg);
434 break;
435 case 'I':
436 ctx->inode_buffer_blocks = atoi(optarg);
437 break;
438 case 'P':
439 ctx->process_inode_size = atoi(optarg);
440 break;
441 case 'L':
442 replace_bad_blocks++;
443 case 'l':
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000444 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000445 if (!bad_blocks_file)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000446 fatal_error(ctx,
447 "Couldn't malloc bad_blocks_file");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000448 strcpy(bad_blocks_file, optarg);
449 break;
450 case 'd':
451 ctx->options |= E2F_OPT_DEBUG;
452 break;
453 case 'f':
454 force = 1;
455 break;
456 case 'F':
457#ifdef BLKFLSBUF
458 flush = 1;
459#else
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000460 fatal_error(ctx, "-F not supported");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000461#endif
462 break;
463 case 'v':
464 verbose = 1;
465 break;
466 case 'V':
467 show_version_only = 1;
468 break;
469#ifdef MTRACE
470 case 'M':
471 mallwatch = (void *) strtol(optarg, NULL, 0);
472 break;
473#endif
474 case 'N':
475 ctx->device_name = optarg;
476 break;
477 case 's':
478 normalize_swapfs = 1;
479 case 'S':
480 swapfs = 1;
481 break;
482 default:
483 usage(ctx);
484 }
485 if (show_version_only)
486 return 0;
487 if (optind != argc - 1)
488 usage(ctx);
489 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
490 !cflag && !swapfs)
491 ctx->options |= E2F_OPT_READONLY;
492 ctx->filesystem_name = argv[optind];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000493 if (flush) {
494#ifdef BLKFLSBUF
495 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
496
497 if (fd < 0) {
498 com_err("open", errno, "while opening %s for flushing",
499 ctx->filesystem_name);
500 exit(FSCK_ERROR);
501 }
502 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
503 com_err("BLKFLSBUF", errno, "while trying to flush %s",
504 ctx->filesystem_name);
505 exit(FSCK_ERROR);
506 }
507 close(fd);
508#else
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000509 fatal_error(ctx, "BLKFLSBUF not supported");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510#endif /* BLKFLSBUF */
511 }
512 if (swapfs) {
513 if (cflag || bad_blocks_file) {
514 fprintf(stderr, "Incompatible options not "
515 "allowed when byte-swapping.\n");
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000516 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000517 }
518 }
Theodore Ts'o5596def1999-07-19 15:27:37 +0000519 /*
520 * Set up signal action
521 */
522 memset(&sa, 0, sizeof(struct sigaction));
523#ifdef SA_RESTART
524 sa.sa_flags = SA_RESTART;
525#endif
526 global_signal_ctx = ctx;
527 sa.sa_handler = signal_progress_on;
528 sigaction(SIGUSR1, &sa, 0);
529 sa.sa_handler = signal_progress_off;
530 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000531 return 0;
532}
533
534static const char *my_ver_string = E2FSPROGS_VERSION;
535static const char *my_ver_date = E2FSPROGS_DATE;
536
537int main (int argc, char *argv[])
538{
539 errcode_t retval = 0;
540 int exit_value = FSCK_OK;
541 int i;
542 ext2_filsys fs = 0;
543 io_manager io_ptr;
544 struct ext2fs_sb *s;
545 const char *lib_ver_date;
546 int my_ver, lib_ver;
547 e2fsck_t ctx;
548 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000549 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000550
551 clear_problem_context(&pctx);
552#ifdef MTRACE
553 mtrace();
554#endif
555#ifdef MCHECK
556 mcheck(0);
557#endif
558 my_ver = ext2fs_parse_version_string(my_ver_string);
559 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
560 if (my_ver > lib_ver) {
561 fprintf( stderr, "Error: ext2fs library version "
562 "out of date!\n");
563 show_version_only++;
564 }
565
566 retval = PRS(argc, argv, &ctx);
567 if (retval) {
568 com_err("e2fsck", retval,
569 "while trying to initialize program");
570 exit(1);
571 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000572 reserve_stdio_fds();
573
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000574#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000576#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000577
578 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
579 fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n",
580 my_ver_string, my_ver_date, EXT2FS_VERSION,
581 EXT2FS_DATE);
582
583 if (show_version_only) {
584 fprintf(stderr, "\tUsing %s, %s\n",
585 error_message(EXT2_ET_BASE), lib_ver_date);
586 exit(0);
587 }
588
589 check_mount(ctx);
590
591 if (!(ctx->options & E2F_OPT_PREEN) &&
592 !(ctx->options & E2F_OPT_NO) &&
593 !(ctx->options & E2F_OPT_YES)) {
594 if (!isatty (0) || !isatty (1))
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000595 fatal_error(ctx,
596 "need terminal for interactive repairs");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000597 }
598 ctx->superblock = ctx->use_superblock;
599restart:
600#if 1
601 io_ptr = unix_io_manager;
602#else
603 io_ptr = test_io_manager;
604 test_io_backing_manager = unix_io_manager;
605#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000606 flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW;
607 if (ctx->superblock && blocksize) {
608 retval = ext2fs_open(ctx->filesystem_name, flags,
609 ctx->superblock, blocksize, io_ptr, &fs);
610 } else if (ctx->superblock) {
611 for (i=0; possible_block_sizes[i]; i++) {
612 retval = ext2fs_open(ctx->filesystem_name, flags,
613 ctx->superblock,
614 possible_block_sizes[i],
615 io_ptr, &fs);
616 if (!retval)
617 break;
618 }
619 } else
620 retval = ext2fs_open(ctx->filesystem_name, flags,
621 0, 0, io_ptr, &fs);
622 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
623 ((retval == EXT2_ET_BAD_MAGIC) ||
624 ((retval == 0) && ext2fs_check_desc(fs)))) {
625 if (!fs || (fs->group_desc_count > 1)) {
626 printf("%s trying backup blocks...\n",
627 retval ? "Couldn't find ext2 superblock," :
628 "Group descriptors look bad...");
629 ctx->superblock = get_backup_sb(fs);
630 if (fs)
631 ext2fs_close(fs);
632 goto restart;
633 }
634 }
635 if (retval) {
636 com_err(ctx->program_name, retval, "while trying to open %s",
637 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000638 if (retval == EXT2_ET_REV_TOO_HIGH) {
639 printf("The filesystem revision is apparently "
640 "too high for this version of e2fsck.\n"
641 "(Or the filesystem superblock "
642 "is corrupt)\n\n");
643 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
644 } else if (retval == EXT2_ET_SHORT_READ)
645 printf("Could this be a zero-length partition?\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000646 else if ((retval == EPERM) || (retval == EACCES))
647 printf("You must have %s access to the "
648 "filesystem or be root\n",
649 (ctx->options & E2F_OPT_READONLY) ?
650 "r/o" : "r/w");
651 else if (retval == ENXIO)
652 printf("Possibly non-existent or swap device?\n");
Theodore Ts'o68227541997-11-04 04:25:22 +0000653#ifdef EROFS
654 else if (retval == EROFS)
Theodore Ts'o813bbb21999-06-22 03:17:45 +0000655 printf("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000656 "to do a read-only\n"
657 "check of the device.\n");
Theodore Ts'o68227541997-11-04 04:25:22 +0000658#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000659 else
660 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000661 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 }
663 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000664 fs->priv_data = ctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665#ifdef EXT2_CURRENT_REV
666 if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) {
667 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
668 "while trying to open %s",
669 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000670 get_newer:
671 fatal_error(ctx, "Get a newer version of e2fsck!");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000672 }
673#endif
674 /*
675 * Check for compatibility with the feature sets. We need to
676 * be more stringent than ext2fs_open().
677 */
678 s = (struct ext2fs_sb *) fs->super;
679 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
Theodore Ts'o8144d671998-07-09 05:33:18 +0000680 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000681 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
682 "(%s)", ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000683 goto get_newer;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684 }
685 if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
686 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
687 "(%s)", ctx->filesystem_name);
688 goto get_newer;
689 }
Theodore Ts'o5596def1999-07-19 15:27:37 +0000690 if (ctx->device_name == 0 &&
691 (s->s_volume_name[0] != 0)) {
692 char *cp = malloc(sizeof(s->s_volume_name)+1);
693 if (cp) {
694 strncpy(cp, s->s_volume_name,
695 sizeof(s->s_volume_name));
696 cp[sizeof(s->s_volume_name)] = 0;
697 ctx->device_name = cp;
698 }
699 }
700 if (ctx->device_name == 0)
701 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000702
703 /*
704 * If the user specified a specific superblock, presumably the
705 * master superblock has been trashed. So we mark the
706 * superblock as dirty, so it can be written out.
707 */
708 if (ctx->superblock &&
709 !(ctx->options & E2F_OPT_READONLY))
710 ext2fs_mark_super_dirty(fs);
711
712 /*
713 * Don't overwrite the backup superblock and block
714 * descriptors, until we're sure the filesystem is OK....
715 */
716 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
717
718 ehandler_init(fs->io);
719
720 if (ctx->superblock)
721 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
722 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000723 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000724 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000725 check_if_skip(ctx);
726 if (bad_blocks_file)
727 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
728 else if (cflag)
729 test_disk(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000730 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000731 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000732
733 if (normalize_swapfs) {
734 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
735 ext2fs_native_flag()) {
736 fprintf(stderr, "%s: Filesystem byte order "
737 "already normalized.\n", ctx->device_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000738 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000739 }
740 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000741 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000742 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000743 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000744 exit(FSCK_ERROR);
745 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000746
747 /*
748 * Mark the system as valid, 'til proven otherwise
749 */
750 ext2fs_mark_valid(fs);
751
752 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
753 if (retval) {
754 com_err(ctx->program_name, retval,
755 "while reading bad blocks inode");
756 preenhalt(ctx);
757 printf("This doesn't bode well, but we'll try to go on...\n");
758 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000759
760 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000761 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000762 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000763 printf("Restarting e2fsck from the beginning...\n");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000764 retval = e2fsck_reset_context(ctx);
765 if (retval) {
766 com_err(ctx->program_name, retval,
767 "while resetting context");
768 exit(1);
769 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000770 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000771 goto restart;
772 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000773 if (run_result & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000774 exit(FSCK_ERROR);
775 if (run_result & E2F_FLAG_CANCEL)
776 ext2fs_unmark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000777
778#ifdef MTRACE
779 mtrace_print("Cleanup");
780#endif
781 if (ext2fs_test_changed(fs)) {
782 exit_value = FSCK_NONDESTRUCT;
783 if (!(ctx->options & E2F_OPT_PREEN))
784 printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n",
785 ctx->device_name);
786 if (root_filesystem && !read_only_root) {
787 printf("%s: ***** REBOOT LINUX *****\n",
788 ctx->device_name);
789 exit_value = FSCK_REBOOT;
790 }
791 }
792 if (ext2fs_test_valid(fs))
793 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
794 else
795 exit_value = FSCK_UNCORRECTED;
796 if (!(ctx->options & E2F_OPT_READONLY)) {
797 if (ext2fs_test_valid(fs)) {
798 if (!(fs->super->s_state & EXT2_VALID_FS))
799 exit_value = FSCK_NONDESTRUCT;
800 fs->super->s_state = EXT2_VALID_FS;
801 } else
802 fs->super->s_state &= ~EXT2_VALID_FS;
803 fs->super->s_mnt_count = 0;
804 fs->super->s_lastcheck = time(NULL);
805 ext2fs_mark_super_dirty(fs);
806 }
807 show_stats(ctx);
808
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000809 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000810
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000811#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000812 if (ctx->options & E2F_OPT_TIME)
813 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000814#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000815
816 e2fsck_free_context(ctx);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000817 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000818
819 return exit_value;
820}