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