]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/unix.c
e2fsck: update the backup superblocks if the feature bitmasks are changed
[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
c07f9f26
TT
42#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_DIRENT_H
46#include <dirent.h>
47#endif
1b6bf175
TT
48
49#include "et/com_err.h"
50#include "e2fsck.h"
51#include "problem.h"
52#include "../version.h"
53
1b6bf175 54/* Command line options */
54a31a3b
TT
55static int swapfs;
56static int normalize_swapfs;
57static int cflag; /* check disk */
58static int show_version_only;
59static int verbose;
1b6bf175 60
54a31a3b 61static int replace_bad_blocks;
4fb9d52b 62static int keep_bad_blocks;
54a31a3b 63static char *bad_blocks_file;
1b6bf175 64
243dc31f
TT
65e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
66
5e72cdbe
TT
67#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
68int journal_enable_debug = -1;
69#endif
70
1b6bf175
TT
71static void usage(e2fsck_t ctx)
72{
73 fprintf(stderr,
4fd6834c 74 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
1b6bf175 75 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
bb145b01 76 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
0684a4f3 77 "\t\t[-E extended-options] device\n"),
5596defa 78 ctx->program_name);
b55199ea 79
0c4a0726 80 fprintf(stderr, _("\nEmergency help:\n"
b55199ea
TT
81 " -p Automatic repair (no questions)\n"
82 " -n Make no changes to the filesystem\n"
83 " -y Assume \"yes\" to all questions\n"
19445ef9 84 " -c Check for bad blocks and add them to the badblock list\n"
53ef44c4
TT
85 " -f Force checking even if filesystem is marked clean\n"));
86 fprintf(stderr, _(""
b55199ea
TT
87 " -v Be verbose\n"
88 " -b superblock Use alternative superblock\n"
89 " -B blocksize Force blocksize when looking for superblock\n"
bb145b01 90 " -j external_journal Set location of the external journal\n"
b55199ea
TT
91 " -l bad_blocks_file Add to badblocks list\n"
92 " -L bad_blocks_file Set badblocks list\n"
0c4a0726 93 ));
b55199ea 94
1b6bf175
TT
95 exit(FSCK_USAGE);
96}
97
98static void show_stats(e2fsck_t ctx)
99{
100 ext2_filsys fs = ctx->fs;
f3358643 101 ext2_ino_t inodes, inodes_used;
8deb80a5 102 blk_t blocks, blocks_used;
1b6bf175
TT
103 int dir_links;
104 int num_files, num_links;
105 int frag_percent;
106
107 dir_links = 2 * ctx->fs_directory_count - 1;
108 num_files = ctx->fs_total_count - dir_links;
109 num_links = ctx->fs_links_count - dir_links;
110 inodes = fs->super->s_inodes_count;
111 inodes_used = (fs->super->s_inodes_count -
112 fs->super->s_free_inodes_count);
113 blocks = fs->super->s_blocks_count;
114 blocks_used = (fs->super->s_blocks_count -
115 fs->super->s_free_blocks_count);
116
117 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
118 frag_percent = (frag_percent + 5) / 10;
119
120 if (!verbose) {
d0ff90d5 121 printf(_("%s: %u/%u files (%0d.%d%% non-contiguous), %u/%u blocks\n"),
1b6bf175
TT
122 ctx->device_name, inodes_used, inodes,
123 frag_percent / 10, frag_percent % 10,
124 blocks_used, blocks);
125 return;
126 }
d0ff90d5
ES
127 printf (P_("\n%8u inode used (%2.2f%%)\n", "\n%8u inodes used (%2.2f%%)\n",
128 inodes_used), inodes_used, 100.0 * inodes_used / inodes);
129 printf (P_("%8u non-contiguous inode (%0d.%d%%)\n",
130 "%8u non-contiguous inodes (%0d.%d%%)\n",
113e405b 131 ctx->fs_fragmented),
0c4a0726 132 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
d0ff90d5 133 printf (_(" # of inodes with ind/dind/tind blocks: %u/%u/%u\n"),
0c4a0726 134 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
d0ff90d5
ES
135 printf (P_("%8u block used (%2.2f%%)\n", "%8u blocks used (%2.2f%%)\n",
136 blocks_used), blocks_used, 100.0 * blocks_used / blocks);
137 printf (P_("%8u bad block\n", "%8u bad blocks\n",
113e405b 138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
d0ff90d5 139 printf (P_("%8u large file\n", "%8u large files\n",
113e405b 140 ctx->large_files), ctx->large_files);
d0ff90d5 141 printf (P_("\n%8u regular file\n", "\n%8u regular files\n",
113e405b 142 ctx->fs_regular_count), ctx->fs_regular_count);
d0ff90d5 143 printf (P_("%8u directory\n", "%8u directories\n",
113e405b 144 ctx->fs_directory_count), ctx->fs_directory_count);
d0ff90d5
ES
145 printf (P_("%8u character device file\n",
146 "%8u character device files\n", ctx->fs_chardev_count),
113e405b 147 ctx->fs_chardev_count);
d0ff90d5 148 printf (P_("%8u block device file\n", "%8u block device files\n",
113e405b 149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
d0ff90d5 150 printf (P_("%8u fifo\n", "%8u fifos\n", ctx->fs_fifo_count),
113e405b 151 ctx->fs_fifo_count);
d0ff90d5 152 printf (P_("%8u link\n", "%8u links\n",
113e405b
TT
153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
d0ff90d5 155 printf (P_("%8u symbolic link", "%8u symbolic links",
113e405b 156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
d0ff90d5 157 printf (P_(" (%u fast symbolic link)\n", " (%u fast symbolic links)\n",
113e405b 158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
d0ff90d5 159 printf (P_("%8u socket\n", "%8u sockets\n", ctx->fs_sockets_count),
113e405b
TT
160 ctx->fs_sockets_count);
161 printf ("--------\n");
d0ff90d5 162 printf (P_("%8u file\n", "%8u files\n",
113e405b 163 ctx->fs_total_count - dir_links),
0c4a0726 164 ctx->fs_total_count - dir_links);
1b6bf175
TT
165}
166
167static void check_mount(e2fsck_t ctx)
168{
169 errcode_t retval;
ee895139 170 int cont;
1b6bf175 171
ee895139
TT
172 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
173 &ctx->mount_flags);
1b6bf175
TT
174 if (retval) {
175 com_err("ext2fs_check_if_mount", retval,
0c4a0726 176 _("while determining whether %s is mounted."),
1b6bf175
TT
177 ctx->filesystem_name);
178 return;
179 }
1b6bf175 180
1b6bf175 181 /*
ae1182cb
TT
182 * If the filesystem isn't mounted, or it's the root
183 * filesystem and it's mounted read-only, and we're not doing
184 * a read/write check, then everything's fine.
1b6bf175 185 */
ee895139
TT
186 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
187 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
ae1182cb
TT
188 (ctx->mount_flags & EXT2_MF_READONLY) &&
189 !(ctx->options & E2F_OPT_WRITECHECK)))
83e6ac84
TT
190 return;
191
ae1182cb
TT
192 if ((ctx->options & E2F_OPT_READONLY) &&
193 !(ctx->options & E2F_OPT_WRITECHECK)) {
0c4a0726 194 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
1b6bf175
TT
195 return;
196 }
197
0c4a0726 198 printf(_("%s is mounted. "), ctx->filesystem_name);
54a31a3b 199 if (!ctx->interactive)
243dc31f 200 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
0c4a0726 201 printf(_("\n\n\007\007\007\007WARNING!!! "
74033350 202 "Running e2fsck on a mounted filesystem may cause\n"
0c4a0726
TT
203 "SEVERE filesystem damage.\007\007\007\n\n"));
204 cont = ask_yn(_("Do you really want to continue"), -1);
1b6bf175 205 if (!cont) {
0c4a0726 206 printf (_("check aborted.\n"));
1b6bf175
TT
207 exit (0);
208 }
209 return;
210}
211
54434927 212static int is_on_batt(void)
015b03df
TT
213{
214 FILE *f;
c07f9f26
TT
215 DIR *d;
216 char tmp[80], tmp2[80], fname[80];
015b03df 217 unsigned int acflag;
c07f9f26 218 struct dirent* de;
015b03df
TT
219
220 f = fopen("/proc/apm", "r");
221 if (f) {
222 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
223 acflag = 1;
224 fclose(f);
225 return (acflag != 1);
226 }
c07f9f26 227 d = opendir("/proc/acpi/ac_adapter");
ecd0d8fe 228 if (d) {
9214dccb 229 while ((de=readdir(d)) != NULL) {
ecd0d8fe
TT
230 if (!strncmp(".", de->d_name, 1))
231 continue;
232 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
233 de->d_name);
234 f = fopen(fname, "r");
235 if (!f)
236 continue;
237 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
238 tmp[0] = 0;
239 fclose(f);
240 if (strncmp(tmp, "off-line", 8) == 0) {
241 closedir(d);
242 return 1;
243 }
c07f9f26 244 }
4b13704c 245 closedir(d);
ecd0d8fe 246 }
015b03df
TT
247 return 0;
248}
249
1b6bf175
TT
250/*
251 * This routine checks to see if a filesystem can be skipped; if so,
252 * it will exit with E2FSCK_OK. Under some conditions it will print a
253 * message explaining why a check is being forced.
254 */
255static void check_if_skip(e2fsck_t ctx)
256{
257 ext2_filsys fs = ctx->fs;
258 const char *reason = NULL;
b6a0807b 259 unsigned int reason_arg = 0;
6de289cb 260 long next_check;
015b03df 261 int batt = is_on_batt();
a5f37a9b 262 int defer_check_on_battery;
60702c26 263 time_t lastcheck;
a5f37a9b
TT
264
265 profile_get_boolean(ctx->profile, "options",
266 "defer_check_on_battery", 0, 1,
267 &defer_check_on_battery);
268 if (!defer_check_on_battery)
269 batt = 0;
270
d37066a9
TT
271 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
272 cflag || swapfs)
1b6bf175
TT
273 return;
274
60702c26
TT
275 lastcheck = fs->super->s_lastcheck;
276 if (lastcheck > ctx->now)
277 lastcheck -= ctx->time_fudge;
550a4afa
TT
278 if ((fs->super->s_state & EXT2_ERROR_FS) ||
279 !ext2fs_test_valid(fs))
b6a0807b 280 reason = _(" contains a file system with errors");
24fc5032 281 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
b6a0807b 282 reason = _(" was not cleanly unmounted");
bc57f153 283 else if ((fs->super->s_max_mnt_count > 0) &&
17390c04 284 (fs->super->s_mnt_count >=
b6a0807b
TT
285 (unsigned) fs->super->s_max_mnt_count)) {
286 reason = _(" has been mounted %u times without being checked");
287 reason_arg = fs->super->s_mnt_count;
015b03df
TT
288 if (batt && (fs->super->s_mnt_count <
289 (unsigned) fs->super->s_max_mnt_count*2))
290 reason = 0;
b6a0807b 291 } else if (fs->super->s_checkinterval &&
60702c26 292 ((ctx->now - lastcheck) >= fs->super->s_checkinterval)) {
b6a0807b 293 reason = _(" has gone %u days without being checked");
1f3ad14a
TT
294 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
295 if (batt && ((ctx->now - fs->super->s_lastcheck) <
54434927 296 fs->super->s_checkinterval*2))
015b03df 297 reason = 0;
b6a0807b 298 }
1b6bf175 299 if (reason) {
b6a0807b
TT
300 fputs(ctx->device_name, stdout);
301 printf(reason, reason_arg);
302 fputs(_(", check forced.\n"), stdout);
1b6bf175
TT
303 return;
304 }
d0ff90d5 305 printf(_("%s: clean, %u/%u files, %u/%u blocks"), ctx->device_name,
1b6bf175
TT
306 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
307 fs->super->s_inodes_count,
308 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
309 fs->super->s_blocks_count);
6de289cb
TT
310 next_check = 100000;
311 if (fs->super->s_max_mnt_count > 0) {
312 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
313 if (next_check <= 0)
314 next_check = 1;
315 }
66fbee8c 316 if (fs->super->s_checkinterval &&
1f3ad14a 317 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
6de289cb
TT
318 next_check = 1;
319 if (next_check <= 5) {
bc3392c3
TT
320 if (next_check == 1) {
321 if (batt)
322 fputs(_(" (check deferred; on battery)"),
323 stdout);
324 else
325 fputs(_(" (check after next mount)"), stdout);
326 } else
54434927 327 printf(_(" (check in %ld mounts)"), next_check);
6de289cb
TT
328 }
329 fputc('\n', stdout);
1b6bf175 330 ext2fs_close(fs);
6d222f32
TT
331 ctx->fs = NULL;
332 e2fsck_free_context(ctx);
1b6bf175
TT
333 exit(FSCK_OK);
334}
335
efac9a1b
TT
336/*
337 * For completion notice
338 */
5596defa
TT
339struct percent_tbl {
340 int max_pass;
341 int table[32];
342};
343struct percent_tbl e2fsck_tbl = {
344 5, { 0, 70, 90, 92, 95, 100 }
345};
54a31a3b 346static char bar[128], spaces[128];
5596defa
TT
347
348static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
349 int max)
350{
351 float percent;
352
353 if (pass <= 0)
354 return 0.0;
b8d164cd 355 if (pass > tbl->max_pass || max == 0)
5596defa
TT
356 return 100.0;
357 percent = ((float) curr) / ((float) max);
358 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
359 + tbl->table[pass-1]);
360}
361
362extern void e2fsck_clear_progbar(e2fsck_t ctx)
363{
364 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
365 return;
366
54a31a3b
TT
367 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
368 ctx->stop_meta);
bc34d6be 369 fflush(stdout);
5596defa
TT
370 ctx->flags &= ~E2F_FLAG_PROG_BAR;
371}
372
520ead37 373int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
b0700a1b
TT
374 unsigned int dpynum)
375{
376 static const char spinner[] = "\\|/-";
b0700a1b 377 int i;
54434927 378 unsigned int tick;
b0700a1b
TT
379 struct timeval tv;
380 int dpywidth;
9214dccb 381 int fixed_percent;
b0700a1b
TT
382
383 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
384 return 0;
385
386 /*
387 * Calculate the new progress position. If the
388 * percentage hasn't changed, then we skip out right
389 * away.
390 */
9214dccb
TT
391 fixed_percent = (int) ((10 * percent) + 0.5);
392 if (ctx->progress_last_percent == fixed_percent)
b0700a1b 393 return 0;
9214dccb 394 ctx->progress_last_percent = fixed_percent;
b0700a1b
TT
395
396 /*
397 * If we've already updated the spinner once within
398 * the last 1/8th of a second, no point doing it
399 * again.
400 */
401 gettimeofday(&tv, NULL);
402 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
403 if ((tick == ctx->progress_last_time) &&
9214dccb 404 (fixed_percent != 0) && (fixed_percent != 1000))
b0700a1b
TT
405 return 0;
406 ctx->progress_last_time = tick;
407
408 /*
409 * Advance the spinner, and note that the progress bar
410 * will be on the screen
411 */
412 ctx->progress_pos = (ctx->progress_pos+1) & 3;
413 ctx->flags |= E2F_FLAG_PROG_BAR;
414
415 dpywidth = 66 - strlen(label);
416 dpywidth = 8 * (dpywidth / 8);
417 if (dpynum)
418 dpywidth -= 8;
419
420 i = ((percent * dpywidth) + 50) / 100;
54a31a3b
TT
421 printf("%s%s: |%s%s", ctx->start_meta, label,
422 bar + (sizeof(bar) - (i+1)),
b0700a1b 423 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
9214dccb 424 if (fixed_percent == 1000)
b0700a1b
TT
425 fputc('|', stdout);
426 else
427 fputc(spinner[ctx->progress_pos & 3], stdout);
9214dccb 428 printf(" %4.1f%% ", percent);
b0700a1b 429 if (dpynum)
9214dccb 430 printf("%u\r", dpynum);
b0700a1b 431 else
9214dccb
TT
432 fputs(" \r", stdout);
433 fputs(ctx->stop_meta, stdout);
b0700a1b 434
9214dccb 435 if (fixed_percent == 1000)
b0700a1b
TT
436 e2fsck_clear_progbar(ctx);
437 fflush(stdout);
438
439 return 0;
440}
441
efac9a1b
TT
442static int e2fsck_update_progress(e2fsck_t ctx, int pass,
443 unsigned long cur, unsigned long max)
444{
efac9a1b 445 char buf[80];
5596defa 446 float percent;
f75c28de
TT
447
448 if (pass == 0)
449 return 0;
efac9a1b
TT
450
451 if (ctx->progress_fd) {
452 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
453 write(ctx->progress_fd, buf, strlen(buf));
454 } else {
e4c8e885 455 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
b0700a1b
TT
456 e2fsck_simple_progress(ctx, ctx->device_name,
457 percent, 0);
efac9a1b
TT
458 }
459 return 0;
460}
1b6bf175
TT
461
462#define PATH_SET "PATH=/sbin"
463
5ba23cb1 464static void reserve_stdio_fds(void)
24fc5032
TT
465{
466 int fd;
467
468 while (1) {
469 fd = open("/dev/null", O_RDWR);
470 if (fd > 2)
471 break;
75d83bec 472 if (fd < 0) {
0c4a0726
TT
473 fprintf(stderr, _("ERROR: Couldn't open "
474 "/dev/null (%s)\n"),
75d83bec
TT
475 strerror(errno));
476 break;
477 }
24fc5032
TT
478 }
479 close(fd);
480}
481
9ecd8bec 482#ifdef HAVE_SIGNAL_H
54434927 483static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
5596defa 484{
243dc31f 485 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
486
487 if (!ctx)
488 return;
489
490 ctx->progress = e2fsck_update_progress;
491 ctx->progress_fd = 0;
492}
493
54434927 494static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
5596defa 495{
243dc31f 496 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
497
498 if (!ctx)
499 return;
500
501 e2fsck_clear_progbar(ctx);
502 ctx->progress = 0;
503}
4cae0452 504
54434927 505static void signal_cancel(int sig EXT2FS_ATTR((unused)))
4cae0452
TT
506{
507 e2fsck_t ctx = e2fsck_global_ctx;
508
509 if (!ctx)
510 exit(FSCK_CANCELED);
511
512 ctx->flags |= E2F_FLAG_CANCEL;
513}
9ecd8bec 514#endif
5596defa 515
0684a4f3
TT
516static void parse_extended_opts(e2fsck_t ctx, const char *opts)
517{
518 char *buf, *token, *next, *p, *arg;
f364093b 519 int ea_ver;
0684a4f3
TT
520 int extended_usage = 0;
521
f364093b 522 buf = string_copy(ctx, opts, 0);
0684a4f3
TT
523 for (token = buf; token && *token; token = next) {
524 p = strchr(token, ',');
525 next = 0;
526 if (p) {
527 *p = 0;
528 next = p+1;
cae542ce 529 }
0684a4f3
TT
530 arg = strchr(token, '=');
531 if (arg) {
532 *arg = 0;
533 arg++;
534 }
535 if (strcmp(token, "ea_ver") == 0) {
536 if (!arg) {
537 extended_usage++;
538 continue;
539 }
540 ea_ver = strtoul(arg, &p, 0);
541 if (*p ||
542 ((ea_ver != 1) && (ea_ver != 2))) {
543 fprintf(stderr,
544 _("Invalid EA version.\n"));
545 extended_usage++;
546 continue;
547 }
548 ctx->ext_attr_ver = ea_ver;
bb145b01
TT
549 } else {
550 fprintf(stderr, _("Unknown extended option: %s\n"),
551 token);
0684a4f3 552 extended_usage++;
bb145b01 553 }
0684a4f3 554 }
cae542ce
BB
555 free(buf);
556
0684a4f3 557 if (extended_usage) {
bb145b01
TT
558 fputs(("\nExtended options are separated by commas, "
559 "and may take an argument which\n"
560 "is set off by an equals ('=') sign. "
561 "Valid extended options are:\n"
562 "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
0684a4f3
TT
563 exit(1);
564 }
cae542ce 565}
0684a4f3 566
f5f14fcf
TT
567static void syntax_err_report(const char *filename, long err, int line_num)
568{
569 fprintf(stderr,
570 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
571 filename, line_num, error_message(err));
572 exit(FSCK_ERROR);
573}
574
abcfdfda 575static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
0684a4f3 576
1b6bf175
TT
577static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
578{
579 int flush = 0;
ae8160e6 580 int c, fd;
1b6bf175
TT
581#ifdef MTRACE
582 extern void *mallwatch;
583#endif
1b6bf175
TT
584 e2fsck_t ctx;
585 errcode_t retval;
9ecd8bec 586#ifdef HAVE_SIGNAL_H
5596defa 587 struct sigaction sa;
9ecd8bec 588#endif
0684a4f3 589 char *extended_opts = 0;
5dd2a6e0 590 char *cp;
5845caa4
TT
591 int res; /* result of sscanf */
592#ifdef CONFIG_JBD_DEBUG
593 char *jbd_debug;
594#endif
1b6bf175
TT
595
596 retval = e2fsck_allocate_context(&ctx);
597 if (retval)
598 return retval;
599
600 *ret_ctx = ctx;
601
908b785c
TT
602 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
603 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
54a31a3b
TT
604 if (isatty(0) && isatty(1)) {
605 ctx->interactive = 1;
606 } else {
607 ctx->start_meta[0] = '\001';
608 ctx->stop_meta[0] = '\002';
609 }
610 memset(bar, '=', sizeof(bar)-1);
611 memset(spaces, ' ', sizeof(spaces)-1);
a6d8302b
TT
612 add_error_table(&et_ext2_error_table);
613 add_error_table(&et_prof_error_table);
f364093b 614 blkid_get_cache(&ctx->blkid, NULL);
1b6bf175
TT
615
616 if (argc && *argv)
617 ctx->program_name = *argv;
618 else
619 ctx->program_name = "e2fsck";
4fb9d52b 620 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
1b6bf175 621 switch (c) {
efac9a1b
TT
622 case 'C':
623 ctx->progress = e2fsck_update_progress;
5845caa4
TT
624 res = sscanf(optarg, "%d", &ctx->progress_fd);
625 if (res != 1)
626 goto sscanf_err;
627
fc9a69ca
TT
628 if (!ctx->progress_fd)
629 break;
ae8160e6
TT
630 /* Validate the file descriptor to avoid disasters */
631 fd = dup(ctx->progress_fd);
632 if (fd < 0) {
633 fprintf(stderr,
634 _("Error validating file descriptor %d: %s\n"),
635 ctx->progress_fd,
636 error_message(errno));
637 fatal_error(ctx,
638 _("Invalid completion information file descriptor"));
639 } else
640 close(fd);
efac9a1b 641 break;
850d05e9
TT
642 case 'D':
643 ctx->options |= E2F_OPT_COMPRESS_DIRS;
644 break;
0684a4f3
TT
645 case 'E':
646 extended_opts = optarg;
647 break;
1b6bf175
TT
648 case 'p':
649 case 'a':
8161a748
TT
650 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
651 conflict_opt:
652 fatal_error(ctx,
f4b6d2a0 653 _("Only one of the options -p/-a, -n or -y may be specified."));
8161a748 654 }
1b6bf175 655 ctx->options |= E2F_OPT_PREEN;
1b6bf175
TT
656 break;
657 case 'n':
8161a748
TT
658 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
659 goto conflict_opt;
1b6bf175 660 ctx->options |= E2F_OPT_NO;
1b6bf175
TT
661 break;
662 case 'y':
8161a748
TT
663 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
664 goto conflict_opt;
1b6bf175 665 ctx->options |= E2F_OPT_YES;
1b6bf175
TT
666 break;
667 case 't':
8bf191e8 668#ifdef RESOURCE_TRACK
1b6bf175
TT
669 if (ctx->options & E2F_OPT_TIME)
670 ctx->options |= E2F_OPT_TIME2;
671 else
672 ctx->options |= E2F_OPT_TIME;
8bf191e8 673#else
0c4a0726
TT
674 fprintf(stderr, _("The -t option is not "
675 "supported on this version of e2fsck.\n"));
8bf191e8 676#endif
1b6bf175
TT
677 break;
678 case 'c':
3ed57c27
TT
679 if (cflag++)
680 ctx->options |= E2F_OPT_WRITECHECK;
1b6bf175
TT
681 ctx->options |= E2F_OPT_CHECKBLOCKS;
682 break;
683 case 'r':
684 /* What we do by default, anyway! */
685 break;
686 case 'b':
5845caa4
TT
687 res = sscanf(optarg, "%d", &ctx->use_superblock);
688 if (res != 1)
689 goto sscanf_err;
ae6cdcf7 690 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
1b6bf175
TT
691 break;
692 case 'B':
f1a1761d 693 ctx->blocksize = atoi(optarg);
1b6bf175
TT
694 break;
695 case 'I':
5845caa4
TT
696 res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
697 if (res != 1)
698 goto sscanf_err;
1b6bf175 699 break;
adee8d75 700 case 'j':
f364093b 701 ctx->journal_name = string_copy(ctx, optarg, 0);
adee8d75 702 break;
1b6bf175 703 case 'P':
5845caa4
TT
704 res = sscanf(optarg, "%d", &ctx->process_inode_size);
705 if (res != 1)
706 goto sscanf_err;
1b6bf175
TT
707 break;
708 case 'L':
709 replace_bad_blocks++;
710 case 'l':
f364093b 711 bad_blocks_file = string_copy(ctx, optarg, 0);
1b6bf175
TT
712 break;
713 case 'd':
714 ctx->options |= E2F_OPT_DEBUG;
715 break;
716 case 'f':
d37066a9 717 ctx->options |= E2F_OPT_FORCE;
1b6bf175
TT
718 break;
719 case 'F':
1b6bf175 720 flush = 1;
1b6bf175
TT
721 break;
722 case 'v':
723 verbose = 1;
724 break;
725 case 'V':
726 show_version_only = 1;
727 break;
728#ifdef MTRACE
729 case 'M':
730 mallwatch = (void *) strtol(optarg, NULL, 0);
731 break;
732#endif
733 case 'N':
734 ctx->device_name = optarg;
735 break;
5df55d7f 736#ifdef ENABLE_SWAPFS
1b6bf175
TT
737 case 's':
738 normalize_swapfs = 1;
739 case 'S':
740 swapfs = 1;
741 break;
5df55d7f
TT
742#else
743 case 's':
744 case 'S':
745 fprintf(stderr, _("Byte-swapping filesystems "
746 "not compiled in this version "
747 "of e2fsck\n"));
748 exit(1);
749#endif
4fb9d52b
TT
750 case 'k':
751 keep_bad_blocks++;
bc69f82d 752 break;
1b6bf175
TT
753 default:
754 usage(ctx);
755 }
756 if (show_version_only)
757 return 0;
758 if (optind != argc - 1)
759 usage(ctx);
760 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
850d05e9 761 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
1b6bf175 762 ctx->options |= E2F_OPT_READONLY;
2e8ca9a2
TT
763 ctx->io_options = strchr(argv[optind], '?');
764 if (ctx->io_options)
765 *ctx->io_options++ = 0;
f364093b 766 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
817e49e3
TT
767 if (!ctx->filesystem_name) {
768 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
769 argv[optind]);
770 fatal_error(ctx, 0);
771 }
0684a4f3
TT
772 if (extended_opts)
773 parse_extended_opts(ctx, extended_opts);
1017f651 774
5dd2a6e0
TT
775 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
776 config_fn[0] = cp;
f5f14fcf 777 profile_set_syntax_err_cb(syntax_err_report);
1017f651
TT
778 profile_init(config_fn, &ctx->profile);
779
28ffafb0 780 if (flush) {
4ea7bd04 781 fd = open(ctx->filesystem_name, O_RDONLY, 0);
1b6bf175 782 if (fd < 0) {
0c4a0726
TT
783 com_err("open", errno,
784 _("while opening %s for flushing"),
1b6bf175 785 ctx->filesystem_name);
243dc31f 786 fatal_error(ctx, 0);
1b6bf175 787 }
5df55d7f 788 if ((retval = ext2fs_sync_device(fd, 1))) {
5ba23cb1 789 com_err("ext2fs_sync_device", retval,
0c4a0726 790 _("while trying to flush %s"),
1b6bf175 791 ctx->filesystem_name);
243dc31f 792 fatal_error(ctx, 0);
1b6bf175
TT
793 }
794 close(fd);
1b6bf175 795 }
5df55d7f 796#ifdef ENABLE_SWAPFS
1b6bf175
TT
797 if (swapfs) {
798 if (cflag || bad_blocks_file) {
0c4a0726 799 fprintf(stderr, _("Incompatible options not "
3ed57c27 800 "allowed when byte-swapping.\n"));
243dc31f 801 exit(FSCK_USAGE);
1b6bf175
TT
802 }
803 }
5df55d7f 804#endif
3ed57c27
TT
805 if (cflag && bad_blocks_file) {
806 fprintf(stderr, _("The -c and the -l/-L options may "
807 "not be both used at the same time.\n"));
808 exit(FSCK_USAGE);
809 }
9ecd8bec 810#ifdef HAVE_SIGNAL_H
5596defa
TT
811 /*
812 * Set up signal action
813 */
814 memset(&sa, 0, sizeof(struct sigaction));
850d05e9
TT
815 sa.sa_handler = signal_cancel;
816 sigaction(SIGINT, &sa, 0);
817 sigaction(SIGTERM, &sa, 0);
5596defa
TT
818#ifdef SA_RESTART
819 sa.sa_flags = SA_RESTART;
820#endif
243dc31f 821 e2fsck_global_ctx = ctx;
5596defa
TT
822 sa.sa_handler = signal_progress_on;
823 sigaction(SIGUSR1, &sa, 0);
824 sa.sa_handler = signal_progress_off;
825 sigaction(SIGUSR2, &sa, 0);
9ecd8bec 826#endif
6d222f32
TT
827
828 /* Update our PATH to include /sbin if we need to run badblocks */
829 if (cflag) {
830 char *oldpath = getenv("PATH");
642935c0
TT
831 char *newpath;
832 int len = sizeof(PATH_SET) + 1;
833
834 if (oldpath)
835 len += strlen(oldpath);
836
837 newpath = malloc(len);
838 if (!newpath)
839 fatal_error(ctx, "Couldn't malloc() newpath");
840 strcpy(newpath, PATH_SET);
841
6d222f32 842 if (oldpath) {
642935c0
TT
843 strcat(newpath, ":");
844 strcat(newpath, oldpath);
845 }
846 putenv(newpath);
6d222f32 847 }
5e72cdbe 848#ifdef CONFIG_JBD_DEBUG
5845caa4
TT
849 jbd_debug = getenv("E2FSCK_JBD_DEBUG");
850 if (jbd_debug)
851 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
852 if (res != 1) {
853 fprintf(stderr,
854 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
855 jbd_debug);
856 exit (1);
857 }
5e72cdbe 858#endif
1b6bf175 859 return 0;
5845caa4
TT
860
861sscanf_err:
862 fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
863 c, optarg);
864 exit (1);
1b6bf175
TT
865}
866
867static const char *my_ver_string = E2FSPROGS_VERSION;
868static const char *my_ver_date = E2FSPROGS_DATE;
869
870int main (int argc, char *argv[])
871{
cd538080 872 errcode_t retval = 0, orig_retval = 0;
1b6bf175 873 int exit_value = FSCK_OK;
1b6bf175
TT
874 ext2_filsys fs = 0;
875 io_manager io_ptr;
5dd8f963 876 struct ext2_super_block *sb;
1b6bf175
TT
877 const char *lib_ver_date;
878 int my_ver, lib_ver;
879 e2fsck_t ctx;
880 struct problem_context pctx;
08b21301 881 int flags, run_result;
5107d0d1 882 int journal_size;
9f0288d3 883 int sysval, sys_page_size = 4096;
1b6bf175
TT
884
885 clear_problem_context(&pctx);
886#ifdef MTRACE
887 mtrace();
888#endif
889#ifdef MCHECK
890 mcheck(0);
0c4a0726
TT
891#endif
892#ifdef ENABLE_NLS
893 setlocale(LC_MESSAGES, "");
14308a53 894 setlocale(LC_CTYPE, "");
0c4a0726
TT
895 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
896 textdomain(NLS_CAT_NAME);
1b6bf175
TT
897#endif
898 my_ver = ext2fs_parse_version_string(my_ver_string);
899 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
900 if (my_ver > lib_ver) {
0c4a0726
TT
901 fprintf( stderr, _("Error: ext2fs library version "
902 "out of date!\n"));
1b6bf175
TT
903 show_version_only++;
904 }
905
906 retval = PRS(argc, argv, &ctx);
907 if (retval) {
908 com_err("e2fsck", retval,
0c4a0726 909 _("while trying to initialize program"));
243dc31f 910 exit(FSCK_ERROR);
1b6bf175 911 }
24fc5032
TT
912 reserve_stdio_fds();
913
8bf191e8 914#ifdef RESOURCE_TRACK
1b6bf175 915 init_resource_track(&ctx->global_rtrack);
8bf191e8 916#endif
1b6bf175
TT
917
918 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
908b785c 919 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
0f8973fb 920 my_ver_date);
1b6bf175
TT
921
922 if (show_version_only) {
0c4a0726 923 fprintf(stderr, _("\tUsing %s, %s\n"),
1b6bf175 924 error_message(EXT2_ET_BASE), lib_ver_date);
243dc31f 925 exit(FSCK_OK);
1b6bf175
TT
926 }
927
928 check_mount(ctx);
929
930 if (!(ctx->options & E2F_OPT_PREEN) &&
931 !(ctx->options & E2F_OPT_NO) &&
932 !(ctx->options & E2F_OPT_YES)) {
54a31a3b 933 if (!ctx->interactive)
f8188fff 934 fatal_error(ctx,
0c4a0726 935 _("need terminal for interactive repairs"));
1b6bf175
TT
936 }
937 ctx->superblock = ctx->use_superblock;
938restart:
2a29f135 939#ifdef CONFIG_TESTIO_DEBUG
1b6bf175
TT
940 io_ptr = test_io_manager;
941 test_io_backing_manager = unix_io_manager;
2a29f135
TT
942#else
943 io_ptr = unix_io_manager;
1b6bf175 944#endif
17390c04
TT
945 flags = 0;
946 if ((ctx->options & E2F_OPT_READONLY) == 0)
947 flags |= EXT2_FLAG_RW;
2e14e0c8
TT
948 if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
949 flags |= EXT2_FLAG_EXCLUSIVE;
17390c04 950
f1a1761d 951 if (ctx->superblock && ctx->blocksize) {
2e8ca9a2
TT
952 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
953 flags, ctx->superblock, ctx->blocksize,
954 io_ptr, &fs);
1b6bf175 955 } else if (ctx->superblock) {
932a489c
AD
956 int blocksize;
957 for (blocksize = EXT2_MIN_BLOCK_SIZE;
958 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
2e8ca9a2
TT
959 retval = ext2fs_open2(ctx->filesystem_name,
960 ctx->io_options, flags,
961 ctx->superblock, blocksize,
962 io_ptr, &fs);
1b6bf175
TT
963 if (!retval)
964 break;
965 }
966 } else
2e8ca9a2
TT
967 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
968 flags, 0, 0, io_ptr, &fs);
ae6cdcf7
TT
969 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
970 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1b6bf175 971 ((retval == EXT2_ET_BAD_MAGIC) ||
cd538080 972 (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
1b6bf175
TT
973 ((retval == 0) && ext2fs_check_desc(fs)))) {
974 if (!fs || (fs->group_desc_count > 1)) {
cd538080
TT
975 printf(_("%s: %s trying backup blocks...\n"),
976 ctx->program_name,
977 retval ? _("Superblock invalid,") :
0c4a0726 978 _("Group descriptors look bad..."));
f1a1761d 979 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
1b6bf175
TT
980 if (fs)
981 ext2fs_close(fs);
cd538080 982 orig_retval = retval;
1b6bf175
TT
983 goto restart;
984 }
985 }
986 if (retval) {
cd538080
TT
987 if (orig_retval)
988 retval = orig_retval;
0c4a0726 989 com_err(ctx->program_name, retval, _("while trying to open %s"),
1b6bf175 990 ctx->filesystem_name);
24dd4028 991 if (retval == EXT2_ET_REV_TOO_HIGH) {
0c4a0726 992 printf(_("The filesystem revision is apparently "
24dd4028
TT
993 "too high for this version of e2fsck.\n"
994 "(Or the filesystem superblock "
0c4a0726 995 "is corrupt)\n\n"));
24dd4028
TT
996 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
997 } else if (retval == EXT2_ET_SHORT_READ)
0c4a0726 998 printf(_("Could this be a zero-length partition?\n"));
1b6bf175 999 else if ((retval == EPERM) || (retval == EACCES))
0c4a0726
TT
1000 printf(_("You must have %s access to the "
1001 "filesystem or be root\n"),
1b6bf175
TT
1002 (ctx->options & E2F_OPT_READONLY) ?
1003 "r/o" : "r/w");
1004 else if (retval == ENXIO)
0c4a0726 1005 printf(_("Possibly non-existent or swap device?\n"));
2e14e0c8
TT
1006 else if (retval == EBUSY)
1007 printf(_("Filesystem mounted or opened exclusively "
1008 "by another program?\n"));
68227542
TT
1009#ifdef EROFS
1010 else if (retval == EROFS)
0c4a0726 1011 printf(_("Disk write-protected; use the -n option "
68227542 1012 "to do a read-only\n"
0c4a0726 1013 "check of the device.\n"));
68227542 1014#endif
1b6bf175
TT
1015 else
1016 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
243dc31f 1017 fatal_error(ctx, 0);
1b6bf175 1018 }
058ad1c7
TT
1019 /*
1020 * We only update the master superblock because (a) paranoia;
1021 * we don't want to corrupt the backup superblocks, and (b) we
1022 * don't need to update the mount count and last checked
1023 * fields in the backup superblock (the kernel doesn't update
1024 * the backup superblocks anyway). With newer versions of the
1025 * library this flag is set by ext2fs_open2(), but we set this
1026 * here just to be sure. (No, we don't support e2fsck running
1027 * with some other libext2fs than the one that it was shipped
1028 * with, but just in case....)
1029 */
1030 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
d2af1bdd
TT
1031
1032 if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1033 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1034 int need_restart = 0;
1035
1036 pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name,
1037 blocksize,
1038 &ctx->num_blocks);
1039 /*
1040 * The floppy driver refuses to allow anyone else to
1041 * open the device if has been opened with O_EXCL;
1042 * this is unlike other block device drivers in Linux.
1043 * To handle this, we close the filesystem and then
1044 * reopen the filesystem after we get the device size.
1045 */
1046 if (pctx.errcode == EBUSY) {
1047 ext2fs_close(fs);
1048 need_restart++;
1049 pctx.errcode =
1050 ext2fs_get_device_size(ctx->filesystem_name,
1051 blocksize,
1052 &ctx->num_blocks);
1053 }
1054 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1055 ctx->num_blocks = 0;
1056 else if (pctx.errcode) {
1057 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1058 ctx->flags |= E2F_FLAG_ABORT;
1059 fatal_error(ctx, 0);
d2af1bdd
TT
1060 }
1061 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1062 if (need_restart)
1063 goto restart;
1064 }
1065
1b6bf175 1066 ctx->fs = fs;
54dc7ca2 1067 fs->priv_data = ctx;
8dceb924 1068 fs->now = ctx->now;
5dd8f963
TT
1069 sb = fs->super;
1070 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1b6bf175 1071 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
0c4a0726 1072 _("while trying to open %s"),
1b6bf175 1073 ctx->filesystem_name);
f8188fff 1074 get_newer:
0c4a0726 1075 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1b6bf175 1076 }
3b5386dc
TT
1077
1078 /*
1079 * Set the device name, which is used whenever we print error
1080 * or informational messages to the user.
1081 */
1082 if (ctx->device_name == 0 &&
5dd8f963 1083 (sb->s_volume_name[0] != 0)) {
f364093b
TT
1084 ctx->device_name = string_copy(ctx, sb->s_volume_name,
1085 sizeof(sb->s_volume_name));
3b5386dc
TT
1086 }
1087 if (ctx->device_name == 0)
1088 ctx->device_name = ctx->filesystem_name;
1089
17390c04 1090 /*
9b565759 1091 * Make sure the ext3 superblock fields are consistent.
17390c04 1092 */
3b5386dc
TT
1093 retval = e2fsck_check_ext3_journal(ctx);
1094 if (retval) {
1095 com_err(ctx->program_name, retval,
1096 _("while checking ext3 journal for %s"),
1097 ctx->device_name);
243dc31f 1098 fatal_error(ctx, 0);
3b5386dc
TT
1099 }
1100
9b565759
TT
1101 /*
1102 * Check to see if we need to do ext3-style recovery. If so,
1103 * do it, and then restart the fsck.
1104 */
5dd8f963 1105 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
2575fb04
TT
1106 if (ctx->options & E2F_OPT_READONLY) {
1107 printf(_("Warning: skipping journal recovery "
1108 "because doing a read-only filesystem "
1109 "check.\n"));
1110 io_channel_flush(ctx->fs->io);
1111 } else {
b92ae153
TT
1112 if (ctx->flags & E2F_FLAG_RESTARTED) {
1113 /*
1114 * Whoops, we attempted to run the
1115 * journal twice. This should never
1116 * happen, unless the hardware or
1117 * device driver is being bogus.
1118 */
1119 com_err(ctx->program_name, 0,
1120 _("unable to set superblock flags on %s\n"), ctx->device_name);
1121 fatal_error(ctx, 0);
1122 }
2575fb04
TT
1123 retval = e2fsck_run_ext3_journal(ctx);
1124 if (retval) {
1125 com_err(ctx->program_name, retval,
3b5386dc 1126 _("while recovering ext3 journal of %s"),
2575fb04
TT
1127 ctx->device_name);
1128 fatal_error(ctx, 0);
1129 }
1130 ext2fs_close(ctx->fs);
1131 ctx->fs = 0;
b92ae153 1132 ctx->flags |= E2F_FLAG_RESTARTED;
2575fb04 1133 goto restart;
17390c04 1134 }
17390c04 1135 }
3b5386dc 1136
1b6bf175
TT
1137 /*
1138 * Check for compatibility with the feature sets. We need to
1139 * be more stringent than ext2fs_open().
1140 */
5dd8f963
TT
1141 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1142 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1b6bf175 1143 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
3b5386dc 1144 "(%s)", ctx->device_name);
f8188fff 1145 goto get_newer;
1b6bf175 1146 }
5dd8f963 1147 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1b6bf175 1148 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
3b5386dc 1149 "(%s)", ctx->device_name);
1b6bf175
TT
1150 goto get_newer;
1151 }
1917875f 1152#ifdef ENABLE_COMPRESSION
5dd8f963 1153 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1917875f
TT
1154 com_err(ctx->program_name, 0,
1155 _("Warning: compression support is experimental.\n"));
1156#endif
8fdc9985
TT
1157#ifndef ENABLE_HTREE
1158 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1159 com_err(ctx->program_name, 0,
1160 _("E2fsck not compiled with HTREE support,\n\t"
1161 "but filesystem %s has HTREE directories.\n"),
1162 ctx->device_name);
1163 goto get_newer;
1164 }
1165#endif
1166
1b6bf175
TT
1167 /*
1168 * If the user specified a specific superblock, presumably the
1169 * master superblock has been trashed. So we mark the
1170 * superblock as dirty, so it can be written out.
1171 */
1172 if (ctx->superblock &&
1173 !(ctx->options & E2F_OPT_READONLY))
1174 ext2fs_mark_super_dirty(fs);
1175
9f0288d3
TT
1176 /*
1177 * Calculate the number of filesystem blocks per pagesize. If
1178 * fs->blocksize > page_size, set the number of blocks per
1179 * pagesize to 1 to avoid division by zero errors.
1180 */
1181#ifdef _SC_PAGESIZE
1182 sysval = sysconf(_SC_PAGESIZE);
1183 if (sysval > 0)
1184 sys_page_size = sysval;
1185#endif /* _SC_PAGESIZE */
1186 ctx->blocks_per_page = sys_page_size / fs->blocksize;
1187 if (ctx->blocks_per_page == 0)
1188 ctx->blocks_per_page = 1;
1189
1b6bf175
TT
1190 ehandler_init(fs->io);
1191
1192 if (ctx->superblock)
1193 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
550a4afa 1194 ext2fs_mark_valid(fs);
1b6bf175 1195 check_super_block(ctx);
a02ce9df 1196 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1197 fatal_error(ctx, 0);
1b6bf175
TT
1198 check_if_skip(ctx);
1199 if (bad_blocks_file)
1200 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1201 else if (cflag)
4fb9d52b 1202 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
a02ce9df 1203 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1204 fatal_error(ctx, 0);
5df55d7f 1205#ifdef ENABLE_SWAPFS
1b6bf175
TT
1206 if (normalize_swapfs) {
1207 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1208 ext2fs_native_flag()) {
0c4a0726
TT
1209 fprintf(stderr, _("%s: Filesystem byte order "
1210 "already normalized.\n"), ctx->device_name);
243dc31f 1211 fatal_error(ctx, 0);
1b6bf175
TT
1212 }
1213 }
f8188fff 1214 if (swapfs) {
1b6bf175 1215 swap_filesys(ctx);
a02ce9df 1216 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1217 fatal_error(ctx, 0);
f8188fff 1218 }
5df55d7f 1219#endif
1b6bf175
TT
1220
1221 /*
1222 * Mark the system as valid, 'til proven otherwise
1223 */
1224 ext2fs_mark_valid(fs);
1225
1226 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1227 if (retval) {
1228 com_err(ctx->program_name, retval,
0c4a0726 1229 _("while reading bad blocks inode"));
1b6bf175 1230 preenhalt(ctx);
0c4a0726
TT
1231 printf(_("This doesn't bode well,"
1232 " but we'll try to go on...\n"));
1b6bf175 1233 }
08b21301 1234
5107d0d1
KS
1235 /*
1236 * Save the journal size in megabytes.
1237 * Try and use the journal size from the backup else let e2fsck
1238 * find the default journal size.
1239 */
1240 if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS)
1241 journal_size = sb->s_jnl_blocks[16] >> 20;
1242 else
1243 journal_size = -1;
1244
08b21301 1245 run_result = e2fsck_run(ctx);
5596defa 1246 e2fsck_clear_progbar(ctx);
5107d0d1
KS
1247
1248 if (ctx->flags & E2F_FLAG_JOURNAL_INODE) {
1249 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1250 if (journal_size < 1024)
1251 journal_size = ext2fs_default_journal_size(fs->super->s_blocks_count);
1252 if (journal_size < 0) {
1253 fs->super->s_feature_compat &=
1254 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
0cfce7f7 1255 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5107d0d1
KS
1256 com_err(ctx->program_name, 0,
1257 _("Couldn't determine journal size"));
1258 goto no_journal;
1259 }
1260 printf(_("Creating journal (%d blocks): "),
1261 journal_size);
1262 fflush(stdout);
1263 retval = ext2fs_add_journal_inode(fs,
1264 journal_size, 0);
1265 if (retval) {
1266 com_err("Error ", retval,
1267 _("\n\twhile trying to create journal"));
1268 goto no_journal;
1269 }
1270 printf(_(" Done.\n"));
1271 printf(_("\n*** journal has been re-created - "
1272 "filesystem is now ext3 again ***\n"));
1273 }
1274 }
1275no_journal:
1276
08b21301 1277 if (run_result == E2F_FLAG_RESTART) {
0c4a0726 1278 printf(_("Restarting e2fsck from the beginning...\n"));
1b6bf175
TT
1279 retval = e2fsck_reset_context(ctx);
1280 if (retval) {
1281 com_err(ctx->program_name, retval,
0c4a0726 1282 _("while resetting context"));
243dc31f 1283 fatal_error(ctx, 0);
1b6bf175 1284 }
73f17cfc 1285 ext2fs_close(fs);
1b6bf175
TT
1286 goto restart;
1287 }
4cae0452
TT
1288 if (run_result & E2F_FLAG_CANCEL) {
1289 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1290 ctx->device_name : ctx->filesystem_name);
1291 exit_value |= FSCK_CANCELED;
1292 }
1293 if (run_result & E2F_FLAG_ABORT)
1294 fatal_error(ctx, _("aborted"));
1b6bf175
TT
1295
1296#ifdef MTRACE
1297 mtrace_print("Cleanup");
1298#endif
1299 if (ext2fs_test_changed(fs)) {
4cae0452 1300 exit_value |= FSCK_NONDESTRUCT;
1b6bf175 1301 if (!(ctx->options & E2F_OPT_PREEN))
0c4a0726 1302 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1b6bf175 1303 ctx->device_name);
ee895139 1304 if (ctx->mount_flags & EXT2_MF_ISROOT) {
0c4a0726 1305 printf(_("%s: ***** REBOOT LINUX *****\n"),
1b6bf175 1306 ctx->device_name);
4cae0452 1307 exit_value |= FSCK_REBOOT;
1b6bf175
TT
1308 }
1309 }
eb065ccf
TT
1310 if (!ext2fs_test_valid(fs) ||
1311 ((exit_value & FSCK_CANCELED) &&
1312 (sb->s_state & EXT2_ERROR_FS))) {
d3124019
TT
1313 printf(_("\n%s: ********** WARNING: Filesystem still has "
1314 "errors **********\n\n"), ctx->device_name);
4cae0452
TT
1315 exit_value |= FSCK_UNCORRECTED;
1316 exit_value &= ~FSCK_NONDESTRUCT;
d3124019 1317 }
eb065ccf
TT
1318 if (exit_value & FSCK_CANCELED) {
1319 int allow_cancellation;
1320
1321 profile_get_boolean(ctx->profile, "options",
1322 "allow_cancellation", 0, 0,
1323 &allow_cancellation);
4cae0452 1324 exit_value &= ~FSCK_NONDESTRUCT;
eb065ccf
TT
1325 if (allow_cancellation && ext2fs_test_valid(fs) &&
1326 (sb->s_state & EXT2_VALID_FS) &&
1327 !(sb->s_state & EXT2_ERROR_FS))
1328 exit_value = 0;
1329 } else {
4cae0452 1330 show_stats(ctx);
c1637bd3
TT
1331 if (!(ctx->options & E2F_OPT_READONLY)) {
1332 if (ext2fs_test_valid(fs)) {
1333 if (!(sb->s_state & EXT2_VALID_FS))
1334 exit_value |= FSCK_NONDESTRUCT;
1335 sb->s_state = EXT2_VALID_FS;
1336 } else
1337 sb->s_state &= ~EXT2_VALID_FS;
1338 sb->s_mnt_count = 0;
1f3ad14a 1339 sb->s_lastcheck = ctx->now;
c1637bd3
TT
1340 ext2fs_mark_super_dirty(fs);
1341 }
1342 }
1b6bf175 1343
f8188fff 1344 e2fsck_write_bitmaps(ctx);
1b6bf175 1345
0628ae39
TT
1346 ext2fs_close(fs);
1347 ctx->fs = NULL;
f364093b
TT
1348 free(ctx->filesystem_name);
1349 free(ctx->journal_name);
b28a6e96 1350
8bf191e8 1351#ifdef RESOURCE_TRACK
1b6bf175
TT
1352 if (ctx->options & E2F_OPT_TIME)
1353 print_resource_track(NULL, &ctx->global_rtrack);
8bf191e8 1354#endif
b28a6e96 1355 e2fsck_free_context(ctx);
a6d8302b
TT
1356 remove_error_table(&et_ext2_error_table);
1357 remove_error_table(&et_prof_error_table);
1b6bf175
TT
1358 return exit_value;
1359}