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