]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/unix.c
Makefile.in (uuidgen): Avoid linking lsattr and chattr against
[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" : "");
2b94c658
TT
172 printf(_("%8d large file%s\n"), ctx->large_files,
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
b0700a1b
TT
336int e2fsck_simple_progress(e2fsck_t ctx, char *label, float percent,
337 unsigned int dpynum)
338{
339 static const char spinner[] = "\\|/-";
340 char buf[80];
341 int i;
342 int tick;
343 struct timeval tv;
344 int dpywidth;
345
346 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
347 return 0;
348
349 /*
350 * Calculate the new progress position. If the
351 * percentage hasn't changed, then we skip out right
352 * away.
353 */
354 if (ctx->progress_last_percent == (int) 10 * percent)
355 return 0;
356 ctx->progress_last_percent = (int) 10 * percent;
357
358 /*
359 * If we've already updated the spinner once within
360 * the last 1/8th of a second, no point doing it
361 * again.
362 */
363 gettimeofday(&tv, NULL);
364 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
365 if ((tick == ctx->progress_last_time) &&
366 (percent != 0.0) && (percent != 100.0))
367 return 0;
368 ctx->progress_last_time = tick;
369
370 /*
371 * Advance the spinner, and note that the progress bar
372 * will be on the screen
373 */
374 ctx->progress_pos = (ctx->progress_pos+1) & 3;
375 ctx->flags |= E2F_FLAG_PROG_BAR;
376
377 dpywidth = 66 - strlen(label);
378 dpywidth = 8 * (dpywidth / 8);
379 if (dpynum)
380 dpywidth -= 8;
381
382 i = ((percent * dpywidth) + 50) / 100;
bc34d6be 383 printf("\001%s: |%s%s", label, bar + (sizeof(bar) - (i+1)),
b0700a1b
TT
384 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
385 if (percent == 100.0)
386 fputc('|', stdout);
387 else
388 fputc(spinner[ctx->progress_pos & 3], stdout);
389 if (dpynum)
bc34d6be 390 printf(" %4.1f%% %u\r\002", percent, dpynum);
b0700a1b 391 else
bc34d6be 392 printf(" %4.1f%% \r\002", percent);
b0700a1b
TT
393
394 if (percent == 100.0)
395 e2fsck_clear_progbar(ctx);
396 fflush(stdout);
397
398 return 0;
399}
400
efac9a1b
TT
401static int e2fsck_update_progress(e2fsck_t ctx, int pass,
402 unsigned long cur, unsigned long max)
403{
53ef44c4 404 static const char spinner[] = "\\|/-";
efac9a1b 405 char buf[80];
5596defa
TT
406 int i;
407 float percent;
06012323
TT
408 int tick;
409 struct timeval tv;
17390c04 410 static int dpywidth = 0;
f75c28de
TT
411
412 if (pass == 0)
413 return 0;
efac9a1b
TT
414
415 if (ctx->progress_fd) {
416 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
417 write(ctx->progress_fd, buf, strlen(buf));
418 } else {
e4c8e885 419 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
b0700a1b
TT
420 e2fsck_simple_progress(ctx, ctx->device_name,
421 percent, 0);
efac9a1b
TT
422 }
423 return 0;
424}
1b6bf175
TT
425
426#define PATH_SET "PATH=/sbin"
427
5ba23cb1 428static void reserve_stdio_fds(void)
24fc5032
TT
429{
430 int fd;
431
432 while (1) {
433 fd = open("/dev/null", O_RDWR);
434 if (fd > 2)
435 break;
75d83bec 436 if (fd < 0) {
0c4a0726
TT
437 fprintf(stderr, _("ERROR: Couldn't open "
438 "/dev/null (%s)\n"),
75d83bec
TT
439 strerror(errno));
440 break;
441 }
24fc5032
TT
442 }
443 close(fd);
444}
445
9ecd8bec 446#ifdef HAVE_SIGNAL_H
5596defa
TT
447static void signal_progress_on(int sig)
448{
243dc31f 449 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
450
451 if (!ctx)
452 return;
453
454 ctx->progress = e2fsck_update_progress;
455 ctx->progress_fd = 0;
456}
457
458static void signal_progress_off(int sig)
459{
243dc31f 460 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
461
462 if (!ctx)
463 return;
464
465 e2fsck_clear_progbar(ctx);
466 ctx->progress = 0;
467}
4cae0452
TT
468
469static void signal_cancel(int sig)
470{
471 e2fsck_t ctx = e2fsck_global_ctx;
472
473 if (!ctx)
474 exit(FSCK_CANCELED);
475
476 ctx->flags |= E2F_FLAG_CANCEL;
477}
9ecd8bec 478#endif
5596defa 479
0684a4f3
TT
480static void parse_extended_opts(e2fsck_t ctx, const char *opts)
481{
482 char *buf, *token, *next, *p, *arg;
f364093b 483 int ea_ver;
0684a4f3
TT
484 int extended_usage = 0;
485
f364093b 486 buf = string_copy(ctx, opts, 0);
0684a4f3
TT
487 for (token = buf; token && *token; token = next) {
488 p = strchr(token, ',');
489 next = 0;
490 if (p) {
491 *p = 0;
492 next = p+1;
493 }
494 arg = strchr(token, '=');
495 if (arg) {
496 *arg = 0;
497 arg++;
498 }
499 if (strcmp(token, "ea_ver") == 0) {
500 if (!arg) {
501 extended_usage++;
502 continue;
503 }
504 ea_ver = strtoul(arg, &p, 0);
505 if (*p ||
506 ((ea_ver != 1) && (ea_ver != 2))) {
507 fprintf(stderr,
508 _("Invalid EA version.\n"));
509 extended_usage++;
510 continue;
511 }
512 ctx->ext_attr_ver = ea_ver;
513 } else
514 extended_usage++;
515 }
516 if (extended_usage) {
517 fprintf(stderr, _("Extended options are separated by commas, "
518 "and may take an argument which\n"
519 "is set off by an equals ('=') sign. "
520 "Valid raid options are:\n"
521 "\tea_ver=<ea_version (1 or 2)\n\n"));
522 exit(1);
523 }
524}
525
526
1b6bf175
TT
527static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
528{
529 int flush = 0;
ae8160e6 530 int c, fd;
1b6bf175
TT
531#ifdef MTRACE
532 extern void *mallwatch;
533#endif
1b6bf175
TT
534 e2fsck_t ctx;
535 errcode_t retval;
9ecd8bec 536#ifdef HAVE_SIGNAL_H
5596defa 537 struct sigaction sa;
9ecd8bec 538#endif
0684a4f3 539 char *extended_opts = 0;
1b6bf175
TT
540
541 retval = e2fsck_allocate_context(&ctx);
542 if (retval)
543 return retval;
544
545 *ret_ctx = ctx;
546
908b785c
TT
547 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
548 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1b6bf175 549 initialize_ext2_error_table();
f364093b 550 blkid_get_cache(&ctx->blkid, NULL);
1b6bf175
TT
551
552 if (argc && *argv)
553 ctx->program_name = *argv;
554 else
555 ctx->program_name = "e2fsck";
0684a4f3 556 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsD")) != EOF)
1b6bf175 557 switch (c) {
efac9a1b
TT
558 case 'C':
559 ctx->progress = e2fsck_update_progress;
560 ctx->progress_fd = atoi(optarg);
fc9a69ca
TT
561 if (!ctx->progress_fd)
562 break;
ae8160e6
TT
563 /* Validate the file descriptor to avoid disasters */
564 fd = dup(ctx->progress_fd);
565 if (fd < 0) {
566 fprintf(stderr,
567 _("Error validating file descriptor %d: %s\n"),
568 ctx->progress_fd,
569 error_message(errno));
570 fatal_error(ctx,
571 _("Invalid completion information file descriptor"));
572 } else
573 close(fd);
efac9a1b 574 break;
850d05e9
TT
575 case 'D':
576 ctx->options |= E2F_OPT_COMPRESS_DIRS;
577 break;
0684a4f3
TT
578 case 'E':
579 extended_opts = optarg;
580 break;
1b6bf175
TT
581 case 'p':
582 case 'a':
8161a748
TT
583 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
584 conflict_opt:
585 fatal_error(ctx,
586 _("Only one the options -p/-a, -n or -y may be specified."));
587 }
1b6bf175 588 ctx->options |= E2F_OPT_PREEN;
1b6bf175
TT
589 break;
590 case 'n':
8161a748
TT
591 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
592 goto conflict_opt;
1b6bf175 593 ctx->options |= E2F_OPT_NO;
1b6bf175
TT
594 break;
595 case 'y':
8161a748
TT
596 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
597 goto conflict_opt;
1b6bf175 598 ctx->options |= E2F_OPT_YES;
1b6bf175
TT
599 break;
600 case 't':
8bf191e8 601#ifdef RESOURCE_TRACK
1b6bf175
TT
602 if (ctx->options & E2F_OPT_TIME)
603 ctx->options |= E2F_OPT_TIME2;
604 else
605 ctx->options |= E2F_OPT_TIME;
8bf191e8 606#else
0c4a0726
TT
607 fprintf(stderr, _("The -t option is not "
608 "supported on this version of e2fsck.\n"));
8bf191e8 609#endif
1b6bf175
TT
610 break;
611 case 'c':
3ed57c27
TT
612 if (cflag++)
613 ctx->options |= E2F_OPT_WRITECHECK;
1b6bf175
TT
614 ctx->options |= E2F_OPT_CHECKBLOCKS;
615 break;
616 case 'r':
617 /* What we do by default, anyway! */
618 break;
619 case 'b':
620 ctx->use_superblock = atoi(optarg);
ae6cdcf7 621 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
1b6bf175
TT
622 break;
623 case 'B':
f1a1761d 624 ctx->blocksize = atoi(optarg);
1b6bf175
TT
625 break;
626 case 'I':
627 ctx->inode_buffer_blocks = atoi(optarg);
628 break;
adee8d75 629 case 'j':
f364093b 630 ctx->journal_name = string_copy(ctx, optarg, 0);
adee8d75 631 break;
1b6bf175
TT
632 case 'P':
633 ctx->process_inode_size = atoi(optarg);
634 break;
635 case 'L':
636 replace_bad_blocks++;
637 case 'l':
f364093b 638 bad_blocks_file = string_copy(ctx, optarg, 0);
1b6bf175
TT
639 break;
640 case 'd':
641 ctx->options |= E2F_OPT_DEBUG;
642 break;
643 case 'f':
d37066a9 644 ctx->options |= E2F_OPT_FORCE;
1b6bf175
TT
645 break;
646 case 'F':
1b6bf175 647 flush = 1;
1b6bf175
TT
648 break;
649 case 'v':
650 verbose = 1;
651 break;
652 case 'V':
653 show_version_only = 1;
654 break;
655#ifdef MTRACE
656 case 'M':
657 mallwatch = (void *) strtol(optarg, NULL, 0);
658 break;
659#endif
660 case 'N':
661 ctx->device_name = optarg;
662 break;
5df55d7f 663#ifdef ENABLE_SWAPFS
1b6bf175
TT
664 case 's':
665 normalize_swapfs = 1;
666 case 'S':
667 swapfs = 1;
668 break;
5df55d7f
TT
669#else
670 case 's':
671 case 'S':
672 fprintf(stderr, _("Byte-swapping filesystems "
673 "not compiled in this version "
674 "of e2fsck\n"));
675 exit(1);
676#endif
1b6bf175
TT
677 default:
678 usage(ctx);
679 }
680 if (show_version_only)
681 return 0;
682 if (optind != argc - 1)
683 usage(ctx);
684 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
850d05e9 685 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
1b6bf175 686 ctx->options |= E2F_OPT_READONLY;
f364093b 687 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
0684a4f3
TT
688 if (extended_opts)
689 parse_extended_opts(ctx, extended_opts);
690
28ffafb0 691 if (flush) {
4ea7bd04 692 fd = open(ctx->filesystem_name, O_RDONLY, 0);
1b6bf175 693 if (fd < 0) {
0c4a0726
TT
694 com_err("open", errno,
695 _("while opening %s for flushing"),
1b6bf175 696 ctx->filesystem_name);
243dc31f 697 fatal_error(ctx, 0);
1b6bf175 698 }
5df55d7f 699 if ((retval = ext2fs_sync_device(fd, 1))) {
5ba23cb1 700 com_err("ext2fs_sync_device", retval,
0c4a0726 701 _("while trying to flush %s"),
1b6bf175 702 ctx->filesystem_name);
243dc31f 703 fatal_error(ctx, 0);
1b6bf175
TT
704 }
705 close(fd);
1b6bf175 706 }
5df55d7f 707#ifdef ENABLE_SWAPFS
1b6bf175
TT
708 if (swapfs) {
709 if (cflag || bad_blocks_file) {
0c4a0726 710 fprintf(stderr, _("Incompatible options not "
3ed57c27 711 "allowed when byte-swapping.\n"));
243dc31f 712 exit(FSCK_USAGE);
1b6bf175
TT
713 }
714 }
5df55d7f 715#endif
3ed57c27
TT
716 if (cflag && bad_blocks_file) {
717 fprintf(stderr, _("The -c and the -l/-L options may "
718 "not be both used at the same time.\n"));
719 exit(FSCK_USAGE);
720 }
9ecd8bec 721#ifdef HAVE_SIGNAL_H
5596defa
TT
722 /*
723 * Set up signal action
724 */
725 memset(&sa, 0, sizeof(struct sigaction));
850d05e9
TT
726 sa.sa_handler = signal_cancel;
727 sigaction(SIGINT, &sa, 0);
728 sigaction(SIGTERM, &sa, 0);
5596defa
TT
729#ifdef SA_RESTART
730 sa.sa_flags = SA_RESTART;
731#endif
243dc31f 732 e2fsck_global_ctx = ctx;
5596defa
TT
733 sa.sa_handler = signal_progress_on;
734 sigaction(SIGUSR1, &sa, 0);
735 sa.sa_handler = signal_progress_off;
736 sigaction(SIGUSR2, &sa, 0);
9ecd8bec 737#endif
6d222f32
TT
738
739 /* Update our PATH to include /sbin if we need to run badblocks */
740 if (cflag) {
741 char *oldpath = getenv("PATH");
742 if (oldpath) {
743 char *newpath;
744
745 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
746 strlen (oldpath));
747 if (!newpath)
748 fatal_error(ctx, "Couldn't malloc() newpath");
749 strcpy (newpath, PATH_SET);
750 strcat (newpath, ":");
751 strcat (newpath, oldpath);
752 putenv (newpath);
753 } else
754 putenv (PATH_SET);
755 }
5e72cdbe
TT
756#ifdef CONFIG_JBD_DEBUG
757 if (getenv("E2FSCK_JBD_DEBUG"))
758 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
759#endif
1b6bf175
TT
760 return 0;
761}
762
763static const char *my_ver_string = E2FSPROGS_VERSION;
764static const char *my_ver_date = E2FSPROGS_DATE;
765
766int main (int argc, char *argv[])
767{
768 errcode_t retval = 0;
769 int exit_value = FSCK_OK;
1b6bf175
TT
770 ext2_filsys fs = 0;
771 io_manager io_ptr;
5dd8f963 772 struct ext2_super_block *sb;
1b6bf175
TT
773 const char *lib_ver_date;
774 int my_ver, lib_ver;
775 e2fsck_t ctx;
776 struct problem_context pctx;
08b21301 777 int flags, run_result;
1b6bf175
TT
778
779 clear_problem_context(&pctx);
780#ifdef MTRACE
781 mtrace();
782#endif
783#ifdef MCHECK
784 mcheck(0);
0c4a0726
TT
785#endif
786#ifdef ENABLE_NLS
787 setlocale(LC_MESSAGES, "");
14308a53 788 setlocale(LC_CTYPE, "");
0c4a0726
TT
789 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
790 textdomain(NLS_CAT_NAME);
1b6bf175
TT
791#endif
792 my_ver = ext2fs_parse_version_string(my_ver_string);
793 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
794 if (my_ver > lib_ver) {
0c4a0726
TT
795 fprintf( stderr, _("Error: ext2fs library version "
796 "out of date!\n"));
1b6bf175
TT
797 show_version_only++;
798 }
799
800 retval = PRS(argc, argv, &ctx);
801 if (retval) {
802 com_err("e2fsck", retval,
0c4a0726 803 _("while trying to initialize program"));
243dc31f 804 exit(FSCK_ERROR);
1b6bf175 805 }
24fc5032
TT
806 reserve_stdio_fds();
807
8bf191e8 808#ifdef RESOURCE_TRACK
1b6bf175 809 init_resource_track(&ctx->global_rtrack);
8bf191e8 810#endif
1b6bf175
TT
811
812 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
908b785c 813 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
0f8973fb 814 my_ver_date);
1b6bf175
TT
815
816 if (show_version_only) {
0c4a0726 817 fprintf(stderr, _("\tUsing %s, %s\n"),
1b6bf175 818 error_message(EXT2_ET_BASE), lib_ver_date);
243dc31f 819 exit(FSCK_OK);
1b6bf175
TT
820 }
821
822 check_mount(ctx);
823
824 if (!(ctx->options & E2F_OPT_PREEN) &&
825 !(ctx->options & E2F_OPT_NO) &&
826 !(ctx->options & E2F_OPT_YES)) {
827 if (!isatty (0) || !isatty (1))
f8188fff 828 fatal_error(ctx,
0c4a0726 829 _("need terminal for interactive repairs"));
1b6bf175
TT
830 }
831 ctx->superblock = ctx->use_superblock;
832restart:
833#if 1
834 io_ptr = unix_io_manager;
835#else
836 io_ptr = test_io_manager;
837 test_io_backing_manager = unix_io_manager;
838#endif
17390c04
TT
839 flags = 0;
840 if ((ctx->options & E2F_OPT_READONLY) == 0)
841 flags |= EXT2_FLAG_RW;
842
f1a1761d 843 if (ctx->superblock && ctx->blocksize) {
1b6bf175 844 retval = ext2fs_open(ctx->filesystem_name, flags,
f1a1761d
TT
845 ctx->superblock, ctx->blocksize,
846 io_ptr, &fs);
1b6bf175 847 } else if (ctx->superblock) {
932a489c
AD
848 int blocksize;
849 for (blocksize = EXT2_MIN_BLOCK_SIZE;
850 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
1b6bf175 851 retval = ext2fs_open(ctx->filesystem_name, flags,
932a489c 852 ctx->superblock, blocksize,
1b6bf175
TT
853 io_ptr, &fs);
854 if (!retval)
855 break;
856 }
857 } else
858 retval = ext2fs_open(ctx->filesystem_name, flags,
859 0, 0, io_ptr, &fs);
ae6cdcf7
TT
860 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
861 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1b6bf175
TT
862 ((retval == EXT2_ET_BAD_MAGIC) ||
863 ((retval == 0) && ext2fs_check_desc(fs)))) {
864 if (!fs || (fs->group_desc_count > 1)) {
0c4a0726
TT
865 printf(_("%s trying backup blocks...\n"),
866 retval ? _("Couldn't find ext2 superblock,") :
867 _("Group descriptors look bad..."));
f1a1761d 868 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
1b6bf175
TT
869 if (fs)
870 ext2fs_close(fs);
871 goto restart;
872 }
873 }
874 if (retval) {
0c4a0726 875 com_err(ctx->program_name, retval, _("while trying to open %s"),
1b6bf175 876 ctx->filesystem_name);
24dd4028 877 if (retval == EXT2_ET_REV_TOO_HIGH) {
0c4a0726 878 printf(_("The filesystem revision is apparently "
24dd4028
TT
879 "too high for this version of e2fsck.\n"
880 "(Or the filesystem superblock "
0c4a0726 881 "is corrupt)\n\n"));
24dd4028
TT
882 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
883 } else if (retval == EXT2_ET_SHORT_READ)
0c4a0726 884 printf(_("Could this be a zero-length partition?\n"));
1b6bf175 885 else if ((retval == EPERM) || (retval == EACCES))
0c4a0726
TT
886 printf(_("You must have %s access to the "
887 "filesystem or be root\n"),
1b6bf175
TT
888 (ctx->options & E2F_OPT_READONLY) ?
889 "r/o" : "r/w");
890 else if (retval == ENXIO)
0c4a0726 891 printf(_("Possibly non-existent or swap device?\n"));
68227542
TT
892#ifdef EROFS
893 else if (retval == EROFS)
0c4a0726 894 printf(_("Disk write-protected; use the -n option "
68227542 895 "to do a read-only\n"
0c4a0726 896 "check of the device.\n"));
68227542 897#endif
1b6bf175
TT
898 else
899 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
243dc31f 900 fatal_error(ctx, 0);
1b6bf175
TT
901 }
902 ctx->fs = fs;
54dc7ca2 903 fs->priv_data = ctx;
5dd8f963
TT
904 sb = fs->super;
905 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1b6bf175 906 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
0c4a0726 907 _("while trying to open %s"),
1b6bf175 908 ctx->filesystem_name);
f8188fff 909 get_newer:
0c4a0726 910 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1b6bf175 911 }
3b5386dc
TT
912
913 /*
914 * Set the device name, which is used whenever we print error
915 * or informational messages to the user.
916 */
917 if (ctx->device_name == 0 &&
5dd8f963 918 (sb->s_volume_name[0] != 0)) {
f364093b
TT
919 ctx->device_name = string_copy(ctx, sb->s_volume_name,
920 sizeof(sb->s_volume_name));
3b5386dc
TT
921 }
922 if (ctx->device_name == 0)
923 ctx->device_name = ctx->filesystem_name;
924
17390c04 925 /*
9b565759 926 * Make sure the ext3 superblock fields are consistent.
17390c04 927 */
3b5386dc
TT
928 retval = e2fsck_check_ext3_journal(ctx);
929 if (retval) {
930 com_err(ctx->program_name, retval,
931 _("while checking ext3 journal for %s"),
932 ctx->device_name);
243dc31f 933 fatal_error(ctx, 0);
3b5386dc
TT
934 }
935
9b565759
TT
936 /*
937 * Check to see if we need to do ext3-style recovery. If so,
938 * do it, and then restart the fsck.
939 */
5dd8f963 940 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
2575fb04
TT
941 if (ctx->options & E2F_OPT_READONLY) {
942 printf(_("Warning: skipping journal recovery "
943 "because doing a read-only filesystem "
944 "check.\n"));
945 io_channel_flush(ctx->fs->io);
946 } else {
b92ae153
TT
947 if (ctx->flags & E2F_FLAG_RESTARTED) {
948 /*
949 * Whoops, we attempted to run the
950 * journal twice. This should never
951 * happen, unless the hardware or
952 * device driver is being bogus.
953 */
954 com_err(ctx->program_name, 0,
955 _("unable to set superblock flags on %s\n"), ctx->device_name);
956 fatal_error(ctx, 0);
957 }
2575fb04
TT
958 retval = e2fsck_run_ext3_journal(ctx);
959 if (retval) {
960 com_err(ctx->program_name, retval,
3b5386dc 961 _("while recovering ext3 journal of %s"),
2575fb04
TT
962 ctx->device_name);
963 fatal_error(ctx, 0);
964 }
965 ext2fs_close(ctx->fs);
966 ctx->fs = 0;
b92ae153 967 ctx->flags |= E2F_FLAG_RESTARTED;
2575fb04 968 goto restart;
17390c04 969 }
17390c04 970 }
3b5386dc 971
1b6bf175
TT
972 /*
973 * Check for compatibility with the feature sets. We need to
974 * be more stringent than ext2fs_open().
975 */
5dd8f963
TT
976 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
977 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1b6bf175 978 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
3b5386dc 979 "(%s)", ctx->device_name);
f8188fff 980 goto get_newer;
1b6bf175 981 }
5dd8f963 982 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1b6bf175 983 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
3b5386dc 984 "(%s)", ctx->device_name);
1b6bf175
TT
985 goto get_newer;
986 }
1917875f 987#ifdef ENABLE_COMPRESSION
5dd8f963 988 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1917875f
TT
989 com_err(ctx->program_name, 0,
990 _("Warning: compression support is experimental.\n"));
991#endif
8fdc9985
TT
992#ifndef ENABLE_HTREE
993 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
994 com_err(ctx->program_name, 0,
995 _("E2fsck not compiled with HTREE support,\n\t"
996 "but filesystem %s has HTREE directories.\n"),
997 ctx->device_name);
998 goto get_newer;
999 }
1000#endif
1001
1b6bf175
TT
1002 /*
1003 * If the user specified a specific superblock, presumably the
1004 * master superblock has been trashed. So we mark the
1005 * superblock as dirty, so it can be written out.
1006 */
1007 if (ctx->superblock &&
1008 !(ctx->options & E2F_OPT_READONLY))
1009 ext2fs_mark_super_dirty(fs);
1010
1011 /*
e70ae99e
TT
1012 * We only update the master superblock because (a) paranoia;
1013 * we don't want to corrupt the backup superblocks, and (b) we
1014 * don't need to update the mount count and last checked
1015 * fields in the backup superblock (the kernel doesn't
1016 * update the backup superblocks anyway).
1b6bf175
TT
1017 */
1018 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1019
1020 ehandler_init(fs->io);
1021
1022 if (ctx->superblock)
1023 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1024 check_super_block(ctx);
a02ce9df 1025 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1026 fatal_error(ctx, 0);
1b6bf175
TT
1027 check_if_skip(ctx);
1028 if (bad_blocks_file)
1029 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1030 else if (cflag)
1031 test_disk(ctx);
a02ce9df 1032 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1033 fatal_error(ctx, 0);
5df55d7f 1034#ifdef ENABLE_SWAPFS
1b6bf175
TT
1035 if (normalize_swapfs) {
1036 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1037 ext2fs_native_flag()) {
0c4a0726
TT
1038 fprintf(stderr, _("%s: Filesystem byte order "
1039 "already normalized.\n"), ctx->device_name);
243dc31f 1040 fatal_error(ctx, 0);
1b6bf175
TT
1041 }
1042 }
f8188fff 1043 if (swapfs) {
1b6bf175 1044 swap_filesys(ctx);
a02ce9df 1045 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1046 fatal_error(ctx, 0);
f8188fff 1047 }
5df55d7f 1048#endif
1b6bf175
TT
1049
1050 /*
1051 * Mark the system as valid, 'til proven otherwise
1052 */
1053 ext2fs_mark_valid(fs);
1054
1055 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1056 if (retval) {
1057 com_err(ctx->program_name, retval,
0c4a0726 1058 _("while reading bad blocks inode"));
1b6bf175 1059 preenhalt(ctx);
0c4a0726
TT
1060 printf(_("This doesn't bode well,"
1061 " but we'll try to go on...\n"));
1b6bf175 1062 }
08b21301
TT
1063
1064 run_result = e2fsck_run(ctx);
5596defa 1065 e2fsck_clear_progbar(ctx);
08b21301 1066 if (run_result == E2F_FLAG_RESTART) {
0c4a0726 1067 printf(_("Restarting e2fsck from the beginning...\n"));
1b6bf175
TT
1068 retval = e2fsck_reset_context(ctx);
1069 if (retval) {
1070 com_err(ctx->program_name, retval,
0c4a0726 1071 _("while resetting context"));
243dc31f 1072 fatal_error(ctx, 0);
1b6bf175 1073 }
73f17cfc 1074 ext2fs_close(fs);
1b6bf175
TT
1075 goto restart;
1076 }
4cae0452
TT
1077 if (run_result & E2F_FLAG_CANCEL) {
1078 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1079 ctx->device_name : ctx->filesystem_name);
1080 exit_value |= FSCK_CANCELED;
1081 }
1082 if (run_result & E2F_FLAG_ABORT)
1083 fatal_error(ctx, _("aborted"));
1b6bf175
TT
1084
1085#ifdef MTRACE
1086 mtrace_print("Cleanup");
1087#endif
1088 if (ext2fs_test_changed(fs)) {
4cae0452 1089 exit_value |= FSCK_NONDESTRUCT;
1b6bf175 1090 if (!(ctx->options & E2F_OPT_PREEN))
0c4a0726 1091 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1b6bf175 1092 ctx->device_name);
ee895139 1093 if (ctx->mount_flags & EXT2_MF_ISROOT) {
0c4a0726 1094 printf(_("%s: ***** REBOOT LINUX *****\n"),
1b6bf175 1095 ctx->device_name);
4cae0452 1096 exit_value |= FSCK_REBOOT;
1b6bf175
TT
1097 }
1098 }
e70ae99e 1099 if (!ext2fs_test_valid(fs)) {
d3124019
TT
1100 printf(_("\n%s: ********** WARNING: Filesystem still has "
1101 "errors **********\n\n"), ctx->device_name);
4cae0452
TT
1102 exit_value |= FSCK_UNCORRECTED;
1103 exit_value &= ~FSCK_NONDESTRUCT;
d3124019 1104 }
4cae0452
TT
1105 if (exit_value & FSCK_CANCELED)
1106 exit_value &= ~FSCK_NONDESTRUCT;
c1637bd3 1107 else {
4cae0452 1108 show_stats(ctx);
c1637bd3
TT
1109 if (!(ctx->options & E2F_OPT_READONLY)) {
1110 if (ext2fs_test_valid(fs)) {
1111 if (!(sb->s_state & EXT2_VALID_FS))
1112 exit_value |= FSCK_NONDESTRUCT;
1113 sb->s_state = EXT2_VALID_FS;
1114 } else
1115 sb->s_state &= ~EXT2_VALID_FS;
1116 sb->s_mnt_count = 0;
1117 sb->s_lastcheck = time(NULL);
1118 ext2fs_mark_super_dirty(fs);
1119 }
1120 }
1b6bf175 1121
f8188fff 1122 e2fsck_write_bitmaps(ctx);
1b6bf175 1123
0628ae39
TT
1124 ext2fs_close(fs);
1125 ctx->fs = NULL;
f364093b
TT
1126 free(ctx->filesystem_name);
1127 free(ctx->journal_name);
0628ae39
TT
1128 e2fsck_free_context(ctx);
1129
8bf191e8 1130#ifdef RESOURCE_TRACK
1b6bf175
TT
1131 if (ctx->options & E2F_OPT_TIME)
1132 print_resource_track(NULL, &ctx->global_rtrack);
8bf191e8 1133#endif
1b6bf175 1134
1b6bf175
TT
1135 return exit_value;
1136}