]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/unix.c
Move linux/jbd.h to ext2fs/kernel-jbd.h, to avoid using the
[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;
1b6bf175
TT
50static int verbose = 0;
51
52static int replace_bad_blocks = 0;
53static char *bad_blocks_file = 0;
54
55static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
56
57static int root_filesystem = 0;
58static int read_only_root = 0;
59
243dc31f
TT
60e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
61
1b6bf175
TT
62static void usage(e2fsck_t ctx)
63{
64 fprintf(stderr,
0c4a0726 65 _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
1b6bf175 66 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
adee8d75 67 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal] device\n"),
5596defa 68 ctx->program_name);
b55199ea 69
0c4a0726 70 fprintf(stderr, _("\nEmergency help:\n"
b55199ea
TT
71 " -p Automatic repair (no questions)\n"
72 " -n Make no changes to the filesystem\n"
73 " -y Assume \"yes\" to all questions\n"
74 " -c Check for bad blocks\n"
53ef44c4
TT
75 " -f Force checking even if filesystem is marked clean\n"));
76 fprintf(stderr, _(""
b55199ea
TT
77 " -v Be verbose\n"
78 " -b superblock Use alternative superblock\n"
79 " -B blocksize Force blocksize when looking for superblock\n"
adee8d75 80 " -j external-journal Set location of the external journal\n"
b55199ea
TT
81 " -l bad_blocks_file Add to badblocks list\n"
82 " -L bad_blocks_file Set badblocks list\n"
0c4a0726 83 ));
b55199ea 84
1b6bf175
TT
85 exit(FSCK_USAGE);
86}
87
88static void show_stats(e2fsck_t ctx)
89{
90 ext2_filsys fs = ctx->fs;
91 int inodes, inodes_used, blocks, blocks_used;
92 int dir_links;
93 int num_files, num_links;
94 int frag_percent;
95
96 dir_links = 2 * ctx->fs_directory_count - 1;
97 num_files = ctx->fs_total_count - dir_links;
98 num_links = ctx->fs_links_count - dir_links;
99 inodes = fs->super->s_inodes_count;
100 inodes_used = (fs->super->s_inodes_count -
101 fs->super->s_free_inodes_count);
102 blocks = fs->super->s_blocks_count;
103 blocks_used = (fs->super->s_blocks_count -
104 fs->super->s_free_blocks_count);
105
106 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
107 frag_percent = (frag_percent + 5) / 10;
108
109 if (!verbose) {
0c4a0726 110 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
1b6bf175
TT
111 ctx->device_name, inodes_used, inodes,
112 frag_percent / 10, frag_percent % 10,
113 blocks_used, blocks);
114 return;
115 }
0c4a0726
TT
116 /*
117 * This is a bit ugly. But I think there will nearly always be more
118 * than one "thing" to report about, so I won't try writing complex
119 * code to handle one/two/many forms of all words.
120 * Some languages (Italian, at least) never uses the plural form
121 * of foreign words, so in real life this could not be a problem.
122 * md@linux.it - 2000-1-15
123 */
124#ifdef ENABLE_NLS
125 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
126 (inodes_used != 1), 100 * inodes_used / inodes);
127 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
128 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
129 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
130 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
131 printf (_("%8d blocks used (%d%%)\n"
132 "%8d bad blocks\n"), blocks_used,
636a954a
TT
133 (int) ((long long) 100 * blocks_used / blocks),
134 ctx->fs_badblocks_count);
2b94c658 135 printf(_("%8d large files\n"), ctx->large_files);
0c4a0726
TT
136 printf (_("\n%8d regular files\n"
137 "%8d directories\n"
138 "%8d character device files\n"
139 "%8d block device files\n"
140 "%8d fifos\n"
141 "%8d links\n"
142 "%8d symbolic links (%d fast symbolic links)\n"
143 "%8d sockets\n"
144 "--------\n"
145 "%8d files\n"),
146 ctx->fs_regular_count,
147 ctx->fs_directory_count,
148 ctx->fs_chardev_count,
149 ctx->fs_blockdev_count,
150 ctx->fs_fifo_count,
151 ctx->fs_links_count - dir_links,
152 ctx->fs_symlinks_count,
153 ctx->fs_fast_symlinks_count,
154 ctx->fs_sockets_count,
155 ctx->fs_total_count - dir_links);
156#else
1b6bf175
TT
157 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
158 (inodes_used != 1) ? "s" : "",
159 100 * inodes_used / inodes);
160 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
161 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
162 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
163 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
164 printf ("%8d block%s used (%d%%)\n"
165 "%8d bad block%s\n", blocks_used,
166 (blocks_used != 1) ? "s" : "",
167 100 * blocks_used / blocks, ctx->fs_badblocks_count,
168 ctx->fs_badblocks_count != 1 ? "s" : "");
2b94c658
TT
169 printf(_("%8d large file%s\n"), ctx->large_files,
170 (ctx->large_files != 1) ? "s" : "");
1b6bf175
TT
171 printf ("\n%8d regular file%s\n"
172 "%8d director%s\n"
173 "%8d character device file%s\n"
174 "%8d block device file%s\n"
175 "%8d fifo%s\n"
176 "%8d link%s\n"
177 "%8d symbolic link%s (%d fast symbolic link%s)\n"
178 "%8d socket%s\n"
179 "--------\n"
180 "%8d file%s\n",
181 ctx->fs_regular_count,
182 (ctx->fs_regular_count != 1) ? "s" : "",
183 ctx->fs_directory_count,
184 (ctx->fs_directory_count != 1) ? "ies" : "y",
185 ctx->fs_chardev_count,
186 (ctx->fs_chardev_count != 1) ? "s" : "",
187 ctx->fs_blockdev_count,
188 (ctx->fs_blockdev_count != 1) ? "s" : "",
189 ctx->fs_fifo_count,
190 (ctx->fs_fifo_count != 1) ? "s" : "",
191 ctx->fs_links_count - dir_links,
192 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
193 ctx->fs_symlinks_count,
194 (ctx->fs_symlinks_count != 1) ? "s" : "",
195 ctx->fs_fast_symlinks_count,
196 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
197 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
198 ctx->fs_total_count - dir_links,
199 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
0c4a0726 200#endif
1b6bf175
TT
201}
202
203static void check_mount(e2fsck_t ctx)
204{
205 errcode_t retval;
4ea7bd04 206 int mount_flags, cont;
1b6bf175
TT
207
208 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
209 if (retval) {
210 com_err("ext2fs_check_if_mount", retval,
0c4a0726 211 _("while determining whether %s is mounted."),
1b6bf175
TT
212 ctx->filesystem_name);
213 return;
214 }
1b6bf175 215
1b6bf175 216 /*
83e6ac84
TT
217 * If the filesystem isn't mounted, or it's the root filesystem
218 * and it's mounted read-only, then everything's fine.
1b6bf175 219 */
83e6ac84
TT
220 if ((!(mount_flags & EXT2_MF_MOUNTED)) ||
221 ((mount_flags & EXT2_MF_ISROOT) &&
222 (mount_flags & EXT2_MF_READONLY)))
223 return;
224
1b6bf175 225 if (ctx->options & E2F_OPT_READONLY) {
0c4a0726 226 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
1b6bf175
TT
227 return;
228 }
229
0c4a0726 230 printf(_("%s is mounted. "), ctx->filesystem_name);
243dc31f
TT
231 if (!isatty(0) || !isatty(1))
232 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
0c4a0726 233 printf(_("\n\n\007\007\007\007WARNING!!! "
74033350 234 "Running e2fsck on a mounted filesystem may cause\n"
0c4a0726
TT
235 "SEVERE filesystem damage.\007\007\007\n\n"));
236 cont = ask_yn(_("Do you really want to continue"), -1);
1b6bf175 237 if (!cont) {
0c4a0726 238 printf (_("check aborted.\n"));
1b6bf175
TT
239 exit (0);
240 }
241 return;
242}
243
1b6bf175
TT
244/*
245 * This routine checks to see if a filesystem can be skipped; if so,
246 * it will exit with E2FSCK_OK. Under some conditions it will print a
247 * message explaining why a check is being forced.
248 */
249static void check_if_skip(e2fsck_t ctx)
250{
251 ext2_filsys fs = ctx->fs;
252 const char *reason = NULL;
b6a0807b 253 unsigned int reason_arg = 0;
1b6bf175 254
d37066a9
TT
255 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
256 cflag || swapfs)
1b6bf175
TT
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':
d37066a9 550 ctx->options |= E2F_OPT_FORCE;
1b6bf175
TT
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) {
4ea7bd04 595 fd = open(ctx->filesystem_name, O_RDONLY, 0);
1b6bf175 596 if (fd < 0) {
0c4a0726
TT
597 com_err("open", errno,
598 _("while opening %s for flushing"),
1b6bf175 599 ctx->filesystem_name);
243dc31f 600 fatal_error(ctx, 0);
1b6bf175 601 }
5df55d7f 602 if ((retval = ext2fs_sync_device(fd, 1))) {
5ba23cb1 603 com_err("ext2fs_sync_device", retval,
0c4a0726 604 _("while trying to flush %s"),
1b6bf175 605 ctx->filesystem_name);
243dc31f 606 fatal_error(ctx, 0);
1b6bf175
TT
607 }
608 close(fd);
1b6bf175 609 }
5df55d7f 610#ifdef ENABLE_SWAPFS
1b6bf175
TT
611 if (swapfs) {
612 if (cflag || bad_blocks_file) {
0c4a0726
TT
613 fprintf(stderr, _("Incompatible options not "
614 "allowed when byte-swapping.\n"));
243dc31f 615 exit(FSCK_USAGE);
1b6bf175
TT
616 }
617 }
5df55d7f 618#endif
9ecd8bec 619#ifdef HAVE_SIGNAL_H
5596defa
TT
620 /*
621 * Set up signal action
622 */
623 memset(&sa, 0, sizeof(struct sigaction));
624#ifdef SA_RESTART
625 sa.sa_flags = SA_RESTART;
626#endif
243dc31f 627 e2fsck_global_ctx = ctx;
5596defa
TT
628 sa.sa_handler = signal_progress_on;
629 sigaction(SIGUSR1, &sa, 0);
630 sa.sa_handler = signal_progress_off;
631 sigaction(SIGUSR2, &sa, 0);
9ecd8bec 632#endif
6d222f32
TT
633
634 /* Update our PATH to include /sbin if we need to run badblocks */
635 if (cflag) {
636 char *oldpath = getenv("PATH");
637 if (oldpath) {
638 char *newpath;
639
640 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
641 strlen (oldpath));
642 if (!newpath)
643 fatal_error(ctx, "Couldn't malloc() newpath");
644 strcpy (newpath, PATH_SET);
645 strcat (newpath, ":");
646 strcat (newpath, oldpath);
647 putenv (newpath);
648 } else
649 putenv (PATH_SET);
650 }
1b6bf175
TT
651 return 0;
652}
653
654static const char *my_ver_string = E2FSPROGS_VERSION;
655static const char *my_ver_date = E2FSPROGS_DATE;
656
657int main (int argc, char *argv[])
658{
659 errcode_t retval = 0;
660 int exit_value = FSCK_OK;
661 int i;
662 ext2_filsys fs = 0;
663 io_manager io_ptr;
5dd8f963 664 struct ext2_super_block *sb;
1b6bf175
TT
665 const char *lib_ver_date;
666 int my_ver, lib_ver;
667 e2fsck_t ctx;
668 struct problem_context pctx;
08b21301 669 int flags, run_result;
1b6bf175
TT
670
671 clear_problem_context(&pctx);
672#ifdef MTRACE
673 mtrace();
674#endif
675#ifdef MCHECK
676 mcheck(0);
0c4a0726
TT
677#endif
678#ifdef ENABLE_NLS
679 setlocale(LC_MESSAGES, "");
680 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
681 textdomain(NLS_CAT_NAME);
1b6bf175
TT
682#endif
683 my_ver = ext2fs_parse_version_string(my_ver_string);
684 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
685 if (my_ver > lib_ver) {
0c4a0726
TT
686 fprintf( stderr, _("Error: ext2fs library version "
687 "out of date!\n"));
1b6bf175
TT
688 show_version_only++;
689 }
690
691 retval = PRS(argc, argv, &ctx);
692 if (retval) {
693 com_err("e2fsck", retval,
0c4a0726 694 _("while trying to initialize program"));
243dc31f 695 exit(FSCK_ERROR);
1b6bf175 696 }
24fc5032
TT
697 reserve_stdio_fds();
698
8bf191e8 699#ifdef RESOURCE_TRACK
1b6bf175 700 init_resource_track(&ctx->global_rtrack);
8bf191e8 701#endif
1b6bf175
TT
702
703 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
0f8973fb
TT
704 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string,
705 my_ver_date);
1b6bf175
TT
706
707 if (show_version_only) {
0c4a0726 708 fprintf(stderr, _("\tUsing %s, %s\n"),
1b6bf175 709 error_message(EXT2_ET_BASE), lib_ver_date);
243dc31f 710 exit(FSCK_OK);
1b6bf175
TT
711 }
712
713 check_mount(ctx);
714
715 if (!(ctx->options & E2F_OPT_PREEN) &&
716 !(ctx->options & E2F_OPT_NO) &&
717 !(ctx->options & E2F_OPT_YES)) {
718 if (!isatty (0) || !isatty (1))
f8188fff 719 fatal_error(ctx,
0c4a0726 720 _("need terminal for interactive repairs"));
1b6bf175
TT
721 }
722 ctx->superblock = ctx->use_superblock;
723restart:
724#if 1
725 io_ptr = unix_io_manager;
726#else
727 io_ptr = test_io_manager;
728 test_io_backing_manager = unix_io_manager;
729#endif
17390c04
TT
730 flags = 0;
731 if ((ctx->options & E2F_OPT_READONLY) == 0)
732 flags |= EXT2_FLAG_RW;
733
1b6bf175
TT
734 if (ctx->superblock && blocksize) {
735 retval = ext2fs_open(ctx->filesystem_name, flags,
736 ctx->superblock, blocksize, io_ptr, &fs);
737 } else if (ctx->superblock) {
738 for (i=0; possible_block_sizes[i]; i++) {
739 retval = ext2fs_open(ctx->filesystem_name, flags,
740 ctx->superblock,
741 possible_block_sizes[i],
742 io_ptr, &fs);
743 if (!retval)
744 break;
745 }
746 } else
747 retval = ext2fs_open(ctx->filesystem_name, flags,
748 0, 0, io_ptr, &fs);
ae6cdcf7
TT
749 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
750 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1b6bf175
TT
751 ((retval == EXT2_ET_BAD_MAGIC) ||
752 ((retval == 0) && ext2fs_check_desc(fs)))) {
753 if (!fs || (fs->group_desc_count > 1)) {
0c4a0726
TT
754 printf(_("%s trying backup blocks...\n"),
755 retval ? _("Couldn't find ext2 superblock,") :
756 _("Group descriptors look bad..."));
1b6bf175
TT
757 ctx->superblock = get_backup_sb(fs);
758 if (fs)
759 ext2fs_close(fs);
760 goto restart;
761 }
762 }
763 if (retval) {
0c4a0726 764 com_err(ctx->program_name, retval, _("while trying to open %s"),
1b6bf175 765 ctx->filesystem_name);
24dd4028 766 if (retval == EXT2_ET_REV_TOO_HIGH) {
0c4a0726 767 printf(_("The filesystem revision is apparently "
24dd4028
TT
768 "too high for this version of e2fsck.\n"
769 "(Or the filesystem superblock "
0c4a0726 770 "is corrupt)\n\n"));
24dd4028
TT
771 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
772 } else if (retval == EXT2_ET_SHORT_READ)
0c4a0726 773 printf(_("Could this be a zero-length partition?\n"));
1b6bf175 774 else if ((retval == EPERM) || (retval == EACCES))
0c4a0726
TT
775 printf(_("You must have %s access to the "
776 "filesystem or be root\n"),
1b6bf175
TT
777 (ctx->options & E2F_OPT_READONLY) ?
778 "r/o" : "r/w");
779 else if (retval == ENXIO)
0c4a0726 780 printf(_("Possibly non-existent or swap device?\n"));
68227542
TT
781#ifdef EROFS
782 else if (retval == EROFS)
0c4a0726 783 printf(_("Disk write-protected; use the -n option "
68227542 784 "to do a read-only\n"
0c4a0726 785 "check of the device.\n"));
68227542 786#endif
1b6bf175
TT
787 else
788 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
243dc31f 789 fatal_error(ctx, 0);
1b6bf175
TT
790 }
791 ctx->fs = fs;
54dc7ca2 792 fs->priv_data = ctx;
5dd8f963
TT
793 sb = fs->super;
794 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1b6bf175 795 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
0c4a0726 796 _("while trying to open %s"),
1b6bf175 797 ctx->filesystem_name);
f8188fff 798 get_newer:
0c4a0726 799 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1b6bf175 800 }
3b5386dc
TT
801
802 /*
803 * Set the device name, which is used whenever we print error
804 * or informational messages to the user.
805 */
806 if (ctx->device_name == 0 &&
5dd8f963
TT
807 (sb->s_volume_name[0] != 0)) {
808 char *cp = malloc(sizeof(sb->s_volume_name)+1);
3b5386dc 809 if (cp) {
5dd8f963
TT
810 strncpy(cp, sb->s_volume_name,
811 sizeof(sb->s_volume_name));
812 cp[sizeof(sb->s_volume_name)] = 0;
3b5386dc
TT
813 ctx->device_name = cp;
814 }
815 }
816 if (ctx->device_name == 0)
817 ctx->device_name = ctx->filesystem_name;
818
17390c04 819 /*
9b565759 820 * Make sure the ext3 superblock fields are consistent.
17390c04 821 */
3b5386dc
TT
822 retval = e2fsck_check_ext3_journal(ctx);
823 if (retval) {
824 com_err(ctx->program_name, retval,
825 _("while checking ext3 journal for %s"),
826 ctx->device_name);
827 ext2fs_close(ctx->fs);
243dc31f 828 fatal_error(ctx, 0);
3b5386dc
TT
829 }
830
9b565759
TT
831 /*
832 * Check to see if we need to do ext3-style recovery. If so,
833 * do it, and then restart the fsck.
834 */
5dd8f963 835 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
2575fb04
TT
836 if (ctx->options & E2F_OPT_READONLY) {
837 printf(_("Warning: skipping journal recovery "
838 "because doing a read-only filesystem "
839 "check.\n"));
840 io_channel_flush(ctx->fs->io);
841 } else {
842 retval = e2fsck_run_ext3_journal(ctx);
843 if (retval) {
844 com_err(ctx->program_name, retval,
3b5386dc 845 _("while recovering ext3 journal of %s"),
2575fb04
TT
846 ctx->device_name);
847 fatal_error(ctx, 0);
848 }
849 ext2fs_close(ctx->fs);
850 ctx->fs = 0;
851 goto restart;
17390c04 852 }
17390c04 853 }
3b5386dc 854
1b6bf175
TT
855 /*
856 * Check for compatibility with the feature sets. We need to
857 * be more stringent than ext2fs_open().
858 */
5dd8f963
TT
859 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
860 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1b6bf175 861 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
3b5386dc 862 "(%s)", ctx->device_name);
f8188fff 863 goto get_newer;
1b6bf175 864 }
5dd8f963 865 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1b6bf175 866 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
3b5386dc 867 "(%s)", ctx->device_name);
1b6bf175
TT
868 goto get_newer;
869 }
1917875f 870#ifdef ENABLE_COMPRESSION
5dd8f963 871 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1917875f
TT
872 com_err(ctx->program_name, 0,
873 _("Warning: compression support is experimental.\n"));
874#endif
1b6bf175
TT
875
876 /*
877 * If the user specified a specific superblock, presumably the
878 * master superblock has been trashed. So we mark the
879 * superblock as dirty, so it can be written out.
880 */
881 if (ctx->superblock &&
882 !(ctx->options & E2F_OPT_READONLY))
883 ext2fs_mark_super_dirty(fs);
884
885 /*
886 * Don't overwrite the backup superblock and block
887 * descriptors, until we're sure the filesystem is OK....
888 */
889 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
890
891 ehandler_init(fs->io);
892
893 if (ctx->superblock)
894 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
895 check_super_block(ctx);
a02ce9df 896 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 897 fatal_error(ctx, 0);
1b6bf175
TT
898 check_if_skip(ctx);
899 if (bad_blocks_file)
900 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
901 else if (cflag)
902 test_disk(ctx);
a02ce9df 903 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 904 fatal_error(ctx, 0);
5df55d7f 905#ifdef ENABLE_SWAPFS
1b6bf175
TT
906 if (normalize_swapfs) {
907 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
908 ext2fs_native_flag()) {
0c4a0726
TT
909 fprintf(stderr, _("%s: Filesystem byte order "
910 "already normalized.\n"), ctx->device_name);
243dc31f 911 fatal_error(ctx, 0);
1b6bf175
TT
912 }
913 }
f8188fff 914 if (swapfs) {
1b6bf175 915 swap_filesys(ctx);
a02ce9df 916 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 917 fatal_error(ctx, 0);
f8188fff 918 }
5df55d7f 919#endif
1b6bf175
TT
920
921 /*
922 * Mark the system as valid, 'til proven otherwise
923 */
924 ext2fs_mark_valid(fs);
925
926 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
927 if (retval) {
928 com_err(ctx->program_name, retval,
0c4a0726 929 _("while reading bad blocks inode"));
1b6bf175 930 preenhalt(ctx);
0c4a0726
TT
931 printf(_("This doesn't bode well,"
932 " but we'll try to go on...\n"));
1b6bf175 933 }
08b21301
TT
934
935 run_result = e2fsck_run(ctx);
5596defa 936 e2fsck_clear_progbar(ctx);
08b21301 937 if (run_result == E2F_FLAG_RESTART) {
0c4a0726 938 printf(_("Restarting e2fsck from the beginning...\n"));
1b6bf175
TT
939 retval = e2fsck_reset_context(ctx);
940 if (retval) {
941 com_err(ctx->program_name, retval,
0c4a0726 942 _("while resetting context"));
243dc31f 943 fatal_error(ctx, 0);
1b6bf175 944 }
73f17cfc 945 ext2fs_close(fs);
1b6bf175
TT
946 goto restart;
947 }
a02ce9df 948 if (run_result & E2F_FLAG_SIGNAL_MASK)
243dc31f 949 fatal_error(ctx, 0);
08b21301
TT
950 if (run_result & E2F_FLAG_CANCEL)
951 ext2fs_unmark_valid(fs);
1b6bf175
TT
952
953#ifdef MTRACE
954 mtrace_print("Cleanup");
955#endif
956 if (ext2fs_test_changed(fs)) {
957 exit_value = FSCK_NONDESTRUCT;
958 if (!(ctx->options & E2F_OPT_PREEN))
0c4a0726 959 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1b6bf175
TT
960 ctx->device_name);
961 if (root_filesystem && !read_only_root) {
0c4a0726 962 printf(_("%s: ***** REBOOT LINUX *****\n"),
1b6bf175
TT
963 ctx->device_name);
964 exit_value = FSCK_REBOOT;
965 }
966 }
967 if (ext2fs_test_valid(fs))
968 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
d3124019
TT
969 else {
970 printf(_("\n%s: ********** WARNING: Filesystem still has "
971 "errors **********\n\n"), ctx->device_name);
1b6bf175 972 exit_value = FSCK_UNCORRECTED;
d3124019 973 }
1b6bf175
TT
974 if (!(ctx->options & E2F_OPT_READONLY)) {
975 if (ext2fs_test_valid(fs)) {
5dd8f963 976 if (!(sb->s_state & EXT2_VALID_FS))
1b6bf175 977 exit_value = FSCK_NONDESTRUCT;
5dd8f963 978 sb->s_state = EXT2_VALID_FS;
1b6bf175 979 } else
5dd8f963
TT
980 sb->s_state &= ~EXT2_VALID_FS;
981 sb->s_mnt_count = 0;
982 sb->s_lastcheck = time(NULL);
1b6bf175
TT
983 ext2fs_mark_super_dirty(fs);
984 }
985 show_stats(ctx);
986
f8188fff 987 e2fsck_write_bitmaps(ctx);
1b6bf175 988
0628ae39
TT
989 ext2fs_close(fs);
990 ctx->fs = NULL;
991 e2fsck_free_context(ctx);
992
8bf191e8 993#ifdef RESOURCE_TRACK
1b6bf175
TT
994 if (ctx->options & E2F_OPT_TIME)
995 print_resource_track(NULL, &ctx->global_rtrack);
8bf191e8 996#endif
1b6bf175 997
1b6bf175
TT
998 return exit_value;
999}