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