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