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