]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/unix.c
Fix e2fsck's handling of external journals,and update journal
[thirdparty/e2fsprogs.git] / e2fsck / unix.c
CommitLineData
1b6bf175
TT
1/*
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>
1b6bf175 19#include <time.h>
9ecd8bec 20#ifdef HAVE_SIGNAL_H
5596defa 21#include <signal.h>
9ecd8bec 22#endif
1b6bf175
TT
23#ifdef HAVE_GETOPT_H
24#include <getopt.h>
373b8337
TT
25#else
26extern char *optarg;
27extern int optind;
1b6bf175
TT
28#endif
29#include <unistd.h>
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33#ifdef HAVE_MNTENT_H
34#include <mntent.h>
35#endif
36#include <sys/ioctl.h>
37#include <malloc.h>
38
39#include "et/com_err.h"
40#include "e2fsck.h"
41#include "problem.h"
42#include "../version.h"
43
1b6bf175
TT
44/* Command line options */
45static int blocksize = 0;
46static int swapfs = 0;
47static int normalize_swapfs = 0;
48static int cflag = 0; /* check disk */
49static int show_version_only = 0;
50static int force = 0;
51static int verbose = 0;
52
53static int replace_bad_blocks = 0;
54static char *bad_blocks_file = 0;
55
56static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
57
58static int root_filesystem = 0;
59static int read_only_root = 0;
60
243dc31f
TT
61e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
62
1b6bf175
TT
63static void usage(e2fsck_t ctx)
64{
65 fprintf(stderr,
0c4a0726 66 _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
1b6bf175 67 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
adee8d75 68 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal] device\n"),
5596defa 69 ctx->program_name);
b55199ea 70
0c4a0726 71 fprintf(stderr, _("\nEmergency help:\n"
b55199ea
TT
72 " -p Automatic repair (no questions)\n"
73 " -n Make no changes to the filesystem\n"
74 " -y Assume \"yes\" to all questions\n"
75 " -c Check for bad blocks\n"
53ef44c4
TT
76 " -f Force checking even if filesystem is marked clean\n"));
77 fprintf(stderr, _(""
b55199ea
TT
78 " -v Be verbose\n"
79 " -b superblock Use alternative superblock\n"
80 " -B blocksize Force blocksize when looking for superblock\n"
adee8d75 81 " -j external-journal Set location of the external journal\n"
b55199ea
TT
82 " -l bad_blocks_file Add to badblocks list\n"
83 " -L bad_blocks_file Set badblocks list\n"
0c4a0726 84 ));
b55199ea 85
1b6bf175
TT
86 exit(FSCK_USAGE);
87}
88
89static void show_stats(e2fsck_t ctx)
90{
91 ext2_filsys fs = ctx->fs;
92 int inodes, inodes_used, blocks, blocks_used;
93 int dir_links;
94 int num_files, num_links;
95 int frag_percent;
96
97 dir_links = 2 * ctx->fs_directory_count - 1;
98 num_files = ctx->fs_total_count - dir_links;
99 num_links = ctx->fs_links_count - dir_links;
100 inodes = fs->super->s_inodes_count;
101 inodes_used = (fs->super->s_inodes_count -
102 fs->super->s_free_inodes_count);
103 blocks = fs->super->s_blocks_count;
104 blocks_used = (fs->super->s_blocks_count -
105 fs->super->s_free_blocks_count);
106
107 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
108 frag_percent = (frag_percent + 5) / 10;
109
110 if (!verbose) {
0c4a0726 111 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
1b6bf175
TT
112 ctx->device_name, inodes_used, inodes,
113 frag_percent / 10, frag_percent % 10,
114 blocks_used, blocks);
115 return;
116 }
0c4a0726
TT
117 /*
118 * This is a bit ugly. But I think there will nearly always be more
119 * than one "thing" to report about, so I won't try writing complex
120 * code to handle one/two/many forms of all words.
121 * Some languages (Italian, at least) never uses the plural form
122 * of foreign words, so in real life this could not be a problem.
123 * md@linux.it - 2000-1-15
124 */
125#ifdef ENABLE_NLS
126 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
127 (inodes_used != 1), 100 * inodes_used / inodes);
128 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
129 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
130 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
131 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
132 printf (_("%8d blocks used (%d%%)\n"
133 "%8d bad blocks\n"), blocks_used,
636a954a
TT
134 (int) ((long long) 100 * blocks_used / blocks),
135 ctx->fs_badblocks_count);
2b94c658 136 printf(_("%8d large files\n"), ctx->large_files);
0c4a0726
TT
137 printf (_("\n%8d regular files\n"
138 "%8d directories\n"
139 "%8d character device files\n"
140 "%8d block device files\n"
141 "%8d fifos\n"
142 "%8d links\n"
143 "%8d symbolic links (%d fast symbolic links)\n"
144 "%8d sockets\n"
145 "--------\n"
146 "%8d files\n"),
147 ctx->fs_regular_count,
148 ctx->fs_directory_count,
149 ctx->fs_chardev_count,
150 ctx->fs_blockdev_count,
151 ctx->fs_fifo_count,
152 ctx->fs_links_count - dir_links,
153 ctx->fs_symlinks_count,
154 ctx->fs_fast_symlinks_count,
155 ctx->fs_sockets_count,
156 ctx->fs_total_count - dir_links);
157#else
1b6bf175
TT
158 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
159 (inodes_used != 1) ? "s" : "",
160 100 * inodes_used / inodes);
161 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
162 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
163 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
164 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
165 printf ("%8d block%s used (%d%%)\n"
166 "%8d bad block%s\n", blocks_used,
167 (blocks_used != 1) ? "s" : "",
168 100 * blocks_used / blocks, ctx->fs_badblocks_count,
169 ctx->fs_badblocks_count != 1 ? "s" : "");
2b94c658
TT
170 printf(_("%8d large file%s\n"), ctx->large_files,
171 (ctx->large_files != 1) ? "s" : "");
1b6bf175
TT
172 printf ("\n%8d regular file%s\n"
173 "%8d director%s\n"
174 "%8d character device file%s\n"
175 "%8d block device file%s\n"
176 "%8d fifo%s\n"
177 "%8d link%s\n"
178 "%8d symbolic link%s (%d fast symbolic link%s)\n"
179 "%8d socket%s\n"
180 "--------\n"
181 "%8d file%s\n",
182 ctx->fs_regular_count,
183 (ctx->fs_regular_count != 1) ? "s" : "",
184 ctx->fs_directory_count,
185 (ctx->fs_directory_count != 1) ? "ies" : "y",
186 ctx->fs_chardev_count,
187 (ctx->fs_chardev_count != 1) ? "s" : "",
188 ctx->fs_blockdev_count,
189 (ctx->fs_blockdev_count != 1) ? "s" : "",
190 ctx->fs_fifo_count,
191 (ctx->fs_fifo_count != 1) ? "s" : "",
192 ctx->fs_links_count - dir_links,
193 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
194 ctx->fs_symlinks_count,
195 (ctx->fs_symlinks_count != 1) ? "s" : "",
196 ctx->fs_fast_symlinks_count,
197 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
198 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
199 ctx->fs_total_count - dir_links,
200 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
0c4a0726 201#endif
1b6bf175
TT
202}
203
204static void check_mount(e2fsck_t ctx)
205{
206 errcode_t retval;
207 int mount_flags, cont, fd;
208
209 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
210 if (retval) {
211 com_err("ext2fs_check_if_mount", retval,
0c4a0726 212 _("while determining whether %s is mounted."),
1b6bf175
TT
213 ctx->filesystem_name);
214 return;
215 }
1b6bf175 216
1b6bf175 217 /*
83e6ac84
TT
218 * If the filesystem isn't mounted, or it's the root filesystem
219 * and it's mounted read-only, then everything's fine.
1b6bf175 220 */
83e6ac84
TT
221 if ((!(mount_flags & EXT2_MF_MOUNTED)) ||
222 ((mount_flags & EXT2_MF_ISROOT) &&
223 (mount_flags & EXT2_MF_READONLY)))
224 return;
225
1b6bf175 226 if (ctx->options & E2F_OPT_READONLY) {
0c4a0726 227 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
1b6bf175
TT
228 return;
229 }
230
0c4a0726 231 printf(_("%s is mounted. "), ctx->filesystem_name);
243dc31f
TT
232 if (!isatty(0) || !isatty(1))
233 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
0c4a0726 234 printf(_("\n\n\007\007\007\007WARNING!!! "
74033350 235 "Running e2fsck on a mounted filesystem may cause\n"
0c4a0726
TT
236 "SEVERE filesystem damage.\007\007\007\n\n"));
237 cont = ask_yn(_("Do you really want to continue"), -1);
1b6bf175 238 if (!cont) {
0c4a0726 239 printf (_("check aborted.\n"));
1b6bf175
TT
240 exit (0);
241 }
242 return;
243}
244
1b6bf175
TT
245/*
246 * This routine checks to see if a filesystem can be skipped; if so,
247 * it will exit with E2FSCK_OK. Under some conditions it will print a
248 * message explaining why a check is being forced.
249 */
250static void check_if_skip(e2fsck_t ctx)
251{
252 ext2_filsys fs = ctx->fs;
253 const char *reason = NULL;
b6a0807b 254 unsigned int reason_arg = 0;
1b6bf175
TT
255
256 if (force || bad_blocks_file || cflag || swapfs)
257 return;
258
259 if (fs->super->s_state & EXT2_ERROR_FS)
b6a0807b 260 reason = _(" contains a file system with errors");
24fc5032 261 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
b6a0807b 262 reason = _(" was not cleanly unmounted");
bc57f153 263 else if ((fs->super->s_max_mnt_count > 0) &&
17390c04 264 (fs->super->s_mnt_count >=
b6a0807b
TT
265 (unsigned) fs->super->s_max_mnt_count)) {
266 reason = _(" has been mounted %u times without being checked");
267 reason_arg = fs->super->s_mnt_count;
268 } else if (fs->super->s_checkinterval &&
1b6bf175 269 time(0) >= (fs->super->s_lastcheck +
b6a0807b
TT
270 fs->super->s_checkinterval)) {
271 reason = _(" has gone %u days without being checked");
272 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
273 }
1b6bf175 274 if (reason) {
b6a0807b
TT
275 fputs(ctx->device_name, stdout);
276 printf(reason, reason_arg);
277 fputs(_(", check forced.\n"), stdout);
1b6bf175
TT
278 return;
279 }
0c4a0726 280 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
1b6bf175
TT
281 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
282 fs->super->s_inodes_count,
283 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
284 fs->super->s_blocks_count);
285 ext2fs_close(fs);
6d222f32
TT
286 ctx->fs = NULL;
287 e2fsck_free_context(ctx);
1b6bf175
TT
288 exit(FSCK_OK);
289}
290
efac9a1b
TT
291/*
292 * For completion notice
293 */
5596defa
TT
294struct percent_tbl {
295 int max_pass;
296 int table[32];
297};
298struct percent_tbl e2fsck_tbl = {
299 5, { 0, 70, 90, 92, 95, 100 }
300};
5596defa
TT
301static char bar[] =
302 "==============================================================="
303 "===============================================================";
304static char spaces[] =
305 " "
306 " ";
307
308static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
309 int max)
310{
311 float percent;
312
313 if (pass <= 0)
314 return 0.0;
b8d164cd 315 if (pass > tbl->max_pass || max == 0)
5596defa
TT
316 return 100.0;
317 percent = ((float) curr) / ((float) max);
318 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
319 + tbl->table[pass-1]);
320}
321
322extern void e2fsck_clear_progbar(e2fsck_t ctx)
323{
324 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
325 return;
326
327 printf("%s\r", spaces + (sizeof(spaces) - 80));
328 ctx->flags &= ~E2F_FLAG_PROG_BAR;
329}
330
efac9a1b
TT
331static int e2fsck_update_progress(e2fsck_t ctx, int pass,
332 unsigned long cur, unsigned long max)
333{
53ef44c4 334 static const char spinner[] = "\\|/-";
efac9a1b 335 char buf[80];
5596defa
TT
336 int i;
337 float percent;
06012323
TT
338 int tick;
339 struct timeval tv;
17390c04 340 static int dpywidth = 0;
f75c28de
TT
341
342 if (pass == 0)
343 return 0;
efac9a1b
TT
344
345 if (ctx->progress_fd) {
346 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
347 write(ctx->progress_fd, buf, strlen(buf));
348 } else {
5596defa
TT
349 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
350 return 0;
17390c04
TT
351 if (dpywidth == 0) {
352 dpywidth = 66 - strlen(ctx->device_name);
353 dpywidth = 8 * (dpywidth / 8);
354 }
e4c8e885
TT
355 /*
356 * Calculate the new progress position. If the
357 * percentage hasn't changed, then we skip out right
358 * away.
359 */
360 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
361 if (ctx->progress_last_percent == (int) 10 * percent)
362 return 0;
363 ctx->progress_last_percent = (int) 10 * percent;
364
365 /*
366 * If we've already updated the spinner once within
367 * the last 1/8th of a second, no point doing it
368 * again.
369 */
06012323
TT
370 gettimeofday(&tv, NULL);
371 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
372 if ((tick == ctx->progress_last_time) &&
373 (cur != max) && (cur != 0))
374 return 0;
375 ctx->progress_last_time = tick;
e4c8e885
TT
376
377 /*
378 * Advance the spinner, and note that the progress bar
379 * will be on the screen
380 */
efac9a1b 381 ctx->progress_pos = (ctx->progress_pos+1) & 3;
5596defa 382 ctx->flags |= E2F_FLAG_PROG_BAR;
e4c8e885 383
5596defa
TT
384 i = ((percent * dpywidth) + 50) / 100;
385 printf("%s: |%s%s", ctx->device_name,
386 bar + (sizeof(bar) - (i+1)),
387 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
388 if (percent == 100.0)
389 fputc('|', stdout);
390 else
391 fputc(spinner[ctx->progress_pos & 3], stdout);
392 printf(" %4.1f%% \r", percent);
393 if (percent == 100.0)
394 e2fsck_clear_progbar(ctx);
efac9a1b
TT
395 fflush(stdout);
396 }
397 return 0;
398}
1b6bf175
TT
399
400#define PATH_SET "PATH=/sbin"
401
5ba23cb1 402static void reserve_stdio_fds(void)
24fc5032
TT
403{
404 int fd;
405
406 while (1) {
407 fd = open("/dev/null", O_RDWR);
408 if (fd > 2)
409 break;
75d83bec 410 if (fd < 0) {
0c4a0726
TT
411 fprintf(stderr, _("ERROR: Couldn't open "
412 "/dev/null (%s)\n"),
75d83bec
TT
413 strerror(errno));
414 break;
415 }
24fc5032
TT
416 }
417 close(fd);
418}
419
9ecd8bec 420#ifdef HAVE_SIGNAL_H
5596defa
TT
421static void signal_progress_on(int sig)
422{
243dc31f 423 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
424
425 if (!ctx)
426 return;
427
428 ctx->progress = e2fsck_update_progress;
429 ctx->progress_fd = 0;
430}
431
432static void signal_progress_off(int sig)
433{
243dc31f 434 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
435
436 if (!ctx)
437 return;
438
439 e2fsck_clear_progbar(ctx);
440 ctx->progress = 0;
441}
9ecd8bec 442#endif
5596defa 443
1b6bf175
TT
444static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
445{
446 int flush = 0;
ae8160e6 447 int c, fd;
1b6bf175
TT
448#ifdef MTRACE
449 extern void *mallwatch;
450#endif
1b6bf175
TT
451 e2fsck_t ctx;
452 errcode_t retval;
9ecd8bec 453#ifdef HAVE_SIGNAL_H
5596defa 454 struct sigaction sa;
9ecd8bec 455#endif
1b6bf175
TT
456
457 retval = e2fsck_allocate_context(&ctx);
458 if (retval)
459 return retval;
460
461 *ret_ctx = ctx;
462
1b6bf175
TT
463 setbuf(stdout, NULL);
464 setbuf(stderr, NULL);
465 initialize_ext2_error_table();
466
467 if (argc && *argv)
468 ctx->program_name = *argv;
469 else
470 ctx->program_name = "e2fsck";
adee8d75 471 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:j:P:l:L:N:Ss")) != EOF)
1b6bf175 472 switch (c) {
efac9a1b
TT
473 case 'C':
474 ctx->progress = e2fsck_update_progress;
475 ctx->progress_fd = atoi(optarg);
fc9a69ca
TT
476 if (!ctx->progress_fd)
477 break;
ae8160e6
TT
478 /* Validate the file descriptor to avoid disasters */
479 fd = dup(ctx->progress_fd);
480 if (fd < 0) {
481 fprintf(stderr,
482 _("Error validating file descriptor %d: %s\n"),
483 ctx->progress_fd,
484 error_message(errno));
485 fatal_error(ctx,
486 _("Invalid completion information file descriptor"));
487 } else
488 close(fd);
efac9a1b 489 break;
1b6bf175
TT
490 case 'p':
491 case 'a':
492 ctx->options |= E2F_OPT_PREEN;
493 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
494 break;
495 case 'n':
496 ctx->options |= E2F_OPT_NO;
497 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
498 break;
499 case 'y':
500 ctx->options |= E2F_OPT_YES;
501 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
502 break;
503 case 't':
8bf191e8 504#ifdef RESOURCE_TRACK
1b6bf175
TT
505 if (ctx->options & E2F_OPT_TIME)
506 ctx->options |= E2F_OPT_TIME2;
507 else
508 ctx->options |= E2F_OPT_TIME;
8bf191e8 509#else
0c4a0726
TT
510 fprintf(stderr, _("The -t option is not "
511 "supported on this version of e2fsck.\n"));
8bf191e8 512#endif
1b6bf175
TT
513 break;
514 case 'c':
515 cflag++;
516 ctx->options |= E2F_OPT_CHECKBLOCKS;
517 break;
518 case 'r':
519 /* What we do by default, anyway! */
520 break;
521 case 'b':
522 ctx->use_superblock = atoi(optarg);
ae6cdcf7 523 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
1b6bf175
TT
524 break;
525 case 'B':
526 blocksize = atoi(optarg);
527 break;
528 case 'I':
529 ctx->inode_buffer_blocks = atoi(optarg);
530 break;
adee8d75
TT
531 case 'j':
532 ctx->journal_name = optarg;
533 break;
1b6bf175
TT
534 case 'P':
535 ctx->process_inode_size = atoi(optarg);
536 break;
537 case 'L':
538 replace_bad_blocks++;
539 case 'l':
54dc7ca2 540 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
1b6bf175 541 if (!bad_blocks_file)
f8188fff
TT
542 fatal_error(ctx,
543 "Couldn't malloc bad_blocks_file");
1b6bf175
TT
544 strcpy(bad_blocks_file, optarg);
545 break;
546 case 'd':
547 ctx->options |= E2F_OPT_DEBUG;
548 break;
549 case 'f':
550 force = 1;
551 break;
552 case 'F':
1b6bf175 553 flush = 1;
1b6bf175
TT
554 break;
555 case 'v':
556 verbose = 1;
557 break;
558 case 'V':
559 show_version_only = 1;
560 break;
561#ifdef MTRACE
562 case 'M':
563 mallwatch = (void *) strtol(optarg, NULL, 0);
564 break;
565#endif
566 case 'N':
567 ctx->device_name = optarg;
568 break;
5df55d7f 569#ifdef ENABLE_SWAPFS
1b6bf175
TT
570 case 's':
571 normalize_swapfs = 1;
572 case 'S':
573 swapfs = 1;
574 break;
5df55d7f
TT
575#else
576 case 's':
577 case 'S':
578 fprintf(stderr, _("Byte-swapping filesystems "
579 "not compiled in this version "
580 "of e2fsck\n"));
581 exit(1);
582#endif
1b6bf175
TT
583 default:
584 usage(ctx);
585 }
586 if (show_version_only)
587 return 0;
588 if (optind != argc - 1)
589 usage(ctx);
590 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
591 !cflag && !swapfs)
592 ctx->options |= E2F_OPT_READONLY;
593 ctx->filesystem_name = argv[optind];
28ffafb0 594 if (flush) {
1b6bf175
TT
595 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
596
597 if (fd < 0) {
0c4a0726
TT
598 com_err("open", errno,
599 _("while opening %s for flushing"),
1b6bf175 600 ctx->filesystem_name);
243dc31f 601 fatal_error(ctx, 0);
1b6bf175 602 }
5df55d7f 603 if ((retval = ext2fs_sync_device(fd, 1))) {
5ba23cb1 604 com_err("ext2fs_sync_device", retval,
0c4a0726 605 _("while trying to flush %s"),
1b6bf175 606 ctx->filesystem_name);
243dc31f 607 fatal_error(ctx, 0);
1b6bf175
TT
608 }
609 close(fd);
1b6bf175 610 }
5df55d7f 611#ifdef ENABLE_SWAPFS
1b6bf175
TT
612 if (swapfs) {
613 if (cflag || bad_blocks_file) {
0c4a0726
TT
614 fprintf(stderr, _("Incompatible options not "
615 "allowed when byte-swapping.\n"));
243dc31f 616 exit(FSCK_USAGE);
1b6bf175
TT
617 }
618 }
5df55d7f 619#endif
9ecd8bec 620#ifdef HAVE_SIGNAL_H
5596defa
TT
621 /*
622 * Set up signal action
623 */
624 memset(&sa, 0, sizeof(struct sigaction));
625#ifdef SA_RESTART
626 sa.sa_flags = SA_RESTART;
627#endif
243dc31f 628 e2fsck_global_ctx = ctx;
5596defa
TT
629 sa.sa_handler = signal_progress_on;
630 sigaction(SIGUSR1, &sa, 0);
631 sa.sa_handler = signal_progress_off;
632 sigaction(SIGUSR2, &sa, 0);
9ecd8bec 633#endif
6d222f32
TT
634
635 /* Update our PATH to include /sbin if we need to run badblocks */
636 if (cflag) {
637 char *oldpath = getenv("PATH");
638 if (oldpath) {
639 char *newpath;
640
641 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
642 strlen (oldpath));
643 if (!newpath)
644 fatal_error(ctx, "Couldn't malloc() newpath");
645 strcpy (newpath, PATH_SET);
646 strcat (newpath, ":");
647 strcat (newpath, oldpath);
648 putenv (newpath);
649 } else
650 putenv (PATH_SET);
651 }
1b6bf175
TT
652 return 0;
653}
654
655static const char *my_ver_string = E2FSPROGS_VERSION;
656static const char *my_ver_date = E2FSPROGS_DATE;
657
658int main (int argc, char *argv[])
659{
660 errcode_t retval = 0;
661 int exit_value = FSCK_OK;
662 int i;
663 ext2_filsys fs = 0;
664 io_manager io_ptr;
5dd8f963 665 struct ext2_super_block *sb;
1b6bf175
TT
666 const char *lib_ver_date;
667 int my_ver, lib_ver;
668 e2fsck_t ctx;
669 struct problem_context pctx;
08b21301 670 int flags, run_result;
1b6bf175
TT
671
672 clear_problem_context(&pctx);
673#ifdef MTRACE
674 mtrace();
675#endif
676#ifdef MCHECK
677 mcheck(0);
0c4a0726
TT
678#endif
679#ifdef ENABLE_NLS
680 setlocale(LC_MESSAGES, "");
681 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
682 textdomain(NLS_CAT_NAME);
1b6bf175
TT
683#endif
684 my_ver = ext2fs_parse_version_string(my_ver_string);
685 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
686 if (my_ver > lib_ver) {
0c4a0726
TT
687 fprintf( stderr, _("Error: ext2fs library version "
688 "out of date!\n"));
1b6bf175
TT
689 show_version_only++;
690 }
691
692 retval = PRS(argc, argv, &ctx);
693 if (retval) {
694 com_err("e2fsck", retval,
0c4a0726 695 _("while trying to initialize program"));
243dc31f 696 exit(FSCK_ERROR);
1b6bf175 697 }
24fc5032
TT
698 reserve_stdio_fds();
699
8bf191e8 700#ifdef RESOURCE_TRACK
1b6bf175 701 init_resource_track(&ctx->global_rtrack);
8bf191e8 702#endif
1b6bf175
TT
703
704 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
0f8973fb
TT
705 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string,
706 my_ver_date);
1b6bf175
TT
707
708 if (show_version_only) {
0c4a0726 709 fprintf(stderr, _("\tUsing %s, %s\n"),
1b6bf175 710 error_message(EXT2_ET_BASE), lib_ver_date);
243dc31f 711 exit(FSCK_OK);
1b6bf175
TT
712 }
713
714 check_mount(ctx);
715
716 if (!(ctx->options & E2F_OPT_PREEN) &&
717 !(ctx->options & E2F_OPT_NO) &&
718 !(ctx->options & E2F_OPT_YES)) {
719 if (!isatty (0) || !isatty (1))
f8188fff 720 fatal_error(ctx,
0c4a0726 721 _("need terminal for interactive repairs"));
1b6bf175
TT
722 }
723 ctx->superblock = ctx->use_superblock;
724restart:
725#if 1
726 io_ptr = unix_io_manager;
727#else
728 io_ptr = test_io_manager;
729 test_io_backing_manager = unix_io_manager;
730#endif
17390c04
TT
731 flags = 0;
732 if ((ctx->options & E2F_OPT_READONLY) == 0)
733 flags |= EXT2_FLAG_RW;
734
1b6bf175
TT
735 if (ctx->superblock && blocksize) {
736 retval = ext2fs_open(ctx->filesystem_name, flags,
737 ctx->superblock, blocksize, io_ptr, &fs);
738 } else if (ctx->superblock) {
739 for (i=0; possible_block_sizes[i]; i++) {
740 retval = ext2fs_open(ctx->filesystem_name, flags,
741 ctx->superblock,
742 possible_block_sizes[i],
743 io_ptr, &fs);
744 if (!retval)
745 break;
746 }
747 } else
748 retval = ext2fs_open(ctx->filesystem_name, flags,
749 0, 0, io_ptr, &fs);
ae6cdcf7
TT
750 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
751 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1b6bf175
TT
752 ((retval == EXT2_ET_BAD_MAGIC) ||
753 ((retval == 0) && ext2fs_check_desc(fs)))) {
754 if (!fs || (fs->group_desc_count > 1)) {
0c4a0726
TT
755 printf(_("%s trying backup blocks...\n"),
756 retval ? _("Couldn't find ext2 superblock,") :
757 _("Group descriptors look bad..."));
1b6bf175
TT
758 ctx->superblock = get_backup_sb(fs);
759 if (fs)
760 ext2fs_close(fs);
761 goto restart;
762 }
763 }
764 if (retval) {
0c4a0726 765 com_err(ctx->program_name, retval, _("while trying to open %s"),
1b6bf175 766 ctx->filesystem_name);
24dd4028 767 if (retval == EXT2_ET_REV_TOO_HIGH) {
0c4a0726 768 printf(_("The filesystem revision is apparently "
24dd4028
TT
769 "too high for this version of e2fsck.\n"
770 "(Or the filesystem superblock "
0c4a0726 771 "is corrupt)\n\n"));
24dd4028
TT
772 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
773 } else if (retval == EXT2_ET_SHORT_READ)
0c4a0726 774 printf(_("Could this be a zero-length partition?\n"));
1b6bf175 775 else if ((retval == EPERM) || (retval == EACCES))
0c4a0726
TT
776 printf(_("You must have %s access to the "
777 "filesystem or be root\n"),
1b6bf175
TT
778 (ctx->options & E2F_OPT_READONLY) ?
779 "r/o" : "r/w");
780 else if (retval == ENXIO)
0c4a0726 781 printf(_("Possibly non-existent or swap device?\n"));
68227542
TT
782#ifdef EROFS
783 else if (retval == EROFS)
0c4a0726 784 printf(_("Disk write-protected; use the -n option "
68227542 785 "to do a read-only\n"
0c4a0726 786 "check of the device.\n"));
68227542 787#endif
1b6bf175
TT
788 else
789 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
243dc31f 790 fatal_error(ctx, 0);
1b6bf175
TT
791 }
792 ctx->fs = fs;
54dc7ca2 793 fs->priv_data = ctx;
5dd8f963
TT
794 sb = fs->super;
795 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1b6bf175 796 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
0c4a0726 797 _("while trying to open %s"),
1b6bf175 798 ctx->filesystem_name);
f8188fff 799 get_newer:
0c4a0726 800 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1b6bf175 801 }
3b5386dc
TT
802
803 /*
804 * Set the device name, which is used whenever we print error
805 * or informational messages to the user.
806 */
807 if (ctx->device_name == 0 &&
5dd8f963
TT
808 (sb->s_volume_name[0] != 0)) {
809 char *cp = malloc(sizeof(sb->s_volume_name)+1);
3b5386dc 810 if (cp) {
5dd8f963
TT
811 strncpy(cp, sb->s_volume_name,
812 sizeof(sb->s_volume_name));
813 cp[sizeof(sb->s_volume_name)] = 0;
3b5386dc
TT
814 ctx->device_name = cp;
815 }
816 }
817 if (ctx->device_name == 0)
818 ctx->device_name = ctx->filesystem_name;
819
17390c04 820 /*
9b565759 821 * Make sure the ext3 superblock fields are consistent.
17390c04 822 */
3b5386dc
TT
823 retval = e2fsck_check_ext3_journal(ctx);
824 if (retval) {
825 com_err(ctx->program_name, retval,
826 _("while checking ext3 journal for %s"),
827 ctx->device_name);
828 ext2fs_close(ctx->fs);
243dc31f 829 fatal_error(ctx, 0);
3b5386dc
TT
830 }
831
9b565759
TT
832 /*
833 * Check to see if we need to do ext3-style recovery. If so,
834 * do it, and then restart the fsck.
835 */
5dd8f963 836 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
2575fb04
TT
837 if (ctx->options & E2F_OPT_READONLY) {
838 printf(_("Warning: skipping journal recovery "
839 "because doing a read-only filesystem "
840 "check.\n"));
841 io_channel_flush(ctx->fs->io);
842 } else {
843 retval = e2fsck_run_ext3_journal(ctx);
844 if (retval) {
845 com_err(ctx->program_name, retval,
3b5386dc 846 _("while recovering ext3 journal of %s"),
2575fb04
TT
847 ctx->device_name);
848 fatal_error(ctx, 0);
849 }
850 ext2fs_close(ctx->fs);
851 ctx->fs = 0;
852 goto restart;
17390c04 853 }
17390c04 854 }
3b5386dc 855
1b6bf175
TT
856 /*
857 * Check for compatibility with the feature sets. We need to
858 * be more stringent than ext2fs_open().
859 */
5dd8f963
TT
860 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
861 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1b6bf175 862 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
3b5386dc 863 "(%s)", ctx->device_name);
f8188fff 864 goto get_newer;
1b6bf175 865 }
5dd8f963 866 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1b6bf175 867 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
3b5386dc 868 "(%s)", ctx->device_name);
1b6bf175
TT
869 goto get_newer;
870 }
1917875f 871#ifdef ENABLE_COMPRESSION
5dd8f963 872 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1917875f
TT
873 com_err(ctx->program_name, 0,
874 _("Warning: compression support is experimental.\n"));
875#endif
1b6bf175
TT
876
877 /*
878 * If the user specified a specific superblock, presumably the
879 * master superblock has been trashed. So we mark the
880 * superblock as dirty, so it can be written out.
881 */
882 if (ctx->superblock &&
883 !(ctx->options & E2F_OPT_READONLY))
884 ext2fs_mark_super_dirty(fs);
885
886 /*
887 * Don't overwrite the backup superblock and block
888 * descriptors, until we're sure the filesystem is OK....
889 */
890 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
891
892 ehandler_init(fs->io);
893
894 if (ctx->superblock)
895 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
896 check_super_block(ctx);
a02ce9df 897 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 898 fatal_error(ctx, 0);
1b6bf175
TT
899 check_if_skip(ctx);
900 if (bad_blocks_file)
901 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
902 else if (cflag)
903 test_disk(ctx);
a02ce9df 904 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 905 fatal_error(ctx, 0);
5df55d7f 906#ifdef ENABLE_SWAPFS
1b6bf175
TT
907 if (normalize_swapfs) {
908 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
909 ext2fs_native_flag()) {
0c4a0726
TT
910 fprintf(stderr, _("%s: Filesystem byte order "
911 "already normalized.\n"), ctx->device_name);
243dc31f 912 fatal_error(ctx, 0);
1b6bf175
TT
913 }
914 }
f8188fff 915 if (swapfs) {
1b6bf175 916 swap_filesys(ctx);
a02ce9df 917 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 918 fatal_error(ctx, 0);
f8188fff 919 }
5df55d7f 920#endif
1b6bf175
TT
921
922 /*
923 * Mark the system as valid, 'til proven otherwise
924 */
925 ext2fs_mark_valid(fs);
926
927 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
928 if (retval) {
929 com_err(ctx->program_name, retval,
0c4a0726 930 _("while reading bad blocks inode"));
1b6bf175 931 preenhalt(ctx);
0c4a0726
TT
932 printf(_("This doesn't bode well,"
933 " but we'll try to go on...\n"));
1b6bf175 934 }
08b21301
TT
935
936 run_result = e2fsck_run(ctx);
5596defa 937 e2fsck_clear_progbar(ctx);
08b21301 938 if (run_result == E2F_FLAG_RESTART) {
0c4a0726 939 printf(_("Restarting e2fsck from the beginning...\n"));
1b6bf175
TT
940 retval = e2fsck_reset_context(ctx);
941 if (retval) {
942 com_err(ctx->program_name, retval,
0c4a0726 943 _("while resetting context"));
243dc31f 944 fatal_error(ctx, 0);
1b6bf175 945 }
73f17cfc 946 ext2fs_close(fs);
1b6bf175
TT
947 goto restart;
948 }
a02ce9df 949 if (run_result & E2F_FLAG_SIGNAL_MASK)
243dc31f 950 fatal_error(ctx, 0);
08b21301
TT
951 if (run_result & E2F_FLAG_CANCEL)
952 ext2fs_unmark_valid(fs);
1b6bf175
TT
953
954#ifdef MTRACE
955 mtrace_print("Cleanup");
956#endif
957 if (ext2fs_test_changed(fs)) {
958 exit_value = FSCK_NONDESTRUCT;
959 if (!(ctx->options & E2F_OPT_PREEN))
0c4a0726 960 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1b6bf175
TT
961 ctx->device_name);
962 if (root_filesystem && !read_only_root) {
0c4a0726 963 printf(_("%s: ***** REBOOT LINUX *****\n"),
1b6bf175
TT
964 ctx->device_name);
965 exit_value = FSCK_REBOOT;
966 }
967 }
968 if (ext2fs_test_valid(fs))
969 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
d3124019
TT
970 else {
971 printf(_("\n%s: ********** WARNING: Filesystem still has "
972 "errors **********\n\n"), ctx->device_name);
1b6bf175 973 exit_value = FSCK_UNCORRECTED;
d3124019 974 }
1b6bf175
TT
975 if (!(ctx->options & E2F_OPT_READONLY)) {
976 if (ext2fs_test_valid(fs)) {
5dd8f963 977 if (!(sb->s_state & EXT2_VALID_FS))
1b6bf175 978 exit_value = FSCK_NONDESTRUCT;
5dd8f963 979 sb->s_state = EXT2_VALID_FS;
1b6bf175 980 } else
5dd8f963
TT
981 sb->s_state &= ~EXT2_VALID_FS;
982 sb->s_mnt_count = 0;
983 sb->s_lastcheck = time(NULL);
1b6bf175
TT
984 ext2fs_mark_super_dirty(fs);
985 }
986 show_stats(ctx);
987
f8188fff 988 e2fsck_write_bitmaps(ctx);
1b6bf175 989
0628ae39
TT
990 ext2fs_close(fs);
991 ctx->fs = NULL;
992 e2fsck_free_context(ctx);
993
8bf191e8 994#ifdef RESOURCE_TRACK
1b6bf175
TT
995 if (ctx->options & E2F_OPT_TIME)
996 print_resource_track(NULL, &ctx->global_rtrack);
8bf191e8 997#endif
1b6bf175 998
1b6bf175
TT
999 return exit_value;
1000}