]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/unix.c
e2fsck: read-ahead metadata during passes 1, 2, and 4
[thirdparty/e2fsprogs.git] / e2fsck / unix.c
CommitLineData
1b6bf175
TT
1/*
2 * unix.c - The unix-specific code for e2fsck
efc6f628 3 *
1b6bf175
TT
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
ebabf2ad
TT
12#define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
13
d1154eb4 14#include "config.h"
1b6bf175
TT
15#include <stdio.h>
16#ifdef HAVE_STDLIB_H
17#include <stdlib.h>
18#endif
19#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
1b6bf175 22#include <time.h>
9ecd8bec 23#ifdef HAVE_SIGNAL_H
5596defa 24#include <signal.h>
9ecd8bec 25#endif
1b6bf175
TT
26#ifdef HAVE_GETOPT_H
27#include <getopt.h>
373b8337
TT
28#else
29extern char *optarg;
30extern int optind;
1b6bf175
TT
31#endif
32#include <unistd.h>
33#ifdef HAVE_ERRNO_H
34#include <errno.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 48
864b8d4e 49#include "e2p/e2p.h"
1b6bf175 50#include "et/com_err.h"
01c196b4 51#include "e2p/e2p.h"
1b6bf175
TT
52#include "e2fsck.h"
53#include "problem.h"
54#include "../version.h"
c8b20b40 55#include "../misc/plausible.h"
1b6bf175 56
1b6bf175 57/* Command line options */
54a31a3b
TT
58static int cflag; /* check disk */
59static int show_version_only;
60static int verbose;
1b6bf175 61
54a31a3b 62static int replace_bad_blocks;
4fb9d52b 63static int keep_bad_blocks;
54a31a3b 64static char *bad_blocks_file;
1b6bf175 65
243dc31f
TT
66e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
67
5e72cdbe
TT
68#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
69int journal_enable_debug = -1;
70#endif
71
1b6bf175
TT
72static void usage(e2fsck_t ctx)
73{
74 fprintf(stderr,
1a855cb2 75 _("Usage: %s [-panyrcdfvtDFV] [-b superblock] [-B blocksize]\n"
1b6bf175 76 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
bb145b01 77 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
0684a4f3 78 "\t\t[-E extended-options] device\n"),
5596defa 79 ctx->program_name);
b55199ea 80
45ff69ff 81 fprintf(stderr, "%s", _("\nEmergency help:\n"
b55199ea
TT
82 " -p Automatic repair (no questions)\n"
83 " -n Make no changes to the filesystem\n"
84 " -y Assume \"yes\" to all questions\n"
19445ef9 85 " -c Check for bad blocks and add them to the badblock list\n"
53ef44c4 86 " -f Force checking even if filesystem is marked clean\n"));
45ff69ff 87 fprintf(stderr, "%s", _(""
b55199ea
TT
88 " -v Be verbose\n"
89 " -b superblock Use alternative superblock\n"
90 " -B blocksize Force blocksize when looking for superblock\n"
bb145b01 91 " -j external_journal Set location of the external journal\n"
b55199ea
TT
92 " -l bad_blocks_file Add to badblocks list\n"
93 " -L bad_blocks_file Set badblocks list\n"
0c4a0726 94 ));
b55199ea 95
1b6bf175
TT
96 exit(FSCK_USAGE);
97}
98
99static void show_stats(e2fsck_t ctx)
100{
101 ext2_filsys fs = ctx->fs;
f3358643 102 ext2_ino_t inodes, inodes_used;
6dc64392
VAH
103 blk64_t blocks, blocks_used;
104 unsigned int dir_links;
105 unsigned int num_files, num_links;
c0a84966 106 __u32 *mask, m;
ce44d8ca 107 int frag_percent_file, frag_percent_dir, frag_percent_total;
c0a84966 108 int i, j, printed = 0;
1b6bf175
TT
109
110 dir_links = 2 * ctx->fs_directory_count - 1;
111 num_files = ctx->fs_total_count - dir_links;
112 num_links = ctx->fs_links_count - dir_links;
113 inodes = fs->super->s_inodes_count;
114 inodes_used = (fs->super->s_inodes_count -
115 fs->super->s_free_inodes_count);
4efbac6f
VAH
116 blocks = ext2fs_blocks_count(fs->super);
117 blocks_used = (ext2fs_blocks_count(fs->super) -
118 ext2fs_free_blocks_count(fs->super));
1b6bf175 119
ce44d8ca
TT
120 frag_percent_file = (10000 * ctx->fs_fragmented) / inodes_used;
121 frag_percent_file = (frag_percent_file + 5) / 10;
122
123 frag_percent_dir = (10000 * ctx->fs_fragmented_dir) / inodes_used;
124 frag_percent_dir = (frag_percent_dir + 5) / 10;
125
126 frag_percent_total = ((10000 * (ctx->fs_fragmented +
127 ctx->fs_fragmented_dir))
128 / inodes_used);
129 frag_percent_total = (frag_percent_total + 5) / 10;
efc6f628 130
1b6bf175 131 if (!verbose) {
b0e91c89
TT
132 log_out(ctx, _("%s: %u/%u files (%0d.%d%% non-contiguous), "
133 "%llu/%llu blocks\n"),
134 ctx->device_name, inodes_used, inodes,
135 frag_percent_total / 10, frag_percent_total % 10,
136 blocks_used, blocks);
1b6bf175
TT
137 return;
138 }
c0a84966
TT
139 profile_get_boolean(ctx->profile, "options", "report_features", 0, 0,
140 &i);
141 if (verbose && i) {
142 log_out(ctx, "\nFilesystem features:");
143 mask = &ctx->fs->super->s_feature_compat;
144 for (i = 0; i < 3; i++, mask++) {
145 for (j = 0, m = 1; j < 32; j++, m <<= 1) {
146 if (*mask & m) {
147 log_out(ctx, " %s",
148 e2p_feature2string(i, m));
149 printed++;
150 }
151 }
152 }
153 if (printed == 0)
154 log_out(ctx, " (none)");
155 log_out(ctx, "\n");
156 }
157
87e56a99
TT
158 log_out(ctx, P_("\n%12u inode used (%2.2f%%, out of %u)\n",
159 "\n%12u inodes used (%2.2f%%, out of %u)\n",
b0e91c89 160 inodes_used), inodes_used,
87e56a99 161 100.0 * inodes_used / inodes, inodes);
da0fa8f4
TT
162 log_out(ctx, P_("%12u non-contiguous file (%0d.%d%%)\n",
163 "%12u non-contiguous files (%0d.%d%%)\n",
b0e91c89 164 ctx->fs_fragmented),
ce44d8ca
TT
165 ctx->fs_fragmented, frag_percent_file / 10,
166 frag_percent_file % 10);
da0fa8f4
TT
167 log_out(ctx, P_("%12u non-contiguous directory (%0d.%d%%)\n",
168 "%12u non-contiguous directories (%0d.%d%%)\n",
b0e91c89 169 ctx->fs_fragmented_dir),
ce44d8ca
TT
170 ctx->fs_fragmented_dir, frag_percent_dir / 10,
171 frag_percent_dir % 10);
da0fa8f4 172 log_out(ctx, _(" # of inodes with ind/dind/tind blocks: "
b0e91c89 173 "%u/%u/%u\n"),
0c4a0726 174 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
8da6d1a1
TT
175
176 for (j=MAX_EXTENT_DEPTH_COUNT-1; j >=0; j--)
177 if (ctx->extent_depth_count[j])
178 break;
179 if (++j) {
45ff69ff 180 log_out(ctx, "%s", _(" Extent depth histogram: "));
8da6d1a1
TT
181 for (i=0; i < j; i++) {
182 if (i)
183 fputc('/', stdout);
b0e91c89 184 log_out(ctx, "%u", ctx->extent_depth_count[i]);
8da6d1a1 185 }
b0e91c89 186 log_out(ctx, "\n");
8da6d1a1
TT
187 }
188
87e56a99
TT
189 log_out(ctx, P_("%12llu block used (%2.2f%%, out of %llu)\n",
190 "%12llu blocks used (%2.2f%%, out of %llu)\n",
191 blocks_used),
192 blocks_used, 100.0 * blocks_used / blocks, blocks);
da0fa8f4 193 log_out(ctx, P_("%12u bad block\n", "%12u bad blocks\n",
b0e91c89 194 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
da0fa8f4 195 log_out(ctx, P_("%12u large file\n", "%12u large files\n",
b0e91c89 196 ctx->large_files), ctx->large_files);
da0fa8f4 197 log_out(ctx, P_("\n%12u regular file\n", "\n%12u regular files\n",
b0e91c89 198 ctx->fs_regular_count), ctx->fs_regular_count);
da0fa8f4 199 log_out(ctx, P_("%12u directory\n", "%12u directories\n",
b0e91c89 200 ctx->fs_directory_count), ctx->fs_directory_count);
da0fa8f4
TT
201 log_out(ctx, P_("%12u character device file\n",
202 "%12u character device files\n", ctx->fs_chardev_count),
113e405b 203 ctx->fs_chardev_count);
da0fa8f4 204 log_out(ctx, P_("%12u block device file\n", "%12u block device files\n",
b0e91c89 205 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
da0fa8f4 206 log_out(ctx, P_("%12u fifo\n", "%12u fifos\n", ctx->fs_fifo_count),
113e405b 207 ctx->fs_fifo_count);
e3507739 208 log_out(ctx, P_("%12u link\n", "%12u links\n", num_links),
113e405b 209 ctx->fs_links_count - dir_links);
da0fa8f4 210 log_out(ctx, P_("%12u symbolic link", "%12u symbolic links",
b0e91c89
TT
211 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
212 log_out(ctx, P_(" (%u fast symbolic link)\n",
213 " (%u fast symbolic links)\n",
214 ctx->fs_fast_symlinks_count),
215 ctx->fs_fast_symlinks_count);
da0fa8f4
TT
216 log_out(ctx, P_("%12u socket\n", "%12u sockets\n",
217 ctx->fs_sockets_count),
113e405b 218 ctx->fs_sockets_count);
da0fa8f4 219 log_out(ctx, "------------\n");
e3507739
TT
220 log_out(ctx, P_("%12u file\n", "%12u files\n", num_files),
221 num_files);
1b6bf175
TT
222}
223
224static void check_mount(e2fsck_t ctx)
225{
226 errcode_t retval;
ee895139 227 int cont;
1b6bf175 228
ee895139
TT
229 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
230 &ctx->mount_flags);
1b6bf175
TT
231 if (retval) {
232 com_err("ext2fs_check_if_mount", retval,
0c4a0726 233 _("while determining whether %s is mounted."),
1b6bf175
TT
234 ctx->filesystem_name);
235 return;
236 }
1b6bf175 237
1b6bf175 238 /*
ae1182cb
TT
239 * If the filesystem isn't mounted, or it's the root
240 * filesystem and it's mounted read-only, and we're not doing
241 * a read/write check, then everything's fine.
1b6bf175 242 */
732e26b9 243 if ((!(ctx->mount_flags & (EXT2_MF_MOUNTED | EXT2_MF_BUSY))) ||
ee895139 244 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
ae1182cb
TT
245 (ctx->mount_flags & EXT2_MF_READONLY) &&
246 !(ctx->options & E2F_OPT_WRITECHECK)))
83e6ac84
TT
247 return;
248
78a0d2ba
TT
249 if (((ctx->options & E2F_OPT_READONLY) ||
250 ((ctx->options & E2F_OPT_FORCE) &&
251 (ctx->mount_flags & EXT2_MF_READONLY))) &&
ae1182cb 252 !(ctx->options & E2F_OPT_WRITECHECK)) {
38f160d3
BS
253 if (ctx->mount_flags & EXT2_MF_MOUNTED)
254 log_out(ctx, _("Warning! %s is mounted.\n"),
255 ctx->filesystem_name);
256 else
257 log_out(ctx, _("Warning! %s is in use.\n"),
258 ctx->filesystem_name);
1b6bf175
TT
259 return;
260 }
261
38f160d3
BS
262 if (ctx->mount_flags & EXT2_MF_MOUNTED)
263 log_out(ctx, _("%s is mounted.\n"), ctx->filesystem_name);
264 else
265 log_out(ctx, _("%s is in use.\n"), ctx->filesystem_name);
732e26b9 266 if (!ctx->interactive || ctx->mount_flags & EXT2_MF_BUSY)
243dc31f 267 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
3fcd8fe8 268 puts("\007\007\007\007");
45ff69ff 269 log_out(ctx, "%s", _("\n\nWARNING!!! "
b0e91c89
TT
270 "The filesystem is mounted. "
271 "If you continue you ***WILL***\n"
272 "cause ***SEVERE*** filesystem damage.\n\n"));
3fcd8fe8 273 puts("\007\007\007");
b0e91c89 274 cont = ask_yn(ctx, _("Do you really want to continue"), 0);
1b6bf175 275 if (!cont) {
45ff69ff 276 printf("%s", _("check aborted.\n"));
1b6bf175
TT
277 exit (0);
278 }
279 return;
280}
281
54434927 282static int is_on_batt(void)
015b03df
TT
283{
284 FILE *f;
c07f9f26
TT
285 DIR *d;
286 char tmp[80], tmp2[80], fname[80];
015b03df 287 unsigned int acflag;
c07f9f26 288 struct dirent* de;
015b03df 289
ad880a0e
TT
290 f = fopen("/sys/class/power_supply/AC/online", "r");
291 if (f) {
e64e6761 292 if (fscanf(f, "%u\n", &acflag) == 1) {
ad880a0e
TT
293 fclose(f);
294 return (!acflag);
295 }
296 fclose(f);
297 }
015b03df
TT
298 f = fopen("/proc/apm", "r");
299 if (f) {
efc6f628 300 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
015b03df
TT
301 acflag = 1;
302 fclose(f);
303 return (acflag != 1);
304 }
c07f9f26 305 d = opendir("/proc/acpi/ac_adapter");
ecd0d8fe 306 if (d) {
9214dccb 307 while ((de=readdir(d)) != NULL) {
ecd0d8fe
TT
308 if (!strncmp(".", de->d_name, 1))
309 continue;
efc6f628 310 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
ecd0d8fe
TT
311 de->d_name);
312 f = fopen(fname, "r");
313 if (!f)
314 continue;
315 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
316 tmp[0] = 0;
317 fclose(f);
318 if (strncmp(tmp, "off-line", 8) == 0) {
319 closedir(d);
320 return 1;
321 }
c07f9f26 322 }
4b13704c 323 closedir(d);
ecd0d8fe 324 }
015b03df
TT
325 return 0;
326}
327
1b6bf175
TT
328/*
329 * This routine checks to see if a filesystem can be skipped; if so,
330 * it will exit with E2FSCK_OK. Under some conditions it will print a
331 * message explaining why a check is being forced.
332 */
333static void check_if_skip(e2fsck_t ctx)
334{
335 ext2_filsys fs = ctx->fs;
a3efe484 336 struct problem_context pctx;
1b6bf175 337 const char *reason = NULL;
b6a0807b 338 unsigned int reason_arg = 0;
6de289cb 339 long next_check;
015b03df 340 int batt = is_on_batt();
a5f37a9b 341 int defer_check_on_battery;
177839e2 342 int broken_system_clock;
60702c26 343 time_t lastcheck;
a5f37a9b 344
7664c753
TT
345 if (ctx->flags & E2F_FLAG_PROBLEMS_FIXED)
346 return;
347
177839e2
TT
348 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
349 0, 0, &broken_system_clock);
350 if (ctx->flags & E2F_FLAG_TIME_INSANE)
351 broken_system_clock = 1;
a5f37a9b 352 profile_get_boolean(ctx->profile, "options",
efc6f628 353 "defer_check_on_battery", 0, 1,
a5f37a9b
TT
354 &defer_check_on_battery);
355 if (!defer_check_on_battery)
356 batt = 0;
357
1a855cb2 358 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || cflag)
1b6bf175 359 return;
efc6f628 360
71873b17
BS
361 if (ctx->options & E2F_OPT_JOURNAL_ONLY)
362 goto skip;
363
60702c26
TT
364 lastcheck = fs->super->s_lastcheck;
365 if (lastcheck > ctx->now)
366 lastcheck -= ctx->time_fudge;
550a4afa
TT
367 if ((fs->super->s_state & EXT2_ERROR_FS) ||
368 !ext2fs_test_valid(fs))
b6a0807b 369 reason = _(" contains a file system with errors");
24fc5032 370 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
b6a0807b 371 reason = _(" was not cleanly unmounted");
0c37f456
TT
372 else if (check_backup_super_block(ctx))
373 reason = _(" primary superblock features different from backup");
bc57f153 374 else if ((fs->super->s_max_mnt_count > 0) &&
17390c04 375 (fs->super->s_mnt_count >=
b6a0807b
TT
376 (unsigned) fs->super->s_max_mnt_count)) {
377 reason = _(" has been mounted %u times without being checked");
378 reason_arg = fs->super->s_mnt_count;
efc6f628 379 if (batt && (fs->super->s_mnt_count <
015b03df
TT
380 (unsigned) fs->super->s_max_mnt_count*2))
381 reason = 0;
177839e2
TT
382 } else if (!broken_system_clock && fs->super->s_checkinterval &&
383 (ctx->now < lastcheck)) {
68eb092d
TT
384 reason = _(" has filesystem last checked time in the future");
385 if (batt)
386 reason = 0;
177839e2 387 } else if (!broken_system_clock && fs->super->s_checkinterval &&
efc6f628 388 ((ctx->now - lastcheck) >=
2acad6b4 389 ((time_t) fs->super->s_checkinterval))) {
b6a0807b 390 reason = _(" has gone %u days without being checked");
1f3ad14a 391 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
efc6f628 392 if (batt && ((ctx->now - fs->super->s_lastcheck) <
54434927 393 fs->super->s_checkinterval*2))
015b03df 394 reason = 0;
b6a0807b 395 }
1b6bf175 396 if (reason) {
b0e91c89
TT
397 log_out(ctx, "%s", ctx->device_name);
398 log_out(ctx, reason, reason_arg);
45ff69ff 399 log_out(ctx, "%s", _(", check forced.\n"));
1b6bf175
TT
400 return;
401 }
a3efe484
TT
402
403 /*
404 * Update the global counts from the block group counts. This
405 * is needed since modern kernels don't update the global
406 * counts so as to avoid locking the entire file system. So
407 * if the filesystem is not unmounted cleanly, the global
408 * counts may not be accurate. Update them here if we can,
409 * for the benefit of users who might examine the file system
410 * using dumpe2fs. (This is for cosmetic reasons only.)
411 */
412 clear_problem_context(&pctx);
413 pctx.ino = fs->super->s_free_inodes_count;
414 pctx.ino2 = ctx->free_inodes;
415 if ((pctx.ino != pctx.ino2) &&
416 !(ctx->options & E2F_OPT_READONLY) &&
417 fix_problem(ctx, PR_0_FREE_INODE_COUNT, &pctx)) {
418 fs->super->s_free_inodes_count = ctx->free_inodes;
419 ext2fs_mark_super_dirty(fs);
420 }
421 clear_problem_context(&pctx);
422 pctx.blk = ext2fs_free_blocks_count(fs->super);
423 pctx.blk2 = ctx->free_blocks;
424 if ((pctx.blk != pctx.blk2) &&
425 !(ctx->options & E2F_OPT_READONLY) &&
426 fix_problem(ctx, PR_0_FREE_BLOCK_COUNT, &pctx)) {
427 ext2fs_free_blocks_count_set(fs->super, ctx->free_blocks);
428 ext2fs_mark_super_dirty(fs);
429 }
430
431 /* Print the summary message when we're skipping a full check */
b0e91c89
TT
432 log_out(ctx, _("%s: clean, %u/%u files, %llu/%llu blocks"),
433 ctx->device_name,
434 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
435 fs->super->s_inodes_count,
436 ext2fs_blocks_count(fs->super) -
437 ext2fs_free_blocks_count(fs->super),
438 ext2fs_blocks_count(fs->super));
6de289cb
TT
439 next_check = 100000;
440 if (fs->super->s_max_mnt_count > 0) {
441 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
efc6f628 442 if (next_check <= 0)
6de289cb
TT
443 next_check = 1;
444 }
177839e2 445 if (!broken_system_clock && fs->super->s_checkinterval &&
1f3ad14a 446 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
6de289cb
TT
447 next_check = 1;
448 if (next_check <= 5) {
bc3392c3 449 if (next_check == 1) {
efc6f628 450 if (batt)
45ff69ff
AD
451 log_out(ctx, "%s",
452 _(" (check deferred; on battery)"));
bc3392c3 453 else
45ff69ff
AD
454 log_out(ctx, "%s",
455 _(" (check after next mount)"));
bc3392c3 456 } else
45ff69ff
AD
457 log_out(ctx, _(" (check in %ld mounts)"),
458 next_check);
6de289cb 459 }
b0e91c89 460 log_out(ctx, "\n");
71873b17 461skip:
ebdf895b 462 ext2fs_close_free(&ctx->fs);
6d222f32 463 e2fsck_free_context(ctx);
1b6bf175
TT
464 exit(FSCK_OK);
465}
466
efac9a1b
TT
467/*
468 * For completion notice
469 */
5596defa
TT
470struct percent_tbl {
471 int max_pass;
472 int table[32];
473};
f404167d 474static struct percent_tbl e2fsck_tbl = {
5596defa
TT
475 5, { 0, 70, 90, 92, 95, 100 }
476};
54a31a3b 477static char bar[128], spaces[128];
5596defa
TT
478
479static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
480 int max)
481{
482 float percent;
efc6f628 483
5596defa
TT
484 if (pass <= 0)
485 return 0.0;
b8d164cd 486 if (pass > tbl->max_pass || max == 0)
5596defa
TT
487 return 100.0;
488 percent = ((float) curr) / ((float) max);
489 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
490 + tbl->table[pass-1]);
491}
492
f404167d 493void e2fsck_clear_progbar(e2fsck_t ctx)
5596defa
TT
494{
495 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
496 return;
efc6f628 497
54a31a3b
TT
498 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
499 ctx->stop_meta);
bc34d6be 500 fflush(stdout);
5596defa
TT
501 ctx->flags &= ~E2F_FLAG_PROG_BAR;
502}
503
520ead37 504int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
b0700a1b
TT
505 unsigned int dpynum)
506{
507 static const char spinner[] = "\\|/-";
b0700a1b 508 int i;
54434927 509 unsigned int tick;
b0700a1b
TT
510 struct timeval tv;
511 int dpywidth;
9214dccb 512 int fixed_percent;
b0700a1b
TT
513
514 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
515 return 0;
516
517 /*
518 * Calculate the new progress position. If the
519 * percentage hasn't changed, then we skip out right
efc6f628 520 * away.
b0700a1b 521 */
9214dccb
TT
522 fixed_percent = (int) ((10 * percent) + 0.5);
523 if (ctx->progress_last_percent == fixed_percent)
b0700a1b 524 return 0;
9214dccb 525 ctx->progress_last_percent = fixed_percent;
b0700a1b
TT
526
527 /*
528 * If we've already updated the spinner once within
529 * the last 1/8th of a second, no point doing it
530 * again.
531 */
532 gettimeofday(&tv, NULL);
533 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
534 if ((tick == ctx->progress_last_time) &&
9214dccb 535 (fixed_percent != 0) && (fixed_percent != 1000))
b0700a1b
TT
536 return 0;
537 ctx->progress_last_time = tick;
538
539 /*
540 * Advance the spinner, and note that the progress bar
541 * will be on the screen
542 */
543 ctx->progress_pos = (ctx->progress_pos+1) & 3;
544 ctx->flags |= E2F_FLAG_PROG_BAR;
545
546 dpywidth = 66 - strlen(label);
547 dpywidth = 8 * (dpywidth / 8);
548 if (dpynum)
549 dpywidth -= 8;
550
551 i = ((percent * dpywidth) + 50) / 100;
54a31a3b
TT
552 printf("%s%s: |%s%s", ctx->start_meta, label,
553 bar + (sizeof(bar) - (i+1)),
b0700a1b 554 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
9214dccb 555 if (fixed_percent == 1000)
b0700a1b
TT
556 fputc('|', stdout);
557 else
558 fputc(spinner[ctx->progress_pos & 3], stdout);
9214dccb 559 printf(" %4.1f%% ", percent);
b0700a1b 560 if (dpynum)
9214dccb 561 printf("%u\r", dpynum);
b0700a1b 562 else
9214dccb
TT
563 fputs(" \r", stdout);
564 fputs(ctx->stop_meta, stdout);
efc6f628 565
9214dccb 566 if (fixed_percent == 1000)
b0700a1b
TT
567 e2fsck_clear_progbar(ctx);
568 fflush(stdout);
569
570 return 0;
571}
572
efac9a1b
TT
573static int e2fsck_update_progress(e2fsck_t ctx, int pass,
574 unsigned long cur, unsigned long max)
575{
1dc506cb 576 char buf[1024];
5596defa 577 float percent;
f75c28de
TT
578
579 if (pass == 0)
580 return 0;
efc6f628 581
efac9a1b 582 if (ctx->progress_fd) {
efc6f628 583 snprintf(buf, sizeof(buf), "%d %lu %lu %s\n",
1dc506cb 584 pass, cur, max, ctx->device_name);
b0258cbc 585 write_all(ctx->progress_fd, buf, strlen(buf));
efac9a1b 586 } else {
e4c8e885 587 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
b0700a1b
TT
588 e2fsck_simple_progress(ctx, ctx->device_name,
589 percent, 0);
efac9a1b
TT
590 }
591 return 0;
592}
1b6bf175
TT
593
594#define PATH_SET "PATH=/sbin"
595
fe65f1ec
ES
596/*
597 * Make sure 0,1,2 file descriptors are open, so that we don't open
598 * the filesystem using the same file descriptor as stdout or stderr.
599 */
5ba23cb1 600static void reserve_stdio_fds(void)
24fc5032 601{
fe65f1ec 602 int fd = 0;
24fc5032 603
fe65f1ec 604 while (fd <= 2) {
24fc5032 605 fd = open("/dev/null", O_RDWR);
75d83bec 606 if (fd < 0) {
0c4a0726
TT
607 fprintf(stderr, _("ERROR: Couldn't open "
608 "/dev/null (%s)\n"),
75d83bec
TT
609 strerror(errno));
610 break;
611 }
24fc5032 612 }
24fc5032
TT
613}
614
9ecd8bec 615#ifdef HAVE_SIGNAL_H
54434927 616static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
5596defa 617{
243dc31f 618 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
619
620 if (!ctx)
621 return;
622
623 ctx->progress = e2fsck_update_progress;
5596defa
TT
624}
625
54434927 626static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
5596defa 627{
243dc31f 628 e2fsck_t ctx = e2fsck_global_ctx;
5596defa
TT
629
630 if (!ctx)
631 return;
632
633 e2fsck_clear_progbar(ctx);
634 ctx->progress = 0;
635}
4cae0452 636
54434927 637static void signal_cancel(int sig EXT2FS_ATTR((unused)))
4cae0452
TT
638{
639 e2fsck_t ctx = e2fsck_global_ctx;
640
641 if (!ctx)
642 exit(FSCK_CANCELED);
643
644 ctx->flags |= E2F_FLAG_CANCEL;
645}
9ecd8bec 646#endif
5596defa 647
0684a4f3
TT
648static void parse_extended_opts(e2fsck_t ctx, const char *opts)
649{
650 char *buf, *token, *next, *p, *arg;
f364093b 651 int ea_ver;
0684a4f3 652 int extended_usage = 0;
a5abfe03 653 unsigned long long reada_kb;
0684a4f3 654
f364093b 655 buf = string_copy(ctx, opts, 0);
0684a4f3
TT
656 for (token = buf; token && *token; token = next) {
657 p = strchr(token, ',');
658 next = 0;
659 if (p) {
660 *p = 0;
661 next = p+1;
cae542ce 662 }
0684a4f3
TT
663 arg = strchr(token, '=');
664 if (arg) {
665 *arg = 0;
666 arg++;
667 }
668 if (strcmp(token, "ea_ver") == 0) {
669 if (!arg) {
670 extended_usage++;
671 continue;
672 }
673 ea_ver = strtoul(arg, &p, 0);
674 if (*p ||
675 ((ea_ver != 1) && (ea_ver != 2))) {
45ff69ff 676 fprintf(stderr, "%s",
0684a4f3
TT
677 _("Invalid EA version.\n"));
678 extended_usage++;
679 continue;
680 }
681 ctx->ext_attr_ver = ea_ver;
a5abfe03
DW
682 } else if (strcmp(token, "readahead_kb") == 0) {
683 reada_kb = strtoull(arg, &p, 0);
684 if (*p) {
685 fprintf(stderr, "%s",
686 _("Invalid readahead buffer size.\n"));
687 extended_usage++;
688 continue;
689 }
690 ctx->readahead_kb = reada_kb;
63b5e354
TT
691 } else if (strcmp(token, "fragcheck") == 0) {
692 ctx->options |= E2F_OPT_FRAGCHECK;
693 continue;
71873b17
BS
694 } else if (strcmp(token, "journal_only") == 0) {
695 if (arg) {
696 extended_usage++;
697 continue;
698 }
699 ctx->options |= E2F_OPT_JOURNAL_ONLY;
efa1a355
LC
700 } else if (strcmp(token, "discard") == 0) {
701 ctx->options |= E2F_OPT_DISCARD;
702 continue;
703 } else if (strcmp(token, "nodiscard") == 0) {
704 ctx->options &= ~E2F_OPT_DISCARD;
705 continue;
b0e91c89
TT
706 } else if (strcmp(token, "log_filename") == 0) {
707 if (!arg)
708 extended_usage++;
709 else
710 ctx->log_fn = string_copy(ctx, arg, 0);
711 continue;
bb145b01
TT
712 } else {
713 fprintf(stderr, _("Unknown extended option: %s\n"),
714 token);
0684a4f3 715 extended_usage++;
bb145b01 716 }
0684a4f3 717 }
cae542ce
BB
718 free(buf);
719
0684a4f3 720 if (extended_usage) {
bb145b01
TT
721 fputs(("\nExtended options are separated by commas, "
722 "and may take an argument which\n"
723 "is set off by an equals ('=') sign. "
63b5e354
TT
724 "Valid extended options are:\n"), stderr);
725 fputs(("\tea_ver=<ea_version (1 or 2)>\n"), stderr);
726 fputs(("\tfragcheck\n"), stderr);
71873b17 727 fputs(("\tjournal_only\n"), stderr);
efa1a355
LC
728 fputs(("\tdiscard\n"), stderr);
729 fputs(("\tnodiscard\n"), stderr);
a5abfe03 730 fputs(("\treadahead_kb=<buffer size>\n"), stderr);
63b5e354 731 fputc('\n', stderr);
0684a4f3
TT
732 exit(1);
733 }
cae542ce 734}
0684a4f3 735
f5f14fcf
TT
736static void syntax_err_report(const char *filename, long err, int line_num)
737{
efc6f628 738 fprintf(stderr,
f5f14fcf
TT
739 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
740 filename, line_num, error_message(err));
741 exit(FSCK_ERROR);
742}
743
abcfdfda 744static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
0684a4f3 745
1b6bf175
TT
746static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
747{
748 int flush = 0;
ae8160e6 749 int c, fd;
1b6bf175
TT
750#ifdef MTRACE
751 extern void *mallwatch;
752#endif
1b6bf175
TT
753 e2fsck_t ctx;
754 errcode_t retval;
9ecd8bec 755#ifdef HAVE_SIGNAL_H
5596defa 756 struct sigaction sa;
9ecd8bec 757#endif
0684a4f3 758 char *extended_opts = 0;
5dd2a6e0 759 char *cp;
5845caa4
TT
760 int res; /* result of sscanf */
761#ifdef CONFIG_JBD_DEBUG
762 char *jbd_debug;
763#endif
a5abfe03 764 unsigned long long phys_mem_kb;
1b6bf175
TT
765
766 retval = e2fsck_allocate_context(&ctx);
767 if (retval)
768 return retval;
769
770 *ret_ctx = ctx;
dde6333b 771 e2fsck_global_ctx = ctx;
1b6bf175 772
908b785c
TT
773 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
774 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
aeb933b3 775 if (getenv("E2FSCK_FORCE_INTERACTIVE") || (isatty(0) && isatty(1))) {
54a31a3b
TT
776 ctx->interactive = 1;
777 } else {
778 ctx->start_meta[0] = '\001';
779 ctx->stop_meta[0] = '\002';
780 }
781 memset(bar, '=', sizeof(bar)-1);
782 memset(spaces, ' ', sizeof(spaces)-1);
a6d8302b
TT
783 add_error_table(&et_ext2_error_table);
784 add_error_table(&et_prof_error_table);
f364093b 785 blkid_get_cache(&ctx->blkid, NULL);
efc6f628 786
1b6bf175
TT
787 if (argc && *argv)
788 ctx->program_name = *argv;
789 else
790 ctx->program_name = "e2fsck";
efa1a355 791
a5abfe03
DW
792 phys_mem_kb = get_memory_size() / 1024;
793 ctx->readahead_kb = ~0ULL;
4fb9d52b 794 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
1b6bf175 795 switch (c) {
efac9a1b
TT
796 case 'C':
797 ctx->progress = e2fsck_update_progress;
5845caa4
TT
798 res = sscanf(optarg, "%d", &ctx->progress_fd);
799 if (res != 1)
800 goto sscanf_err;
801
be62523b
TT
802 if (ctx->progress_fd < 0) {
803 ctx->progress = 0;
804 ctx->progress_fd = ctx->progress_fd * -1;
805 }
fc9a69ca
TT
806 if (!ctx->progress_fd)
807 break;
ae8160e6
TT
808 /* Validate the file descriptor to avoid disasters */
809 fd = dup(ctx->progress_fd);
810 if (fd < 0) {
811 fprintf(stderr,
812 _("Error validating file descriptor %d: %s\n"),
813 ctx->progress_fd,
814 error_message(errno));
815 fatal_error(ctx,
816 _("Invalid completion information file descriptor"));
817 } else
818 close(fd);
efac9a1b 819 break;
850d05e9
TT
820 case 'D':
821 ctx->options |= E2F_OPT_COMPRESS_DIRS;
822 break;
0684a4f3
TT
823 case 'E':
824 extended_opts = optarg;
825 break;
1b6bf175
TT
826 case 'p':
827 case 'a':
8161a748
TT
828 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
829 conflict_opt:
efc6f628 830 fatal_error(ctx,
f4b6d2a0 831 _("Only one of the options -p/-a, -n or -y may be specified."));
8161a748 832 }
1b6bf175 833 ctx->options |= E2F_OPT_PREEN;
1b6bf175
TT
834 break;
835 case 'n':
8161a748
TT
836 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
837 goto conflict_opt;
1b6bf175 838 ctx->options |= E2F_OPT_NO;
1b6bf175
TT
839 break;
840 case 'y':
8161a748
TT
841 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
842 goto conflict_opt;
1b6bf175 843 ctx->options |= E2F_OPT_YES;
1b6bf175
TT
844 break;
845 case 't':
8bf191e8 846#ifdef RESOURCE_TRACK
1b6bf175
TT
847 if (ctx->options & E2F_OPT_TIME)
848 ctx->options |= E2F_OPT_TIME2;
849 else
850 ctx->options |= E2F_OPT_TIME;
8bf191e8 851#else
0c4a0726
TT
852 fprintf(stderr, _("The -t option is not "
853 "supported on this version of e2fsck.\n"));
8bf191e8 854#endif
1b6bf175
TT
855 break;
856 case 'c':
3ed57c27
TT
857 if (cflag++)
858 ctx->options |= E2F_OPT_WRITECHECK;
1b6bf175
TT
859 ctx->options |= E2F_OPT_CHECKBLOCKS;
860 break;
861 case 'r':
862 /* What we do by default, anyway! */
863 break;
864 case 'b':
6dc64392 865 res = sscanf(optarg, "%llu", &ctx->use_superblock);
5845caa4
TT
866 if (res != 1)
867 goto sscanf_err;
ae6cdcf7 868 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
1b6bf175
TT
869 break;
870 case 'B':
f1a1761d 871 ctx->blocksize = atoi(optarg);
1b6bf175
TT
872 break;
873 case 'I':
5845caa4
TT
874 res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
875 if (res != 1)
876 goto sscanf_err;
1b6bf175 877 break;
adee8d75 878 case 'j':
b95ca928
TT
879 ctx->journal_name = blkid_get_devname(ctx->blkid,
880 optarg, NULL);
881 if (!ctx->journal_name) {
882 com_err(ctx->program_name, 0,
883 _("Unable to resolve '%s'"),
884 optarg);
885 fatal_error(ctx, 0);
886 }
adee8d75 887 break;
1b6bf175 888 case 'P':
5845caa4
TT
889 res = sscanf(optarg, "%d", &ctx->process_inode_size);
890 if (res != 1)
891 goto sscanf_err;
1b6bf175
TT
892 break;
893 case 'L':
894 replace_bad_blocks++;
895 case 'l':
f0131bdc
DW
896 if (bad_blocks_file)
897 free(bad_blocks_file);
f364093b 898 bad_blocks_file = string_copy(ctx, optarg, 0);
1b6bf175
TT
899 break;
900 case 'd':
901 ctx->options |= E2F_OPT_DEBUG;
902 break;
903 case 'f':
d37066a9 904 ctx->options |= E2F_OPT_FORCE;
1b6bf175
TT
905 break;
906 case 'F':
1b6bf175 907 flush = 1;
1b6bf175
TT
908 break;
909 case 'v':
910 verbose = 1;
911 break;
912 case 'V':
913 show_version_only = 1;
914 break;
915#ifdef MTRACE
916 case 'M':
917 mallwatch = (void *) strtol(optarg, NULL, 0);
918 break;
919#endif
920 case 'N':
a2447f8c 921 ctx->device_name = string_copy(ctx, optarg, 0);
1b6bf175 922 break;
4fb9d52b
TT
923 case 'k':
924 keep_bad_blocks++;
bc69f82d 925 break;
1b6bf175
TT
926 default:
927 usage(ctx);
928 }
929 if (show_version_only)
930 return 0;
931 if (optind != argc - 1)
932 usage(ctx);
298c9c2f
TT
933 if ((ctx->options & E2F_OPT_NO) &&
934 (ctx->options & E2F_OPT_COMPRESS_DIRS)) {
45ff69ff 935 com_err(ctx->program_name, 0, "%s",
298c9c2f
TT
936 _("The -n and -D options are incompatible."));
937 fatal_error(ctx, 0);
938 }
939 if ((ctx->options & E2F_OPT_NO) && cflag) {
45ff69ff 940 com_err(ctx->program_name, 0, "%s",
298c9c2f
TT
941 _("The -n and -c options are incompatible."));
942 fatal_error(ctx, 0);
943 }
944 if ((ctx->options & E2F_OPT_NO) && bad_blocks_file) {
45ff69ff 945 com_err(ctx->program_name, 0, "%s",
298c9c2f
TT
946 _("The -n and -l/-L options are incompatible."));
947 fatal_error(ctx, 0);
948 }
949 if (ctx->options & E2F_OPT_NO)
1b6bf175 950 ctx->options |= E2F_OPT_READONLY;
49a7360b 951
2e8ca9a2 952 ctx->io_options = strchr(argv[optind], '?');
efc6f628 953 if (ctx->io_options)
2e8ca9a2 954 *ctx->io_options++ = 0;
f364093b 955 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
817e49e3 956 if (!ctx->filesystem_name) {
efc6f628 957 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
817e49e3
TT
958 argv[optind]);
959 fatal_error(ctx, 0);
960 }
0684a4f3
TT
961 if (extended_opts)
962 parse_extended_opts(ctx, extended_opts);
1017f651 963
5dd2a6e0
TT
964 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
965 config_fn[0] = cp;
f5f14fcf 966 profile_set_syntax_err_cb(syntax_err_report);
1017f651
TT
967 profile_init(config_fn, &ctx->profile);
968
c0a84966
TT
969 profile_get_boolean(ctx->profile, "options", "report_time", 0, 0,
970 &c);
971 if (c)
972 ctx->options |= E2F_OPT_TIME | E2F_OPT_TIME2;
973 profile_get_boolean(ctx->profile, "options", "report_verbose", 0, 0,
974 &c);
975 if (c)
976 verbose = 1;
977
a5abfe03
DW
978 if (ctx->readahead_kb == ~0ULL) {
979 profile_get_integer(ctx->profile, "options",
980 "readahead_mem_pct", 0, -1, &c);
981 if (c >= 0 && c <= 100)
982 ctx->readahead_kb = phys_mem_kb * c / 100;
983 profile_get_integer(ctx->profile, "options",
984 "readahead_kb", 0, -1, &c);
985 if (c >= 0)
986 ctx->readahead_kb = c;
987 if (ctx->readahead_kb != ~0ULL &&
988 ctx->readahead_kb > phys_mem_kb)
989 ctx->readahead_kb = phys_mem_kb;
990 }
991
f0fe5dae
LC
992 /* Turn off discard in read-only mode */
993 if ((ctx->options & E2F_OPT_NO) &&
994 (ctx->options & E2F_OPT_DISCARD))
995 ctx->options &= ~E2F_OPT_DISCARD;
996
28ffafb0 997 if (flush) {
4ea7bd04 998 fd = open(ctx->filesystem_name, O_RDONLY, 0);
1b6bf175 999 if (fd < 0) {
0c4a0726
TT
1000 com_err("open", errno,
1001 _("while opening %s for flushing"),
1b6bf175 1002 ctx->filesystem_name);
243dc31f 1003 fatal_error(ctx, 0);
1b6bf175 1004 }
5df55d7f 1005 if ((retval = ext2fs_sync_device(fd, 1))) {
5ba23cb1 1006 com_err("ext2fs_sync_device", retval,
0c4a0726 1007 _("while trying to flush %s"),
1b6bf175 1008 ctx->filesystem_name);
243dc31f 1009 fatal_error(ctx, 0);
1b6bf175
TT
1010 }
1011 close(fd);
1b6bf175 1012 }
3ed57c27 1013 if (cflag && bad_blocks_file) {
45ff69ff
AD
1014 fprintf(stderr, "%s", _("The -c and the -l/-L options may not "
1015 "be both used at the same time.\n"));
3ed57c27
TT
1016 exit(FSCK_USAGE);
1017 }
9ecd8bec 1018#ifdef HAVE_SIGNAL_H
5596defa
TT
1019 /*
1020 * Set up signal action
1021 */
1022 memset(&sa, 0, sizeof(struct sigaction));
850d05e9
TT
1023 sa.sa_handler = signal_cancel;
1024 sigaction(SIGINT, &sa, 0);
1025 sigaction(SIGTERM, &sa, 0);
5596defa
TT
1026#ifdef SA_RESTART
1027 sa.sa_flags = SA_RESTART;
1028#endif
5596defa
TT
1029 sa.sa_handler = signal_progress_on;
1030 sigaction(SIGUSR1, &sa, 0);
1031 sa.sa_handler = signal_progress_off;
1032 sigaction(SIGUSR2, &sa, 0);
9ecd8bec 1033#endif
6d222f32
TT
1034
1035 /* Update our PATH to include /sbin if we need to run badblocks */
1036 if (cflag) {
1037 char *oldpath = getenv("PATH");
642935c0
TT
1038 char *newpath;
1039 int len = sizeof(PATH_SET) + 1;
1040
1041 if (oldpath)
1042 len += strlen(oldpath);
1043
1044 newpath = malloc(len);
1045 if (!newpath)
1046 fatal_error(ctx, "Couldn't malloc() newpath");
1047 strcpy(newpath, PATH_SET);
1048
6d222f32 1049 if (oldpath) {
642935c0
TT
1050 strcat(newpath, ":");
1051 strcat(newpath, oldpath);
1052 }
1053 putenv(newpath);
6d222f32 1054 }
5e72cdbe 1055#ifdef CONFIG_JBD_DEBUG
5845caa4 1056 jbd_debug = getenv("E2FSCK_JBD_DEBUG");
414025e5 1057 if (jbd_debug) {
5845caa4
TT
1058 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
1059 if (res != 1) {
1060 fprintf(stderr,
1061 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
1062 jbd_debug);
1063 exit (1);
1064 }
414025e5 1065 }
5e72cdbe 1066#endif
1b6bf175 1067 return 0;
5845caa4
TT
1068
1069sscanf_err:
1070 fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
1071 c, optarg);
1072 exit (1);
1b6bf175
TT
1073}
1074
60663890
TT
1075static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr,
1076 ext2_filsys *ret_fs)
1077{
1078 errcode_t retval;
1079
1080 *ret_fs = NULL;
1081 if (ctx->superblock && ctx->blocksize) {
1082 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1083 flags, ctx->superblock, ctx->blocksize,
1084 io_ptr, ret_fs);
1085 } else if (ctx->superblock) {
1086 int blocksize;
1087 for (blocksize = EXT2_MIN_BLOCK_SIZE;
1088 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
1089 if (*ret_fs) {
1090 ext2fs_free(*ret_fs);
1091 *ret_fs = NULL;
1092 }
1093 retval = ext2fs_open2(ctx->filesystem_name,
1094 ctx->io_options, flags,
1095 ctx->superblock, blocksize,
1096 io_ptr, ret_fs);
1097 if (!retval)
1098 break;
1099 }
1100 } else
1101 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1102 flags, 0, 0, io_ptr, ret_fs);
830b44f4 1103
93d0db34
TT
1104 if (retval == 0) {
1105 (*ret_fs)->priv_data = ctx;
830b44f4
TT
1106 e2fsck_set_bitmap_type(*ret_fs, EXT2FS_BMAP64_RBTREE,
1107 "default", NULL);
93d0db34 1108 }
60663890
TT
1109 return retval;
1110}
1111
1b6bf175
TT
1112static const char *my_ver_string = E2FSPROGS_VERSION;
1113static const char *my_ver_date = E2FSPROGS_DATE;
49a7360b 1114
974d57d3 1115static errcode_t e2fsck_check_mmp(ext2_filsys fs, e2fsck_t ctx)
0f5eba75
AD
1116{
1117 struct mmp_struct *mmp_s;
1118 unsigned int mmp_check_interval;
1119 errcode_t retval = 0;
1120 struct problem_context pctx;
1121 unsigned int wait_time = 0;
1122
1123 clear_problem_context(&pctx);
1124 if (fs->mmp_buf == NULL) {
1125 retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
1126 if (retval)
1127 goto check_error;
1128 }
1129
1130 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
1131 if (retval)
1132 goto check_error;
1133
1134 mmp_s = fs->mmp_buf;
1135
1136 mmp_check_interval = fs->super->s_mmp_update_interval;
1137 if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
1138 mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
1139
1140 /*
1141 * If check_interval in MMP block is larger, use that instead of
1142 * check_interval from the superblock.
1143 */
1144 if (mmp_s->mmp_check_interval > mmp_check_interval)
1145 mmp_check_interval = mmp_s->mmp_check_interval;
1146
1147 wait_time = mmp_check_interval * 2 + 1;
1148
1149 if (mmp_s->mmp_seq == EXT4_MMP_SEQ_CLEAN)
1150 retval = 0;
1151 else if (mmp_s->mmp_seq == EXT4_MMP_SEQ_FSCK)
1152 retval = EXT2_ET_MMP_FSCK_ON;
1153 else if (mmp_s->mmp_seq > EXT4_MMP_SEQ_MAX)
1154 retval = EXT2_ET_MMP_UNKNOWN_SEQ;
1155
1156 if (retval)
1157 goto check_error;
1158
1159 /* Print warning if e2fck will wait for more than 20 secs. */
1160 if (verbose || wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4) {
b0e91c89
TT
1161 log_out(ctx, _("MMP interval is %u seconds and total wait "
1162 "time is %u seconds. Please wait...\n"),
0f5eba75
AD
1163 mmp_check_interval, wait_time * 2);
1164 }
1165
1166 return 0;
1167
1168check_error:
1169
1170 if (retval == EXT2_ET_MMP_BAD_BLOCK) {
1171 if (fix_problem(ctx, PR_0_MMP_INVALID_BLK, &pctx)) {
1172 fs->super->s_mmp_block = 0;
1173 ext2fs_mark_super_dirty(fs);
1174 retval = 0;
1175 }
1176 } else if (retval == EXT2_ET_MMP_FAILED) {
45ff69ff 1177 com_err(ctx->program_name, retval, "%s",
0f5eba75
AD
1178 _("while checking MMP block"));
1179 dump_mmp_msg(fs->mmp_buf, NULL);
1180 } else if (retval == EXT2_ET_MMP_FSCK_ON ||
1181 retval == EXT2_ET_MMP_UNKNOWN_SEQ) {
45ff69ff 1182 com_err(ctx->program_name, retval, "%s",
0f5eba75
AD
1183 _("while checking MMP block"));
1184 dump_mmp_msg(fs->mmp_buf,
1185 _("If you are sure the filesystem is not "
1186 "in use on any node, run:\n"
1187 "'tune2fs -f -E clear_mmp {device}'\n"));
1188 } else if (retval == EXT2_ET_MMP_MAGIC_INVALID) {
1189 if (fix_problem(ctx, PR_0_MMP_INVALID_MAGIC, &pctx)) {
1190 ext2fs_mmp_clear(fs);
1191 retval = 0;
1192 }
f57b3715
DW
1193 } else if (retval == EXT2_ET_MMP_CSUM_INVALID) {
1194 if (fix_problem(ctx, PR_0_MMP_CSUM_INVALID, &pctx)) {
1195 ext2fs_mmp_clear(fs);
1196 retval = 0;
1197 }
a8a1ee12
DW
1198 } else
1199 com_err(ctx->program_name, retval, "%s",
1200 _("while reading MMP block"));
0f5eba75
AD
1201 return retval;
1202}
1203
1b6bf175
TT
1204int main (int argc, char *argv[])
1205{
82b59ca1 1206 errcode_t retval = 0, retval2 = 0, orig_retval = 0;
1b6bf175 1207 int exit_value = FSCK_OK;
1b6bf175
TT
1208 ext2_filsys fs = 0;
1209 io_manager io_ptr;
5dd8f963 1210 struct ext2_super_block *sb;
1b6bf175
TT
1211 const char *lib_ver_date;
1212 int my_ver, lib_ver;
1213 e2fsck_t ctx;
4dbfd79d 1214 blk64_t orig_superblock;
1b6bf175 1215 struct problem_context pctx;
115d4b4b 1216 int flags, run_result, was_changed;
5107d0d1 1217 int journal_size;
9f0288d3 1218 int sysval, sys_page_size = 4096;
c5d2f50d 1219 int old_bitmaps;
dcc91e10 1220 __u32 features[3];
1dc506cb 1221 char *cp;
1d6fd6d0 1222 int qtype = -99; /* quota type */
efc6f628 1223
1b6bf175 1224 clear_problem_context(&pctx);
9b3018a8 1225 sigcatcher_setup();
1b6bf175
TT
1226#ifdef MTRACE
1227 mtrace();
1228#endif
1229#ifdef MCHECK
1230 mcheck(0);
0c4a0726
TT
1231#endif
1232#ifdef ENABLE_NLS
1233 setlocale(LC_MESSAGES, "");
14308a53 1234 setlocale(LC_CTYPE, "");
0c4a0726
TT
1235 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1236 textdomain(NLS_CAT_NAME);
9d4507c5 1237 set_com_err_gettext(gettext);
1b6bf175
TT
1238#endif
1239 my_ver = ext2fs_parse_version_string(my_ver_string);
1240 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
1241 if (my_ver > lib_ver) {
45ff69ff
AD
1242 fprintf( stderr, "%s",
1243 _("Error: ext2fs library version out of date!\n"));
1b6bf175
TT
1244 show_version_only++;
1245 }
efc6f628 1246
1b6bf175
TT
1247 retval = PRS(argc, argv, &ctx);
1248 if (retval) {
45ff69ff 1249 com_err("e2fsck", retval, "%s",
0c4a0726 1250 _("while trying to initialize program"));
243dc31f 1251 exit(FSCK_ERROR);
1b6bf175 1252 }
24fc5032 1253 reserve_stdio_fds();
efc6f628 1254
b0e91c89
TT
1255 set_up_logging(ctx);
1256 if (ctx->logf) {
1257 int i;
1258 fputs("E2fsck run: ", ctx->logf);
1259 for (i = 0; i < argc; i++) {
1260 if (i)
1261 fputc(' ', ctx->logf);
1262 fputs(argv[i], ctx->logf);
1263 }
1264 fputc('\n', ctx->logf);
1265 }
1266
6d96b00d 1267 init_resource_track(&ctx->global_rtrack, NULL);
1b6bf175 1268 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
b0e91c89 1269 log_err(ctx, "e2fsck %s (%s)\n", my_ver_string,
0f8973fb 1270 my_ver_date);
1b6bf175
TT
1271
1272 if (show_version_only) {
b0e91c89 1273 log_err(ctx, _("\tUsing %s, %s\n"),
1b6bf175 1274 error_message(EXT2_ET_BASE), lib_ver_date);
243dc31f 1275 exit(FSCK_OK);
1b6bf175 1276 }
efc6f628 1277
1b6bf175 1278 check_mount(ctx);
efc6f628 1279
1b6bf175
TT
1280 if (!(ctx->options & E2F_OPT_PREEN) &&
1281 !(ctx->options & E2F_OPT_NO) &&
1282 !(ctx->options & E2F_OPT_YES)) {
54a31a3b 1283 if (!ctx->interactive)
f8188fff 1284 fatal_error(ctx,
0c4a0726 1285 _("need terminal for interactive repairs"));
1b6bf175
TT
1286 }
1287 ctx->superblock = ctx->use_superblock;
0f5eba75
AD
1288
1289 flags = EXT2_FLAG_SKIP_MMP;
1b6bf175 1290restart:
2a29f135 1291#ifdef CONFIG_TESTIO_DEBUG
f38cf3cb
TT
1292 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1293 io_ptr = test_io_manager;
1294 test_io_backing_manager = unix_io_manager;
1295 } else
1b6bf175 1296#endif
f38cf3cb 1297 io_ptr = unix_io_manager;
0f5eba75 1298 flags |= EXT2_FLAG_NOFREE_ON_ERROR;
c5d2f50d
VAH
1299 profile_get_boolean(ctx->profile, "options", "old_bitmaps", 0, 0,
1300 &old_bitmaps);
1301 if (!old_bitmaps)
1302 flags |= EXT2_FLAG_64BITS;
cd5bb7c8
AD
1303 if ((ctx->options & E2F_OPT_READONLY) == 0) {
1304 flags |= EXT2_FLAG_RW;
1305 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
1306 ctx->mount_flags & EXT2_MF_READONLY))
1307 flags |= EXT2_FLAG_EXCLUSIVE;
78a0d2ba
TT
1308 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
1309 (ctx->options & E2F_OPT_FORCE))
1310 flags &= ~EXT2_FLAG_EXCLUSIVE;
cd5bb7c8 1311 }
17390c04 1312
0d19ccbd 1313 ctx->openfs_flags = flags;
60663890
TT
1314 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1315
ae6cdcf7
TT
1316 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
1317 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1b6bf175 1318 ((retval == EXT2_ET_BAD_MAGIC) ||
dbfab110 1319 (retval == EXT2_ET_SB_CSUM_INVALID) ||
cd538080 1320 (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
82b59ca1 1321 ((retval == 0) && (retval2 = ext2fs_check_desc(fs))))) {
010c49cf
TT
1322 if (retval) {
1323 pctx.errcode = retval;
1324 fix_problem(ctx, PR_0_OPEN_FAILED, &pctx);
1325 }
1326 if (retval2) {
1327 pctx.errcode = retval2;
1328 fix_problem(ctx, PR_0_CHECK_DESC_FAILED, &pctx);
1329 }
1330 pctx.errcode = 0;
41cec349 1331 if (retval2 == ENOMEM || retval2 == EXT2_ET_NO_MEMORY) {
82b59ca1
TT
1332 retval = retval2;
1333 goto failure;
1334 }
dcc91e10
TT
1335 if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) {
1336 ext2fs_free(fs);
1337 fs = NULL;
1338 }
1b6bf175 1339 if (!fs || (fs->group_desc_count > 1)) {
b0e91c89
TT
1340 log_out(ctx, _("%s: %s trying backup blocks...\n"),
1341 ctx->program_name,
1342 retval ? _("Superblock invalid,") :
1343 _("Group descriptors look bad..."));
60663890 1344 orig_superblock = ctx->superblock;
f1a1761d 1345 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
1b6bf175 1346 if (fs)
47fee2ef 1347 ext2fs_close_free(&fs);
cd538080 1348 orig_retval = retval;
60663890
TT
1349 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1350 if ((orig_retval == 0) && retval != 0) {
22ff06d5 1351 if (fs)
47fee2ef 1352 ext2fs_close_free(&fs);
b0e91c89
TT
1353 log_out(ctx, _("%s: %s while using the "
1354 "backup blocks"),
1355 ctx->program_name,
1356 error_message(retval));
1357 log_out(ctx, _("%s: going back to original "
1358 "superblock\n"),
1359 ctx->program_name);
60663890
TT
1360 ctx->superblock = orig_superblock;
1361 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1362 }
1b6bf175
TT
1363 }
1364 }
dcc91e10
TT
1365 if (((retval == EXT2_ET_UNSUPP_FEATURE) ||
1366 (retval == EXT2_ET_RO_UNSUPP_FEATURE)) &&
1367 fs && fs->super) {
1368 sb = fs->super;
efc6f628 1369 features[0] = (sb->s_feature_compat &
dcc91e10 1370 ~EXT2_LIB_FEATURE_COMPAT_SUPP);
efc6f628 1371 features[1] = (sb->s_feature_incompat &
dcc91e10 1372 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP);
efc6f628 1373 features[2] = (sb->s_feature_ro_compat &
dcc91e10
TT
1374 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1375 if (features[0] || features[1] || features[2])
1376 goto print_unsupp_features;
1377 }
82b59ca1 1378failure:
1b6bf175 1379 if (retval) {
cd538080
TT
1380 if (orig_retval)
1381 retval = orig_retval;
0c4a0726 1382 com_err(ctx->program_name, retval, _("while trying to open %s"),
1b6bf175 1383 ctx->filesystem_name);
24dd4028 1384 if (retval == EXT2_ET_REV_TOO_HIGH) {
45ff69ff
AD
1385 log_out(ctx, "%s",
1386 _("The filesystem revision is apparently "
1387 "too high for this version of e2fsck.\n"
1388 "(Or the filesystem superblock "
1389 "is corrupt)\n\n"));
24dd4028
TT
1390 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1391 } else if (retval == EXT2_ET_SHORT_READ)
45ff69ff
AD
1392 log_out(ctx, "%s",
1393 _("Could this be a zero-length partition?\n"));
1b6bf175 1394 else if ((retval == EPERM) || (retval == EACCES))
b0e91c89 1395 log_out(ctx, _("You must have %s access to the "
45ff69ff 1396 "filesystem or be root\n"),
1b6bf175
TT
1397 (ctx->options & E2F_OPT_READONLY) ?
1398 "r/o" : "r/w");
1399 else if (retval == ENXIO)
45ff69ff
AD
1400 log_out(ctx, "%s",
1401 _("Possibly non-existent or swap device?\n"));
2e14e0c8 1402 else if (retval == EBUSY)
45ff69ff
AD
1403 log_out(ctx, "%s", _("Filesystem mounted or opened "
1404 "exclusively by another program?\n"));
32f66cc7 1405 else if (retval == ENOENT)
45ff69ff
AD
1406 log_out(ctx, "%s",
1407 _("Possibly non-existent device?\n"));
68227542
TT
1408#ifdef EROFS
1409 else if (retval == EROFS)
45ff69ff
AD
1410 log_out(ctx, "%s", _("Disk write-protected; use the "
1411 "-n option to do a read-only\n"
1412 "check of the device.\n"));
68227542 1413#endif
c8b20b40 1414 else {
1b6bf175 1415 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
c8b20b40
DW
1416 if (retval == EXT2_ET_BAD_MAGIC)
1417 check_plausibility(ctx->filesystem_name,
1418 CHECK_FS_EXIST, NULL);
1419 }
243dc31f 1420 fatal_error(ctx, 0);
1b6bf175 1421 }
058ad1c7
TT
1422 /*
1423 * We only update the master superblock because (a) paranoia;
1424 * we don't want to corrupt the backup superblocks, and (b) we
1425 * don't need to update the mount count and last checked
1426 * fields in the backup superblock (the kernel doesn't update
1427 * the backup superblocks anyway). With newer versions of the
1428 * library this flag is set by ext2fs_open2(), but we set this
1429 * here just to be sure. (No, we don't support e2fsck running
1430 * with some other libext2fs than the one that it was shipped
1431 * with, but just in case....)
1432 */
1433 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
d2af1bdd
TT
1434
1435 if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1436 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1437 int need_restart = 0;
1438
59119646
TT
1439 pctx.errcode = ext2fs_get_device_size2(ctx->filesystem_name,
1440 blocksize,
1441 &ctx->num_blocks);
d2af1bdd
TT
1442 /*
1443 * The floppy driver refuses to allow anyone else to
1444 * open the device if has been opened with O_EXCL;
1445 * this is unlike other block device drivers in Linux.
1446 * To handle this, we close the filesystem and then
1447 * reopen the filesystem after we get the device size.
1448 */
1449 if (pctx.errcode == EBUSY) {
47fee2ef 1450 ext2fs_close_free(&fs);
d2af1bdd 1451 need_restart++;
efc6f628 1452 pctx.errcode =
59119646
TT
1453 ext2fs_get_device_size2(ctx->filesystem_name,
1454 blocksize,
1455 &ctx->num_blocks);
d2af1bdd
TT
1456 }
1457 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1458 ctx->num_blocks = 0;
1459 else if (pctx.errcode) {
1460 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1461 ctx->flags |= E2F_FLAG_ABORT;
1462 fatal_error(ctx, 0);
d2af1bdd
TT
1463 }
1464 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1465 if (need_restart)
1466 goto restart;
1467 }
1468
1b6bf175 1469 ctx->fs = fs;
8dceb924 1470 fs->now = ctx->now;
5dd8f963 1471 sb = fs->super;
b0e91c89 1472
5dd8f963 1473 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1b6bf175 1474 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
0c4a0726 1475 _("while trying to open %s"),
1b6bf175 1476 ctx->filesystem_name);
f8188fff 1477 get_newer:
0c4a0726 1478 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1b6bf175 1479 }
3b5386dc
TT
1480
1481 /*
1482 * Set the device name, which is used whenever we print error
1483 * or informational messages to the user.
1484 */
1485 if (ctx->device_name == 0 &&
5dd8f963 1486 (sb->s_volume_name[0] != 0)) {
f364093b
TT
1487 ctx->device_name = string_copy(ctx, sb->s_volume_name,
1488 sizeof(sb->s_volume_name));
3b5386dc
TT
1489 }
1490 if (ctx->device_name == 0)
1dc506cb
TT
1491 ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
1492 for (cp = ctx->device_name; *cp; cp++)
1493 if (isspace(*cp) || *cp == ':')
1494 *cp = '_';
3b5386dc 1495
14c5af32
AD
1496 ehandler_init(fs->io);
1497
0f5eba75
AD
1498 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1499 (flags & EXT2_FLAG_SKIP_MMP)) {
1500 if (e2fsck_check_mmp(fs, ctx))
1501 fatal_error(ctx, 0);
0f5eba75 1502
349b8a37
TT
1503 /*
1504 * Restart in order to reopen fs but this time start mmp.
1505 */
ebdf895b 1506 ext2fs_close_free(&ctx->fs);
0f5eba75
AD
1507 flags &= ~EXT2_FLAG_SKIP_MMP;
1508 goto restart;
1509 }
1510
b0e91c89
TT
1511 if (ctx->logf)
1512 fprintf(ctx->logf, "Filesystem UUID: %s\n",
1513 e2p_uuid2str(sb->s_uuid));
1514
17390c04 1515 /*
9b565759 1516 * Make sure the ext3 superblock fields are consistent.
17390c04 1517 */
3b5386dc
TT
1518 retval = e2fsck_check_ext3_journal(ctx);
1519 if (retval) {
1520 com_err(ctx->program_name, retval,
1521 _("while checking ext3 journal for %s"),
1522 ctx->device_name);
243dc31f 1523 fatal_error(ctx, 0);
3b5386dc
TT
1524 }
1525
9b565759
TT
1526 /*
1527 * Check to see if we need to do ext3-style recovery. If so,
1528 * do it, and then restart the fsck.
1529 */
5dd8f963 1530 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
2575fb04 1531 if (ctx->options & E2F_OPT_READONLY) {
45ff69ff
AD
1532 log_out(ctx, "%s",
1533 _("Warning: skipping journal recovery because "
1534 "doing a read-only filesystem check.\n"));
2575fb04
TT
1535 io_channel_flush(ctx->fs->io);
1536 } else {
b92ae153
TT
1537 if (ctx->flags & E2F_FLAG_RESTARTED) {
1538 /*
1539 * Whoops, we attempted to run the
1540 * journal twice. This should never
1541 * happen, unless the hardware or
1542 * device driver is being bogus.
1543 */
1544 com_err(ctx->program_name, 0,
45ff69ff
AD
1545 _("unable to set superblock flags "
1546 "on %s\n"), ctx->device_name);
b92ae153
TT
1547 fatal_error(ctx, 0);
1548 }
2575fb04
TT
1549 retval = e2fsck_run_ext3_journal(ctx);
1550 if (retval) {
1551 com_err(ctx->program_name, retval,
3b5386dc 1552 _("while recovering ext3 journal of %s"),
2575fb04
TT
1553 ctx->device_name);
1554 fatal_error(ctx, 0);
1555 }
47fee2ef 1556 ext2fs_close_free(&ctx->fs);
b92ae153 1557 ctx->flags |= E2F_FLAG_RESTARTED;
2575fb04 1558 goto restart;
17390c04 1559 }
17390c04 1560 }
3b5386dc 1561
1b6bf175
TT
1562 /*
1563 * Check for compatibility with the feature sets. We need to
1564 * be more stringent than ext2fs_open().
1565 */
dcc91e10
TT
1566 features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP;
1567 features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP;
efc6f628 1568 features[2] = (sb->s_feature_ro_compat &
dcc91e10
TT
1569 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1570print_unsupp_features:
1571 if (features[0] || features[1] || features[2]) {
1572 int i, j;
1573 __u32 *mask = features, m;
1574
b0e91c89 1575 log_err(ctx, _("%s has unsupported feature(s):"),
dcc91e10
TT
1576 ctx->filesystem_name);
1577
1578 for (i=0; i <3; i++,mask++) {
1579 for (j=0,m=1; j < 32; j++, m<<=1) {
1580 if (*mask & m)
b0e91c89 1581 log_err(ctx, " %s",
dcc91e10
TT
1582 e2p_feature2string(i, m));
1583 }
1584 }
b0e91c89 1585 log_err(ctx, "\n");
1b6bf175
TT
1586 goto get_newer;
1587 }
8fdc9985
TT
1588#ifndef ENABLE_HTREE
1589 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
b0e91c89 1590 log_err(ctx, _("%s: e2fsck not compiled with HTREE support,\n\t"
8fdc9985 1591 "but filesystem %s has HTREE directories.\n"),
b0e91c89 1592 ctx->program_name, ctx->device_name);
8fdc9985
TT
1593 goto get_newer;
1594 }
1595#endif
1596
1b6bf175
TT
1597 /*
1598 * If the user specified a specific superblock, presumably the
1599 * master superblock has been trashed. So we mark the
1600 * superblock as dirty, so it can be written out.
1601 */
1602 if (ctx->superblock &&
1603 !(ctx->options & E2F_OPT_READONLY))
1604 ext2fs_mark_super_dirty(fs);
1605
9f0288d3
TT
1606 /*
1607 * Calculate the number of filesystem blocks per pagesize. If
1608 * fs->blocksize > page_size, set the number of blocks per
1609 * pagesize to 1 to avoid division by zero errors.
1610 */
1611#ifdef _SC_PAGESIZE
1612 sysval = sysconf(_SC_PAGESIZE);
1613 if (sysval > 0)
1614 sys_page_size = sysval;
1615#endif /* _SC_PAGESIZE */
1616 ctx->blocks_per_page = sys_page_size / fs->blocksize;
1617 if (ctx->blocks_per_page == 0)
1618 ctx->blocks_per_page = 1;
1619
1b6bf175
TT
1620 if (ctx->superblock)
1621 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
550a4afa 1622 ext2fs_mark_valid(fs);
1b6bf175 1623 check_super_block(ctx);
a02ce9df 1624 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1625 fatal_error(ctx, 0);
1b6bf175 1626 check_if_skip(ctx);
69d0edfd 1627 check_resize_inode(ctx);
1b6bf175
TT
1628 if (bad_blocks_file)
1629 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1630 else if (cflag)
4fb9d52b 1631 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
a02ce9df 1632 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
243dc31f 1633 fatal_error(ctx, 0);
1b6bf175
TT
1634
1635 /*
1636 * Mark the system as valid, 'til proven otherwise
1637 */
1638 ext2fs_mark_valid(fs);
1639
1640 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1641 if (retval) {
b0e91c89
TT
1642 log_out(ctx, _("%s: %s while reading bad blocks inode\n"),
1643 ctx->program_name, error_message(retval));
1b6bf175 1644 preenhalt(ctx);
45ff69ff
AD
1645 log_out(ctx, "%s", _("This doesn't bode well, "
1646 "but we'll try to go on...\n"));
1b6bf175 1647 }
08b21301 1648
5107d0d1
KS
1649 /*
1650 * Save the journal size in megabytes.
1651 * Try and use the journal size from the backup else let e2fsck
1652 * find the default journal size.
1653 */
1654 if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS)
931b58e1
AD
1655 journal_size = (sb->s_jnl_blocks[15] << (32 - 20)) |
1656 (sb->s_jnl_blocks[16] >> 20);
5107d0d1
KS
1657 else
1658 journal_size = -1;
1659
624e4a64 1660 if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA) {
624e4a64
AK
1661 /* Quotas were enabled. Do quota accounting during fsck. */
1662 if ((sb->s_usr_quota_inum && sb->s_grp_quota_inum) ||
1663 (!sb->s_usr_quota_inum && !sb->s_grp_quota_inum))
1664 qtype = -1;
1665 else
1666 qtype = sb->s_usr_quota_inum ? USRQUOTA : GRPQUOTA;
1667
a86d55da 1668 quota_init_context(&ctx->qctx, ctx->fs, qtype);
624e4a64
AK
1669 }
1670
08b21301 1671 run_result = e2fsck_run(ctx);
5596defa 1672 e2fsck_clear_progbar(ctx);
5107d0d1 1673
c4c93e2d
DW
1674 if (!ctx->invalid_bitmaps &&
1675 (ctx->flags & E2F_FLAG_JOURNAL_INODE)) {
5107d0d1
KS
1676 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1677 if (journal_size < 1024)
4efbac6f 1678 journal_size = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
5107d0d1
KS
1679 if (journal_size < 0) {
1680 fs->super->s_feature_compat &=
1681 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
0cfce7f7 1682 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
b0e91c89
TT
1683 log_out(ctx, "%s: Couldn't determine "
1684 "journal size\n", ctx->program_name);
5107d0d1
KS
1685 goto no_journal;
1686 }
b0e91c89 1687 log_out(ctx, _("Creating journal (%d blocks): "),
5107d0d1
KS
1688 journal_size);
1689 fflush(stdout);
1690 retval = ext2fs_add_journal_inode(fs,
1691 journal_size, 0);
1692 if (retval) {
b0e91c89
TT
1693 log_out(ctx, "%s: while trying to create "
1694 "journal\n", error_message(retval));
5107d0d1
KS
1695 goto no_journal;
1696 }
45ff69ff
AD
1697 log_out(ctx, "%s", _(" Done.\n"));
1698 log_out(ctx, "%s",
b151d346 1699 _("\n*** journal has been regenerated ***\n"));
5107d0d1
KS
1700 }
1701 }
1702no_journal:
1703
624e4a64 1704 if (ctx->qctx) {
7943ccf5
AK
1705 int i, needs_writeout;
1706 for (i = 0; i < MAXQUOTAS; i++) {
1707 if (qtype != -1 && qtype != i)
1708 continue;
1709 needs_writeout = 0;
1710 pctx.num = i;
1711 retval = quota_compare_and_update(ctx->qctx, i,
1712 &needs_writeout);
1713 if ((retval || needs_writeout) &&
1714 fix_problem(ctx, PR_6_UPDATE_QUOTAS, &pctx))
1715 quota_write_inode(ctx->qctx, i);
1716 }
a86d55da 1717 quota_release_context(&ctx->qctx);
624e4a64
AK
1718 }
1719
08b21301 1720 if (run_result == E2F_FLAG_RESTART) {
45ff69ff
AD
1721 log_out(ctx, "%s",
1722 _("Restarting e2fsck from the beginning...\n"));
1b6bf175
TT
1723 retval = e2fsck_reset_context(ctx);
1724 if (retval) {
45ff69ff 1725 com_err(ctx->program_name, retval, "%s",
0c4a0726 1726 _("while resetting context"));
243dc31f 1727 fatal_error(ctx, 0);
1b6bf175 1728 }
ebdf895b 1729 ext2fs_close_free(&ctx->fs);
1b6bf175
TT
1730 goto restart;
1731 }
115d4b4b
TT
1732 if (run_result & E2F_FLAG_ABORT)
1733 fatal_error(ctx, _("aborted"));
1734
1735#ifdef MTRACE
1736 mtrace_print("Cleanup");
1737#endif
1738 was_changed = ext2fs_test_changed(fs);
4cae0452 1739 if (run_result & E2F_FLAG_CANCEL) {
b0e91c89
TT
1740 log_out(ctx, _("%s: e2fsck canceled.\n"), ctx->device_name ?
1741 ctx->device_name : ctx->filesystem_name);
4cae0452 1742 exit_value |= FSCK_CANCELED;
115d4b4b
TT
1743 } else if (!(ctx->options & E2F_OPT_READONLY)) {
1744 if (ext2fs_test_valid(fs)) {
1745 if (!(sb->s_state & EXT2_VALID_FS))
1746 exit_value |= FSCK_NONDESTRUCT;
1747 sb->s_state = EXT2_VALID_FS;
1748 if (check_backup_super_block(ctx))
1749 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1750 } else
1751 sb->s_state &= ~EXT2_VALID_FS;
1752 if (!(ctx->flags & E2F_FLAG_TIME_INSANE))
1753 sb->s_lastcheck = ctx->now;
1754 sb->s_mnt_count = 0;
1755 memset(((char *) sb) + EXT4_S_ERR_START, 0, EXT4_S_ERR_LEN);
1756 pctx.errcode = ext2fs_set_gdt_csum(ctx->fs);
1757 if (pctx.errcode)
1758 fix_problem(ctx, PR_6_SET_BG_CHECKSUM, &pctx);
0c37f456
TT
1759 ext2fs_mark_super_dirty(fs);
1760 }
1b6bf175 1761
115d4b4b 1762 e2fsck_write_bitmaps(ctx);
ab2cd4a4
DW
1763 if (fs->flags & EXT2_FLAG_DIRTY) {
1764 pctx.errcode = ext2fs_flush(ctx->fs);
1765 if (pctx.errcode)
1766 fix_problem(ctx, PR_6_FLUSH_FILESYSTEM, &pctx);
1767 }
115d4b4b
TT
1768 pctx.errcode = io_channel_flush(ctx->fs->io);
1769 if (pctx.errcode)
1770 fix_problem(ctx, PR_6_IO_FLUSH, &pctx);
1771
1772 if (was_changed) {
4cae0452 1773 exit_value |= FSCK_NONDESTRUCT;
1b6bf175 1774 if (!(ctx->options & E2F_OPT_PREEN))
b0e91c89
TT
1775 log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS "
1776 "MODIFIED *****\n"),
1777 ctx->device_name);
ee895139 1778 if (ctx->mount_flags & EXT2_MF_ISROOT) {
b0e91c89
TT
1779 log_out(ctx, _("%s: ***** REBOOT LINUX *****\n"),
1780 ctx->device_name);
4cae0452 1781 exit_value |= FSCK_REBOOT;
1b6bf175
TT
1782 }
1783 }
eb065ccf 1784 if (!ext2fs_test_valid(fs) ||
efc6f628 1785 ((exit_value & FSCK_CANCELED) &&
eb065ccf 1786 (sb->s_state & EXT2_ERROR_FS))) {
b0e91c89
TT
1787 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
1788 "errors **********\n\n"), ctx->device_name);
4cae0452
TT
1789 exit_value |= FSCK_UNCORRECTED;
1790 exit_value &= ~FSCK_NONDESTRUCT;
d3124019 1791 }
eb065ccf
TT
1792 if (exit_value & FSCK_CANCELED) {
1793 int allow_cancellation;
1794
1795 profile_get_boolean(ctx->profile, "options",
efc6f628 1796 "allow_cancellation", 0, 0,
eb065ccf 1797 &allow_cancellation);
4cae0452 1798 exit_value &= ~FSCK_NONDESTRUCT;
eb065ccf 1799 if (allow_cancellation && ext2fs_test_valid(fs) &&
efc6f628 1800 (sb->s_state & EXT2_VALID_FS) &&
eb065ccf
TT
1801 !(sb->s_state & EXT2_ERROR_FS))
1802 exit_value = 0;
115d4b4b 1803 } else
4cae0452 1804 show_stats(ctx);
1b6bf175 1805
9facd076
KC
1806 print_resource_track(ctx, NULL, &ctx->global_rtrack, ctx->fs->io);
1807
a82d88ea 1808 ext2fs_close_free(&ctx->fs);
f364093b 1809 free(ctx->journal_name);
b28a6e96 1810
b28a6e96 1811 e2fsck_free_context(ctx);
a6d8302b
TT
1812 remove_error_table(&et_ext2_error_table);
1813 remove_error_table(&et_prof_error_table);
1b6bf175
TT
1814 return exit_value;
1815}