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