unix.c revision 53ef44c40a3e425d2c700d8fd77a6b655aa121fe
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#include <sys/ioctl.h>
37#include <malloc.h>
38
39#include "et/com_err.h"
40#include "e2fsck.h"
41#include "problem.h"
42#include "../version.h"
43
44/* Command line options */
45static int blocksize = 0;
46static int swapfs = 0;
47static int normalize_swapfs = 0;
48static int cflag = 0;		/* check disk */
49static int show_version_only = 0;
50static int force = 0;
51static int verbose = 0;
52
53static int replace_bad_blocks = 0;
54static char *bad_blocks_file = 0;
55
56static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
57
58static int root_filesystem = 0;
59static int read_only_root = 0;
60
61e2fsck_t e2fsck_global_ctx;	/* Try your very best not to use this! */
62
63static void usage(e2fsck_t ctx)
64{
65	fprintf(stderr,
66		_("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
67		"\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
68		"\t\t[-l|-L bad_blocks_file] [-C fd] device\n"),
69		ctx->program_name);
70
71	fprintf(stderr, _("\nEmergency help:\n"
72		" -p                   Automatic repair (no questions)\n"
73		" -n                   Make no changes to the filesystem\n"
74		" -y                   Assume \"yes\" to all questions\n"
75		" -c                   Check for bad blocks\n"
76		" -f                   Force checking even if filesystem is marked clean\n"));
77	fprintf(stderr, _(""
78		" -v                   Be verbose\n"
79		" -b superblock        Use alternative superblock\n"
80		" -B blocksize         Force blocksize when looking for superblock\n"
81		" -l bad_blocks_file   Add to badblocks list\n"
82		" -L bad_blocks_file   Set badblocks list\n"
83		));
84
85	exit(FSCK_USAGE);
86}
87
88static void show_stats(e2fsck_t	ctx)
89{
90	ext2_filsys fs = ctx->fs;
91	int inodes, inodes_used, blocks, blocks_used;
92	int dir_links;
93	int num_files, num_links;
94	int frag_percent;
95
96	dir_links = 2 * ctx->fs_directory_count - 1;
97	num_files = ctx->fs_total_count - dir_links;
98	num_links = ctx->fs_links_count - dir_links;
99	inodes = fs->super->s_inodes_count;
100	inodes_used = (fs->super->s_inodes_count -
101		       fs->super->s_free_inodes_count);
102	blocks = fs->super->s_blocks_count;
103	blocks_used = (fs->super->s_blocks_count -
104		       fs->super->s_free_blocks_count);
105
106	frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
107	frag_percent = (frag_percent + 5) / 10;
108
109	if (!verbose) {
110		printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
111		       ctx->device_name, inodes_used, inodes,
112		       frag_percent / 10, frag_percent % 10,
113		       blocks_used, blocks);
114		return;
115	}
116	/*
117	 * This is a bit ugly. But I think there will nearly always be more
118	 * than one "thing" to report about, so I won't try writing complex
119	 * code to handle one/two/many forms of all words.
120	 * Some languages (Italian, at least) never uses the plural form
121	 * of foreign words, so in real life this could not be a problem.
122	 * md@linux.it - 2000-1-15
123	 */
124#ifdef ENABLE_NLS
125	printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
126		(inodes_used != 1), 100 * inodes_used / inodes);
127	printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
128		ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
129	printf (_("         # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
130		ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
131	printf (_("%8d blocks used (%d%%)\n"
132		"%8d bad blocks\n"), blocks_used,
133		100 * blocks_used / blocks, ctx->fs_badblocks_count);
134	printf (_("\n%8d regular files\n"
135		"%8d directories\n"
136		"%8d character device files\n"
137		"%8d block device files\n"
138		"%8d fifos\n"
139		"%8d links\n"
140		"%8d symbolic links (%d fast symbolic links)\n"
141		"%8d sockets\n"
142		"--------\n"
143		"%8d files\n"),
144		ctx->fs_regular_count,
145		ctx->fs_directory_count,
146		ctx->fs_chardev_count,
147		ctx->fs_blockdev_count,
148		ctx->fs_fifo_count,
149		ctx->fs_links_count - dir_links,
150		ctx->fs_symlinks_count,
151		ctx->fs_fast_symlinks_count,
152		ctx->fs_sockets_count,
153		ctx->fs_total_count - dir_links);
154#else
155	printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
156		(inodes_used != 1) ? "s" : "",
157		100 * inodes_used / inodes);
158	printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
159		ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
160	printf ("         # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
161		ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
162	printf ("%8d block%s used (%d%%)\n"
163		"%8d bad block%s\n", blocks_used,
164		(blocks_used != 1) ? "s" : "",
165		100 * blocks_used / blocks, ctx->fs_badblocks_count,
166		ctx->fs_badblocks_count != 1 ? "s" : "");
167	printf ("\n%8d regular file%s\n"
168		"%8d director%s\n"
169		"%8d character device file%s\n"
170		"%8d block device file%s\n"
171		"%8d fifo%s\n"
172		"%8d link%s\n"
173		"%8d symbolic link%s (%d fast symbolic link%s)\n"
174		"%8d socket%s\n"
175		"--------\n"
176		"%8d file%s\n",
177		ctx->fs_regular_count,
178		(ctx->fs_regular_count != 1) ? "s" : "",
179		ctx->fs_directory_count,
180		(ctx->fs_directory_count != 1) ? "ies" : "y",
181		ctx->fs_chardev_count,
182		(ctx->fs_chardev_count != 1) ? "s" : "",
183		ctx->fs_blockdev_count,
184		(ctx->fs_blockdev_count != 1) ? "s" : "",
185		ctx->fs_fifo_count,
186		(ctx->fs_fifo_count != 1) ? "s" : "",
187		ctx->fs_links_count - dir_links,
188		((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
189		ctx->fs_symlinks_count,
190		(ctx->fs_symlinks_count != 1) ? "s" : "",
191		ctx->fs_fast_symlinks_count,
192		(ctx->fs_fast_symlinks_count != 1) ? "s" : "",
193		ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
194		ctx->fs_total_count - dir_links,
195		((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
196#endif
197}
198
199static void check_mount(e2fsck_t ctx)
200{
201	errcode_t	retval;
202	int		mount_flags, cont, fd;
203
204	retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
205	if (retval) {
206		com_err("ext2fs_check_if_mount", retval,
207			_("while determining whether %s is mounted."),
208			ctx->filesystem_name);
209		return;
210	}
211	if (!(mount_flags & EXT2_MF_MOUNTED))
212		return;
213
214#if (defined(__linux__) && defined(HAVE_MNTENT_H))
215	/*
216	 * If the root is mounted read-only, then /etc/mtab is
217	 * probably not correct; so we won't issue a warning based on
218	 * it.
219	 */
220	fd = open(MOUNTED, O_RDWR);
221	if (fd < 0) {
222		if (errno == EROFS)
223			return;
224	} else
225		close(fd);
226#endif
227
228	if (ctx->options & E2F_OPT_READONLY) {
229		printf(_("Warning!  %s is mounted.\n"), ctx->filesystem_name);
230		return;
231	}
232
233	printf(_("%s is mounted.  "), ctx->filesystem_name);
234	if (!isatty(0) || !isatty(1))
235		fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
236	printf(_("\n\n\007\007\007\007WARNING!!!  "
237	       "Running e2fsck on a mounted filesystem may cause\n"
238	       "SEVERE filesystem damage.\007\007\007\n\n"));
239	cont = ask_yn(_("Do you really want to continue"), -1);
240	if (!cont) {
241		printf (_("check aborted.\n"));
242		exit (0);
243	}
244	return;
245}
246
247/*
248 * This routine checks to see if a filesystem can be skipped; if so,
249 * it will exit with E2FSCK_OK.  Under some conditions it will print a
250 * message explaining why a check is being forced.
251 */
252static void check_if_skip(e2fsck_t ctx)
253{
254	ext2_filsys fs = ctx->fs;
255	const char *reason = NULL;
256
257	if (force || bad_blocks_file || cflag || swapfs)
258		return;
259
260	if (fs->super->s_state & EXT2_ERROR_FS)
261		reason = _("contains a file system with errors");
262	else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
263		reason = _("was not cleanly unmounted");
264	else if ((fs->super->s_max_mnt_count >= 0) &&
265		 (fs->super->s_mnt_count >=
266		  (unsigned) fs->super->s_max_mnt_count))
267		reason = _("has reached maximal mount count");
268	else if (fs->super->s_checkinterval &&
269		 time(0) >= (fs->super->s_lastcheck +
270			     fs->super->s_checkinterval))
271		reason = _("has gone too long without being checked");
272	if (reason) {
273		printf(_("%s %s, check forced.\n"), ctx->device_name, reason);
274		return;
275	}
276	printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
277	       fs->super->s_inodes_count - fs->super->s_free_inodes_count,
278	       fs->super->s_inodes_count,
279	       fs->super->s_blocks_count - fs->super->s_free_blocks_count,
280	       fs->super->s_blocks_count);
281	ext2fs_close(fs);
282	exit(FSCK_OK);
283}
284
285/*
286 * For completion notice
287 */
288struct percent_tbl {
289	int	max_pass;
290	int	table[32];
291};
292struct percent_tbl e2fsck_tbl = {
293	5, { 0, 70, 90, 92,  95, 100 }
294};
295static char bar[] =
296	"==============================================================="
297	"===============================================================";
298static char spaces[] =
299	"                                                               "
300	"                                                               ";
301
302static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
303			  int max)
304{
305	float	percent;
306
307	if (pass <= 0)
308		return 0.0;
309	if (pass > tbl->max_pass || max == 0)
310		return 100.0;
311	percent = ((float) curr) / ((float) max);
312	return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
313		+ tbl->table[pass-1]);
314}
315
316extern void e2fsck_clear_progbar(e2fsck_t ctx)
317{
318	if (!(ctx->flags & E2F_FLAG_PROG_BAR))
319		return;
320
321	printf("%s\r", spaces + (sizeof(spaces) - 80));
322	ctx->flags &= ~E2F_FLAG_PROG_BAR;
323}
324
325static int e2fsck_update_progress(e2fsck_t ctx, int pass,
326				  unsigned long cur, unsigned long max)
327{
328	static const char spinner[] = "\\|/-";
329	char buf[80];
330	int	i;
331	float percent;
332	int	tick;
333	struct timeval	tv;
334	static int dpywidth = 0;
335
336	if (pass == 0)
337		return 0;
338
339	if (ctx->progress_fd) {
340		sprintf(buf, "%d %lu %lu\n", pass, cur, max);
341		write(ctx->progress_fd, buf, strlen(buf));
342	} else {
343		if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
344			return 0;
345		if (dpywidth == 0) {
346			dpywidth = 66 - strlen(ctx->device_name);
347			dpywidth = 8 * (dpywidth / 8);
348		}
349		/*
350		 * Calculate the new progress position.  If the
351		 * percentage hasn't changed, then we skip out right
352		 * away.
353		 */
354		percent = calc_percent(&e2fsck_tbl, pass, cur, max);
355		if (ctx->progress_last_percent == (int) 10 * percent)
356			return 0;
357		ctx->progress_last_percent = (int) 10 * percent;
358
359		/*
360		 * If we've already updated the spinner once within
361		 * the last 1/8th of a second, no point doing it
362		 * again.
363		 */
364		gettimeofday(&tv, NULL);
365		tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
366		if ((tick == ctx->progress_last_time) &&
367		    (cur != max) && (cur != 0))
368			return 0;
369		ctx->progress_last_time = tick;
370
371		/*
372		 * Advance the spinner, and note that the progress bar
373		 * will be on the screen
374		 */
375		ctx->progress_pos = (ctx->progress_pos+1) & 3;
376		ctx->flags |= E2F_FLAG_PROG_BAR;
377
378		i = ((percent * dpywidth) + 50) / 100;
379		printf("%s: |%s%s", ctx->device_name,
380		       bar + (sizeof(bar) - (i+1)),
381		       spaces + (sizeof(spaces) - (dpywidth - i + 1)));
382		if (percent == 100.0)
383			fputc('|', stdout);
384		else
385			fputc(spinner[ctx->progress_pos & 3], stdout);
386		printf(" %4.1f%%   \r", percent);
387		if (percent == 100.0)
388			e2fsck_clear_progbar(ctx);
389		fflush(stdout);
390	}
391	return 0;
392}
393
394#define PATH_SET "PATH=/sbin"
395
396static void reserve_stdio_fds(NOARGS)
397{
398	int	fd;
399
400	while (1) {
401		fd = open("/dev/null", O_RDWR);
402		if (fd > 2)
403			break;
404		if (fd < 0) {
405			fprintf(stderr, _("ERROR: Couldn't open "
406				"/dev/null (%s)\n"),
407				strerror(errno));
408			break;
409		}
410	}
411	close(fd);
412}
413
414#ifdef HAVE_SIGNAL_H
415static void signal_progress_on(int sig)
416{
417	e2fsck_t ctx = e2fsck_global_ctx;
418
419	if (!ctx)
420		return;
421
422	ctx->progress = e2fsck_update_progress;
423	ctx->progress_fd = 0;
424}
425
426static void signal_progress_off(int sig)
427{
428	e2fsck_t ctx = e2fsck_global_ctx;
429
430	if (!ctx)
431		return;
432
433	e2fsck_clear_progbar(ctx);
434	ctx->progress = 0;
435}
436#endif
437
438static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
439{
440	int		flush = 0;
441	int		c;
442#ifdef MTRACE
443	extern void	*mallwatch;
444#endif
445	char		*oldpath = getenv("PATH");
446	e2fsck_t	ctx;
447	errcode_t	retval;
448#ifdef HAVE_SIGNAL_H
449	struct sigaction	sa;
450#endif
451
452	retval = e2fsck_allocate_context(&ctx);
453	if (retval)
454		return retval;
455
456	*ret_ctx = ctx;
457
458	/* Update our PATH to include /sbin  */
459	if (oldpath) {
460		char *newpath;
461
462		newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
463					  strlen (oldpath));
464		if (!newpath)
465			fatal_error(ctx, "Couldn't malloc() newpath");
466		strcpy (newpath, PATH_SET);
467		strcat (newpath, ":");
468		strcat (newpath, oldpath);
469		putenv (newpath);
470	} else
471		putenv (PATH_SET);
472
473	setbuf(stdout, NULL);
474	setbuf(stderr, NULL);
475	initialize_ext2_error_table();
476
477	if (argc && *argv)
478		ctx->program_name = *argv;
479	else
480		ctx->program_name = "e2fsck";
481	while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF)
482		switch (c) {
483		case 'C':
484			ctx->progress = e2fsck_update_progress;
485			ctx->progress_fd = atoi(optarg);
486			break;
487		case 'p':
488		case 'a':
489			ctx->options |= E2F_OPT_PREEN;
490			ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
491			break;
492		case 'n':
493			ctx->options |= E2F_OPT_NO;
494			ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
495			break;
496		case 'y':
497			ctx->options |= E2F_OPT_YES;
498			ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
499			break;
500		case 't':
501#ifdef RESOURCE_TRACK
502			if (ctx->options & E2F_OPT_TIME)
503				ctx->options |= E2F_OPT_TIME2;
504			else
505				ctx->options |= E2F_OPT_TIME;
506#else
507			fprintf(stderr, _("The -t option is not "
508				"supported on this version of e2fsck.\n"));
509#endif
510			break;
511		case 'c':
512			cflag++;
513			ctx->options |= E2F_OPT_CHECKBLOCKS;
514			break;
515		case 'r':
516			/* What we do by default, anyway! */
517			break;
518		case 'b':
519			ctx->use_superblock = atoi(optarg);
520			break;
521		case 'B':
522			blocksize = atoi(optarg);
523			break;
524		case 'I':
525			ctx->inode_buffer_blocks = atoi(optarg);
526			break;
527		case 'P':
528			ctx->process_inode_size = atoi(optarg);
529			break;
530		case 'L':
531			replace_bad_blocks++;
532		case 'l':
533			bad_blocks_file = (char *) malloc(strlen(optarg)+1);
534			if (!bad_blocks_file)
535				fatal_error(ctx,
536					    "Couldn't malloc bad_blocks_file");
537			strcpy(bad_blocks_file, optarg);
538			break;
539		case 'd':
540			ctx->options |= E2F_OPT_DEBUG;
541			break;
542		case 'f':
543			force = 1;
544			break;
545		case 'F':
546#ifdef BLKFLSBUF
547			flush = 1;
548#else
549			fatal_error(ctx, _("-F not supported"));
550#endif
551			break;
552		case 'v':
553			verbose = 1;
554			break;
555		case 'V':
556			show_version_only = 1;
557			break;
558#ifdef MTRACE
559		case 'M':
560			mallwatch = (void *) strtol(optarg, NULL, 0);
561			break;
562#endif
563		case 'N':
564			ctx->device_name = optarg;
565			break;
566		case 's':
567			normalize_swapfs = 1;
568		case 'S':
569			swapfs = 1;
570			break;
571		default:
572			usage(ctx);
573		}
574	if (show_version_only)
575		return 0;
576	if (optind != argc - 1)
577		usage(ctx);
578	if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
579	    !cflag && !swapfs)
580		ctx->options |= E2F_OPT_READONLY;
581	ctx->filesystem_name = argv[optind];
582#ifdef BLKFLSBUF
583	if (flush) {
584		int	fd = open(ctx->filesystem_name, O_RDONLY, 0);
585
586		if (fd < 0) {
587			com_err("open", errno,
588				_("while opening %s for flushing"),
589				ctx->filesystem_name);
590			fatal_error(ctx, 0);
591		}
592		if (fsync(fd) < 0) {
593			com_err("fsync", errno,
594				_("while trying to flush %s"),
595				ctx->filesystem_name);
596			fatal_error(ctx, 0);
597		}
598		if (ioctl(fd, BLKFLSBUF, 0) < 0) {
599			com_err("BLKFLSBUF", errno,
600				_("while trying to flush %s"),
601				ctx->filesystem_name);
602			fatal_error(ctx, 0);
603		}
604		close(fd);
605	}
606#endif /* BLKFLSBUF */
607	if (swapfs) {
608		if (cflag || bad_blocks_file) {
609			fprintf(stderr, _("Incompatible options not "
610				"allowed when byte-swapping.\n"));
611			exit(FSCK_USAGE);
612		}
613	}
614#ifdef HAVE_SIGNAL_H
615	/*
616	 * Set up signal action
617	 */
618	memset(&sa, 0, sizeof(struct sigaction));
619#ifdef SA_RESTART
620	sa.sa_flags = SA_RESTART;
621#endif
622	e2fsck_global_ctx = ctx;
623	sa.sa_handler = signal_progress_on;
624	sigaction(SIGUSR1, &sa, 0);
625	sa.sa_handler = signal_progress_off;
626	sigaction(SIGUSR2, &sa, 0);
627#endif
628	return 0;
629}
630
631static const char *my_ver_string = E2FSPROGS_VERSION;
632static const char *my_ver_date = E2FSPROGS_DATE;
633
634int main (int argc, char *argv[])
635{
636	errcode_t	retval = 0;
637	int		exit_value = FSCK_OK;
638	int		i;
639	ext2_filsys	fs = 0;
640	io_manager	io_ptr;
641	struct ext2_super_block *sb;
642	const char	*lib_ver_date;
643	int		my_ver, lib_ver;
644	e2fsck_t	ctx;
645	struct problem_context pctx;
646	int flags, run_result;
647
648	clear_problem_context(&pctx);
649#ifdef MTRACE
650	mtrace();
651#endif
652#ifdef MCHECK
653	mcheck(0);
654#endif
655#ifdef ENABLE_NLS
656	setlocale(LC_MESSAGES, "");
657	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
658	textdomain(NLS_CAT_NAME);
659#endif
660	my_ver = ext2fs_parse_version_string(my_ver_string);
661	lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
662	if (my_ver > lib_ver) {
663		fprintf( stderr, _("Error: ext2fs library version "
664			"out of date!\n"));
665		show_version_only++;
666	}
667
668	retval = PRS(argc, argv, &ctx);
669	if (retval) {
670		com_err("e2fsck", retval,
671			_("while trying to initialize program"));
672		exit(FSCK_ERROR);
673	}
674	reserve_stdio_fds();
675
676#ifdef RESOURCE_TRACK
677	init_resource_track(&ctx->global_rtrack);
678#endif
679
680	if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
681		fprintf (stderr, _("e2fsck %s, %s for EXT2 FS %s, %s\n"),
682			 my_ver_string, my_ver_date, EXT2FS_VERSION,
683			 EXT2FS_DATE);
684
685	if (show_version_only) {
686		fprintf(stderr, _("\tUsing %s, %s\n"),
687			error_message(EXT2_ET_BASE), lib_ver_date);
688		exit(FSCK_OK);
689	}
690
691	check_mount(ctx);
692
693	if (!(ctx->options & E2F_OPT_PREEN) &&
694	    !(ctx->options & E2F_OPT_NO) &&
695	    !(ctx->options & E2F_OPT_YES)) {
696		if (!isatty (0) || !isatty (1))
697			fatal_error(ctx,
698				    _("need terminal for interactive repairs"));
699	}
700	ctx->superblock = ctx->use_superblock;
701restart:
702#if 1
703	io_ptr = unix_io_manager;
704#else
705	io_ptr = test_io_manager;
706	test_io_backing_manager = unix_io_manager;
707#endif
708	flags = 0;
709	if ((ctx->options & E2F_OPT_READONLY) == 0)
710		flags |= EXT2_FLAG_RW;
711
712	if (ctx->superblock && blocksize) {
713		retval = ext2fs_open(ctx->filesystem_name, flags,
714				     ctx->superblock, blocksize, io_ptr, &fs);
715	} else if (ctx->superblock) {
716		for (i=0; possible_block_sizes[i]; i++) {
717			retval = ext2fs_open(ctx->filesystem_name, flags,
718					     ctx->superblock,
719					     possible_block_sizes[i],
720					     io_ptr, &fs);
721			if (!retval)
722				break;
723		}
724	} else
725		retval = ext2fs_open(ctx->filesystem_name, flags,
726				     0, 0, io_ptr, &fs);
727	if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
728	    ((retval == EXT2_ET_BAD_MAGIC) ||
729	     ((retval == 0) && ext2fs_check_desc(fs)))) {
730		if (!fs || (fs->group_desc_count > 1)) {
731			printf(_("%s trying backup blocks...\n"),
732			       retval ? _("Couldn't find ext2 superblock,") :
733			       _("Group descriptors look bad..."));
734			ctx->superblock = get_backup_sb(fs);
735			if (fs)
736				ext2fs_close(fs);
737			goto restart;
738		}
739	}
740	if (retval) {
741		com_err(ctx->program_name, retval, _("while trying to open %s"),
742			ctx->filesystem_name);
743		if (retval == EXT2_ET_REV_TOO_HIGH) {
744			printf(_("The filesystem revision is apparently "
745			       "too high for this version of e2fsck.\n"
746			       "(Or the filesystem superblock "
747			       "is corrupt)\n\n"));
748			fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
749		} else if (retval == EXT2_ET_SHORT_READ)
750			printf(_("Could this be a zero-length partition?\n"));
751		else if ((retval == EPERM) || (retval == EACCES))
752			printf(_("You must have %s access to the "
753			       "filesystem or be root\n"),
754			       (ctx->options & E2F_OPT_READONLY) ?
755			       "r/o" : "r/w");
756		else if (retval == ENXIO)
757			printf(_("Possibly non-existent or swap device?\n"));
758#ifdef EROFS
759		else if (retval == EROFS)
760			printf(_("Disk write-protected; use the -n option "
761			       "to do a read-only\n"
762			       "check of the device.\n"));
763#endif
764		else
765			fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
766		fatal_error(ctx, 0);
767	}
768	ctx->fs = fs;
769	fs->priv_data = ctx;
770	sb = fs->super;
771	if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
772		com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
773			_("while trying to open %s"),
774			ctx->filesystem_name);
775	get_newer:
776		fatal_error(ctx, _("Get a newer version of e2fsck!"));
777	}
778
779	/*
780	 * Set the device name, which is used whenever we print error
781	 * or informational messages to the user.
782	 */
783	if (ctx->device_name == 0 &&
784	    (sb->s_volume_name[0] != 0)) {
785		char *cp = malloc(sizeof(sb->s_volume_name)+1);
786		if (cp) {
787			strncpy(cp, sb->s_volume_name,
788				sizeof(sb->s_volume_name));
789			cp[sizeof(sb->s_volume_name)] = 0;
790			ctx->device_name = cp;
791		}
792	}
793	if (ctx->device_name == 0)
794		ctx->device_name = ctx->filesystem_name;
795
796	/*
797	 * Make sure the ext3 superblock fields are consistent.
798	 */
799	retval = e2fsck_check_ext3_journal(ctx);
800	if (retval) {
801		com_err(ctx->program_name, retval,
802			_("while checking ext3 journal for %s"),
803			ctx->device_name);
804		ext2fs_close(ctx->fs);
805		fatal_error(ctx, 0);
806	}
807
808	/*
809	 * Check to see if we need to do ext3-style recovery.  If so,
810	 * do it, and then restart the fsck.
811	 */
812	if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
813		if (ctx->options & E2F_OPT_READONLY) {
814			printf(_("Warning: skipping journal recovery "
815				 "because doing a read-only filesystem "
816				 "check.\n"));
817			io_channel_flush(ctx->fs->io);
818		} else {
819			retval = e2fsck_run_ext3_journal(ctx);
820			if (retval) {
821				com_err(ctx->program_name, retval,
822				_("while recovering ext3 journal of %s"),
823					ctx->device_name);
824				fatal_error(ctx, 0);
825			}
826			ext2fs_close(ctx->fs);
827			ctx->fs = 0;
828			goto restart;
829		}
830	}
831
832	/*
833	 * Check for compatibility with the feature sets.  We need to
834	 * be more stringent than ext2fs_open().
835	 */
836	if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
837	    (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
838		com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
839			"(%s)", ctx->device_name);
840		goto get_newer;
841	}
842	if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
843		com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
844			"(%s)", ctx->device_name);
845		goto get_newer;
846	}
847#ifdef ENABLE_COMPRESSION
848	if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
849		com_err(ctx->program_name, 0,
850			_("Warning: compression support is experimental.\n"));
851#endif
852
853	/*
854	 * If the user specified a specific superblock, presumably the
855	 * master superblock has been trashed.  So we mark the
856	 * superblock as dirty, so it can be written out.
857	 */
858	if (ctx->superblock &&
859	    !(ctx->options & E2F_OPT_READONLY))
860		ext2fs_mark_super_dirty(fs);
861
862	/*
863	 * Don't overwrite the backup superblock and block
864	 * descriptors, until we're sure the filesystem is OK....
865	 */
866	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
867
868	ehandler_init(fs->io);
869
870	if (ctx->superblock)
871		set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
872	check_super_block(ctx);
873	if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
874		fatal_error(ctx, 0);
875	check_if_skip(ctx);
876	if (bad_blocks_file)
877		read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
878	else if (cflag)
879		test_disk(ctx);
880	if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
881		fatal_error(ctx, 0);
882
883	if (normalize_swapfs) {
884		if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
885		    ext2fs_native_flag()) {
886			fprintf(stderr, _("%s: Filesystem byte order "
887				"already normalized.\n"), ctx->device_name);
888			fatal_error(ctx, 0);
889		}
890	}
891	if (swapfs) {
892		swap_filesys(ctx);
893		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
894			fatal_error(ctx, 0);
895	}
896
897	/*
898	 * Mark the system as valid, 'til proven otherwise
899	 */
900	ext2fs_mark_valid(fs);
901
902	retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
903	if (retval) {
904		com_err(ctx->program_name, retval,
905			_("while reading bad blocks inode"));
906		preenhalt(ctx);
907		printf(_("This doesn't bode well,"
908			 " but we'll try to go on...\n"));
909	}
910
911	run_result = e2fsck_run(ctx);
912	e2fsck_clear_progbar(ctx);
913	if (run_result == E2F_FLAG_RESTART) {
914		printf(_("Restarting e2fsck from the beginning...\n"));
915		retval = e2fsck_reset_context(ctx);
916		if (retval) {
917			com_err(ctx->program_name, retval,
918				_("while resetting context"));
919			fatal_error(ctx, 0);
920		}
921		ext2fs_close(fs);
922		goto restart;
923	}
924	if (run_result & E2F_FLAG_SIGNAL_MASK)
925		fatal_error(ctx, 0);
926	if (run_result & E2F_FLAG_CANCEL)
927		ext2fs_unmark_valid(fs);
928
929#ifdef MTRACE
930	mtrace_print("Cleanup");
931#endif
932	if (ext2fs_test_changed(fs)) {
933		exit_value = FSCK_NONDESTRUCT;
934		if (!(ctx->options & E2F_OPT_PREEN))
935		    printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
936			       ctx->device_name);
937		if (root_filesystem && !read_only_root) {
938			printf(_("%s: ***** REBOOT LINUX *****\n"),
939			       ctx->device_name);
940			exit_value = FSCK_REBOOT;
941		}
942	}
943	if (ext2fs_test_valid(fs))
944		fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
945	else
946		exit_value = FSCK_UNCORRECTED;
947	if (!(ctx->options & E2F_OPT_READONLY)) {
948		if (ext2fs_test_valid(fs)) {
949			if (!(sb->s_state & EXT2_VALID_FS))
950				exit_value = FSCK_NONDESTRUCT;
951			sb->s_state = EXT2_VALID_FS;
952		} else
953			sb->s_state &= ~EXT2_VALID_FS;
954		sb->s_mnt_count = 0;
955		sb->s_lastcheck = time(NULL);
956		ext2fs_mark_super_dirty(fs);
957	}
958	show_stats(ctx);
959
960	e2fsck_write_bitmaps(ctx);
961
962#ifdef RESOURCE_TRACK
963	if (ctx->options & E2F_OPT_TIME)
964		print_resource_track(NULL, &ctx->global_rtrack);
965#endif
966
967	ext2fs_close(fs);
968	ctx->fs = NULL;
969	e2fsck_free_context(ctx);
970
971	return exit_value;
972}
973