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