xref: /aosp_15_r20/external/e2fsprogs/e2fsck/unix.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * unix.c - The unix-specific code for e2fsck
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Public
8*6a54128fSAndroid Build Coastguard Worker  * License.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
13*6a54128fSAndroid Build Coastguard Worker 
14*6a54128fSAndroid Build Coastguard Worker #include "config.h"
15*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
16*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_STDLIB_H
17*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
18*6a54128fSAndroid Build Coastguard Worker #endif
19*6a54128fSAndroid Build Coastguard Worker #include <string.h>
20*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
21*6a54128fSAndroid Build Coastguard Worker #include <ctype.h>
22*6a54128fSAndroid Build Coastguard Worker #include <time.h>
23*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SIGNAL_H
24*6a54128fSAndroid Build Coastguard Worker #include <signal.h>
25*6a54128fSAndroid Build Coastguard Worker #endif
26*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETOPT_H
27*6a54128fSAndroid Build Coastguard Worker #include <getopt.h>
28*6a54128fSAndroid Build Coastguard Worker #else
29*6a54128fSAndroid Build Coastguard Worker extern char *optarg;
30*6a54128fSAndroid Build Coastguard Worker extern int optind;
31*6a54128fSAndroid Build Coastguard Worker #endif
32*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
33*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_ERRNO_H
34*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
35*6a54128fSAndroid Build Coastguard Worker #endif
36*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_IOCTL_H
37*6a54128fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
38*6a54128fSAndroid Build Coastguard Worker #endif
39*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MALLOC_H
40*6a54128fSAndroid Build Coastguard Worker #include <malloc.h>
41*6a54128fSAndroid Build Coastguard Worker #endif
42*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_TYPES_H
43*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
44*6a54128fSAndroid Build Coastguard Worker #endif
45*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_DIRENT_H
46*6a54128fSAndroid Build Coastguard Worker #include <dirent.h>
47*6a54128fSAndroid Build Coastguard Worker #endif
48*6a54128fSAndroid Build Coastguard Worker #include <libgen.h>
49*6a54128fSAndroid Build Coastguard Worker 
50*6a54128fSAndroid Build Coastguard Worker #include "e2p/e2p.h"
51*6a54128fSAndroid Build Coastguard Worker #include "et/com_err.h"
52*6a54128fSAndroid Build Coastguard Worker #include "e2p/e2p.h"
53*6a54128fSAndroid Build Coastguard Worker #include "uuid/uuid.h"
54*6a54128fSAndroid Build Coastguard Worker #include "support/plausible.h"
55*6a54128fSAndroid Build Coastguard Worker #include "support/devname.h"
56*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
57*6a54128fSAndroid Build Coastguard Worker #include "problem.h"
58*6a54128fSAndroid Build Coastguard Worker #include "jfs_user.h"
59*6a54128fSAndroid Build Coastguard Worker #include "../version.h"
60*6a54128fSAndroid Build Coastguard Worker 
61*6a54128fSAndroid Build Coastguard Worker /* Command line options */
62*6a54128fSAndroid Build Coastguard Worker static int cflag;		/* check disk */
63*6a54128fSAndroid Build Coastguard Worker static int show_version_only;
64*6a54128fSAndroid Build Coastguard Worker static int verbose;
65*6a54128fSAndroid Build Coastguard Worker 
66*6a54128fSAndroid Build Coastguard Worker static int replace_bad_blocks;
67*6a54128fSAndroid Build Coastguard Worker static int keep_bad_blocks;
68*6a54128fSAndroid Build Coastguard Worker static char *bad_blocks_file;
69*6a54128fSAndroid Build Coastguard Worker 
70*6a54128fSAndroid Build Coastguard Worker e2fsck_t e2fsck_global_ctx;	/* Try your very best not to use this! */
71*6a54128fSAndroid Build Coastguard Worker 
72*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_JBD_DEBUG		/* Enabled by configure --enable-jbd-debug */
73*6a54128fSAndroid Build Coastguard Worker int journal_enable_debug = -1;
74*6a54128fSAndroid Build Coastguard Worker #endif
75*6a54128fSAndroid Build Coastguard Worker 
usage(e2fsck_t ctx)76*6a54128fSAndroid Build Coastguard Worker static void usage(e2fsck_t ctx)
77*6a54128fSAndroid Build Coastguard Worker {
78*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr,
79*6a54128fSAndroid Build Coastguard Worker 		_("Usage: %s [-panyrcdfktvDFV] [-b superblock] [-B blocksize]\n"
80*6a54128fSAndroid Build Coastguard Worker 		"\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
81*6a54128fSAndroid Build Coastguard Worker 		"\t\t[-E extended-options] [-z undo_file] device\n"),
82*6a54128fSAndroid Build Coastguard Worker 		ctx->program_name ? ctx->program_name : "e2fsck");
83*6a54128fSAndroid Build Coastguard Worker 
84*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr, "%s", _("\nEmergency help:\n"
85*6a54128fSAndroid Build Coastguard Worker 		" -p                   Automatic repair (no questions)\n"
86*6a54128fSAndroid Build Coastguard Worker 		" -n                   Make no changes to the filesystem\n"
87*6a54128fSAndroid Build Coastguard Worker 		" -y                   Assume \"yes\" to all questions\n"
88*6a54128fSAndroid Build Coastguard Worker 		" -c                   Check for bad blocks and add them to the badblock list\n"
89*6a54128fSAndroid Build Coastguard Worker 		" -f                   Force checking even if filesystem is marked clean\n"));
90*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr, "%s", _(""
91*6a54128fSAndroid Build Coastguard Worker 		" -v                   Be verbose\n"
92*6a54128fSAndroid Build Coastguard Worker 		" -b superblock        Use alternative superblock\n"
93*6a54128fSAndroid Build Coastguard Worker 		" -B blocksize         Force blocksize when looking for superblock\n"
94*6a54128fSAndroid Build Coastguard Worker 		" -j external_journal  Set location of the external journal\n"
95*6a54128fSAndroid Build Coastguard Worker 		" -l bad_blocks_file   Add to badblocks list\n"
96*6a54128fSAndroid Build Coastguard Worker 		" -L bad_blocks_file   Set badblocks list\n"
97*6a54128fSAndroid Build Coastguard Worker 		" -z undo_file         Create an undo file\n"
98*6a54128fSAndroid Build Coastguard Worker 		));
99*6a54128fSAndroid Build Coastguard Worker 
100*6a54128fSAndroid Build Coastguard Worker 	exit(FSCK_USAGE);
101*6a54128fSAndroid Build Coastguard Worker }
102*6a54128fSAndroid Build Coastguard Worker 
show_stats(e2fsck_t ctx)103*6a54128fSAndroid Build Coastguard Worker static void show_stats(e2fsck_t	ctx)
104*6a54128fSAndroid Build Coastguard Worker {
105*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
106*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t inodes, inodes_used;
107*6a54128fSAndroid Build Coastguard Worker 	blk64_t blocks, blocks_used;
108*6a54128fSAndroid Build Coastguard Worker 	unsigned int dir_links;
109*6a54128fSAndroid Build Coastguard Worker 	unsigned int num_files, num_links;
110*6a54128fSAndroid Build Coastguard Worker 	__u32 *mask, m;
111*6a54128fSAndroid Build Coastguard Worker 	int frag_percent_file = 0, frag_percent_dir = 0, frag_percent_total = 0;
112*6a54128fSAndroid Build Coastguard Worker 	int i, j, printed = 0;
113*6a54128fSAndroid Build Coastguard Worker 
114*6a54128fSAndroid Build Coastguard Worker 	dir_links = 2 * ctx->fs_directory_count - 1;
115*6a54128fSAndroid Build Coastguard Worker 	num_files = ctx->fs_total_count - dir_links;
116*6a54128fSAndroid Build Coastguard Worker 	num_links = ctx->fs_links_count - dir_links;
117*6a54128fSAndroid Build Coastguard Worker 	inodes = fs->super->s_inodes_count;
118*6a54128fSAndroid Build Coastguard Worker 	inodes_used = (fs->super->s_inodes_count -
119*6a54128fSAndroid Build Coastguard Worker 		       fs->super->s_free_inodes_count);
120*6a54128fSAndroid Build Coastguard Worker 	blocks = ext2fs_blocks_count(fs->super);
121*6a54128fSAndroid Build Coastguard Worker 	blocks_used = (ext2fs_blocks_count(fs->super) -
122*6a54128fSAndroid Build Coastguard Worker 		       ext2fs_free_blocks_count(fs->super));
123*6a54128fSAndroid Build Coastguard Worker 
124*6a54128fSAndroid Build Coastguard Worker 	if (inodes_used > 0) {
125*6a54128fSAndroid Build Coastguard Worker 		frag_percent_file = (10000 * ctx->fs_fragmented) / inodes_used;
126*6a54128fSAndroid Build Coastguard Worker 		frag_percent_file = (frag_percent_file + 5) / 10;
127*6a54128fSAndroid Build Coastguard Worker 
128*6a54128fSAndroid Build Coastguard Worker 		frag_percent_dir = (10000 * ctx->fs_fragmented_dir) / inodes_used;
129*6a54128fSAndroid Build Coastguard Worker 		frag_percent_dir = (frag_percent_dir + 5) / 10;
130*6a54128fSAndroid Build Coastguard Worker 
131*6a54128fSAndroid Build Coastguard Worker 		frag_percent_total = ((10000 * (ctx->fs_fragmented +
132*6a54128fSAndroid Build Coastguard Worker 						ctx->fs_fragmented_dir))
133*6a54128fSAndroid Build Coastguard Worker 				      / inodes_used);
134*6a54128fSAndroid Build Coastguard Worker 		frag_percent_total = (frag_percent_total + 5) / 10;
135*6a54128fSAndroid Build Coastguard Worker 	}
136*6a54128fSAndroid Build Coastguard Worker 
137*6a54128fSAndroid Build Coastguard Worker 	if (!verbose) {
138*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s: %u/%u files (%0d.%d%% non-contiguous), "
139*6a54128fSAndroid Build Coastguard Worker 			       "%llu/%llu blocks\n"),
140*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name, inodes_used, inodes,
141*6a54128fSAndroid Build Coastguard Worker 			frag_percent_total / 10, frag_percent_total % 10,
142*6a54128fSAndroid Build Coastguard Worker 			(unsigned long long) blocks_used,
143*6a54128fSAndroid Build Coastguard Worker 			(unsigned long long) blocks);
144*6a54128fSAndroid Build Coastguard Worker 		return;
145*6a54128fSAndroid Build Coastguard Worker 	}
146*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "report_features", 0, 0,
147*6a54128fSAndroid Build Coastguard Worker 			    &i);
148*6a54128fSAndroid Build Coastguard Worker 	if (verbose && i) {
149*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "\nFilesystem features:");
150*6a54128fSAndroid Build Coastguard Worker 		mask = &ctx->fs->super->s_feature_compat;
151*6a54128fSAndroid Build Coastguard Worker 		for (i = 0; i < 3; i++, mask++) {
152*6a54128fSAndroid Build Coastguard Worker 			for (j = 0, m = 1; j < 32; j++, m <<= 1) {
153*6a54128fSAndroid Build Coastguard Worker 				if (*mask & m) {
154*6a54128fSAndroid Build Coastguard Worker 					log_out(ctx, " %s",
155*6a54128fSAndroid Build Coastguard Worker 						e2p_feature2string(i, m));
156*6a54128fSAndroid Build Coastguard Worker 					printed++;
157*6a54128fSAndroid Build Coastguard Worker 				}
158*6a54128fSAndroid Build Coastguard Worker 			}
159*6a54128fSAndroid Build Coastguard Worker 		}
160*6a54128fSAndroid Build Coastguard Worker 		if (printed == 0)
161*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, " (none)");
162*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "\n");
163*6a54128fSAndroid Build Coastguard Worker 	}
164*6a54128fSAndroid Build Coastguard Worker 
165*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("\n%12u inode used (%2.2f%%, out of %u)\n",
166*6a54128fSAndroid Build Coastguard Worker 			"\n%12u inodes used (%2.2f%%, out of %u)\n",
167*6a54128fSAndroid Build Coastguard Worker 			inodes_used), inodes_used,
168*6a54128fSAndroid Build Coastguard Worker 		100.0 * inodes_used / inodes, inodes);
169*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u non-contiguous file (%0d.%d%%)\n",
170*6a54128fSAndroid Build Coastguard Worker 			"%12u non-contiguous files (%0d.%d%%)\n",
171*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_fragmented),
172*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_fragmented, frag_percent_file / 10,
173*6a54128fSAndroid Build Coastguard Worker 		frag_percent_file % 10);
174*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u non-contiguous directory (%0d.%d%%)\n",
175*6a54128fSAndroid Build Coastguard Worker 			"%12u non-contiguous directories (%0d.%d%%)\n",
176*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_fragmented_dir),
177*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_fragmented_dir, frag_percent_dir / 10,
178*6a54128fSAndroid Build Coastguard Worker 		frag_percent_dir % 10);
179*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, _("             # of inodes with ind/dind/tind blocks: "
180*6a54128fSAndroid Build Coastguard Worker 		       "%u/%u/%u\n"),
181*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
182*6a54128fSAndroid Build Coastguard Worker 
183*6a54128fSAndroid Build Coastguard Worker 	for (j=MAX_EXTENT_DEPTH_COUNT-1; j >=0; j--)
184*6a54128fSAndroid Build Coastguard Worker 		if (ctx->extent_depth_count[j])
185*6a54128fSAndroid Build Coastguard Worker 			break;
186*6a54128fSAndroid Build Coastguard Worker 	if (++j) {
187*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", _("             Extent depth histogram: "));
188*6a54128fSAndroid Build Coastguard Worker 		for (i=0; i < j; i++) {
189*6a54128fSAndroid Build Coastguard Worker 			if (i)
190*6a54128fSAndroid Build Coastguard Worker 				fputc('/', stdout);
191*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%u", ctx->extent_depth_count[i]);
192*6a54128fSAndroid Build Coastguard Worker 		}
193*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "\n");
194*6a54128fSAndroid Build Coastguard Worker 	}
195*6a54128fSAndroid Build Coastguard Worker 
196*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12llu block used (%2.2f%%, out of %llu)\n",
197*6a54128fSAndroid Build Coastguard Worker 			"%12llu blocks used (%2.2f%%, out of %llu)\n",
198*6a54128fSAndroid Build Coastguard Worker 		   blocks_used),
199*6a54128fSAndroid Build Coastguard Worker 		(unsigned long long) blocks_used, 100.0 * blocks_used / blocks,
200*6a54128fSAndroid Build Coastguard Worker 		(unsigned long long) blocks);
201*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u bad block\n", "%12u bad blocks\n",
202*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_badblocks_count), ctx->fs_badblocks_count);
203*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u large file\n", "%12u large files\n",
204*6a54128fSAndroid Build Coastguard Worker 			ctx->large_files), ctx->large_files);
205*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("\n%12u regular file\n", "\n%12u regular files\n",
206*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_regular_count), ctx->fs_regular_count);
207*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u directory\n", "%12u directories\n",
208*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_directory_count), ctx->fs_directory_count);
209*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u character device file\n",
210*6a54128fSAndroid Build Coastguard Worker 			"%12u character device files\n", ctx->fs_chardev_count),
211*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_chardev_count);
212*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u block device file\n", "%12u block device files\n",
213*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_blockdev_count), ctx->fs_blockdev_count);
214*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u fifo\n", "%12u fifos\n", ctx->fs_fifo_count),
215*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_fifo_count);
216*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u link\n", "%12u links\n", num_links),
217*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_links_count - dir_links);
218*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u symbolic link", "%12u symbolic links",
219*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_symlinks_count), ctx->fs_symlinks_count);
220*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_(" (%u fast symbolic link)\n",
221*6a54128fSAndroid Build Coastguard Worker 			" (%u fast symbolic links)\n",
222*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_fast_symlinks_count),
223*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_fast_symlinks_count);
224*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u socket\n", "%12u sockets\n",
225*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_sockets_count),
226*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_sockets_count);
227*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, "------------\n");
228*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, P_("%12u file\n", "%12u files\n", num_files),
229*6a54128fSAndroid Build Coastguard Worker 		num_files);
230*6a54128fSAndroid Build Coastguard Worker }
231*6a54128fSAndroid Build Coastguard Worker 
check_mount(e2fsck_t ctx)232*6a54128fSAndroid Build Coastguard Worker static void check_mount(e2fsck_t ctx)
233*6a54128fSAndroid Build Coastguard Worker {
234*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
235*6a54128fSAndroid Build Coastguard Worker 	int		cont;
236*6a54128fSAndroid Build Coastguard Worker 
237*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_check_if_mounted(ctx->filesystem_name,
238*6a54128fSAndroid Build Coastguard Worker 					 &ctx->mount_flags);
239*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
240*6a54128fSAndroid Build Coastguard Worker 		com_err("ext2fs_check_if_mount", retval,
241*6a54128fSAndroid Build Coastguard Worker 			_("while determining whether %s is mounted."),
242*6a54128fSAndroid Build Coastguard Worker 			ctx->filesystem_name);
243*6a54128fSAndroid Build Coastguard Worker 		return;
244*6a54128fSAndroid Build Coastguard Worker 	}
245*6a54128fSAndroid Build Coastguard Worker 
246*6a54128fSAndroid Build Coastguard Worker 	/*
247*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem isn't mounted, or it's the root
248*6a54128fSAndroid Build Coastguard Worker 	 * filesystem and it's mounted read-only, and we're not doing
249*6a54128fSAndroid Build Coastguard Worker 	 * a read/write check, then everything's fine.
250*6a54128fSAndroid Build Coastguard Worker 	 */
251*6a54128fSAndroid Build Coastguard Worker 	if ((!(ctx->mount_flags & (EXT2_MF_MOUNTED | EXT2_MF_BUSY))) ||
252*6a54128fSAndroid Build Coastguard Worker 	    ((ctx->mount_flags & EXT2_MF_ISROOT) &&
253*6a54128fSAndroid Build Coastguard Worker 	     (ctx->mount_flags & EXT2_MF_READONLY) &&
254*6a54128fSAndroid Build Coastguard Worker 	     !(ctx->options & E2F_OPT_WRITECHECK)))
255*6a54128fSAndroid Build Coastguard Worker 		return;
256*6a54128fSAndroid Build Coastguard Worker 
257*6a54128fSAndroid Build Coastguard Worker 	if (((ctx->options & E2F_OPT_READONLY) ||
258*6a54128fSAndroid Build Coastguard Worker 	     ((ctx->options & E2F_OPT_FORCE) &&
259*6a54128fSAndroid Build Coastguard Worker 	      (ctx->mount_flags & EXT2_MF_READONLY))) &&
260*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_WRITECHECK)) {
261*6a54128fSAndroid Build Coastguard Worker 		if (ctx->mount_flags & EXT2_MF_MOUNTED)
262*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _("Warning!  %s is mounted.\n"),
263*6a54128fSAndroid Build Coastguard Worker 					ctx->filesystem_name);
264*6a54128fSAndroid Build Coastguard Worker 		else
265*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _("Warning!  %s is in use.\n"),
266*6a54128fSAndroid Build Coastguard Worker 					ctx->filesystem_name);
267*6a54128fSAndroid Build Coastguard Worker 		return;
268*6a54128fSAndroid Build Coastguard Worker 	}
269*6a54128fSAndroid Build Coastguard Worker 
270*6a54128fSAndroid Build Coastguard Worker 	if (ctx->mount_flags & EXT2_MF_MOUNTED)
271*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s is mounted.\n"), ctx->filesystem_name);
272*6a54128fSAndroid Build Coastguard Worker 	else
273*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s is in use.\n"), ctx->filesystem_name);
274*6a54128fSAndroid Build Coastguard Worker 	if (!ctx->interactive || ctx->mount_flags & EXT2_MF_BUSY)
275*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
276*6a54128fSAndroid Build Coastguard Worker 	puts("\007\007\007\007");
277*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, "%s", _("\n\nWARNING!!!  "
278*6a54128fSAndroid Build Coastguard Worker 		       "The filesystem is mounted.   "
279*6a54128fSAndroid Build Coastguard Worker 		       "If you continue you ***WILL***\n"
280*6a54128fSAndroid Build Coastguard Worker 		       "cause ***SEVERE*** filesystem damage.\n\n"));
281*6a54128fSAndroid Build Coastguard Worker 	puts("\007\007\007");
282*6a54128fSAndroid Build Coastguard Worker 	cont = ask_yn(ctx, _("Do you really want to continue"), 0);
283*6a54128fSAndroid Build Coastguard Worker 	if (!cont) {
284*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("check aborted.\n"));
285*6a54128fSAndroid Build Coastguard Worker 		exit (0);
286*6a54128fSAndroid Build Coastguard Worker 	}
287*6a54128fSAndroid Build Coastguard Worker 	return;
288*6a54128fSAndroid Build Coastguard Worker }
289*6a54128fSAndroid Build Coastguard Worker 
is_on_batt(void)290*6a54128fSAndroid Build Coastguard Worker static int is_on_batt(void)
291*6a54128fSAndroid Build Coastguard Worker {
292*6a54128fSAndroid Build Coastguard Worker 	FILE	*f;
293*6a54128fSAndroid Build Coastguard Worker 	DIR	*d;
294*6a54128fSAndroid Build Coastguard Worker 	char	tmp[80], tmp2[80], fname[NAME_MAX+30];
295*6a54128fSAndroid Build Coastguard Worker 	unsigned int	acflag;
296*6a54128fSAndroid Build Coastguard Worker 	struct dirent*	de;
297*6a54128fSAndroid Build Coastguard Worker 
298*6a54128fSAndroid Build Coastguard Worker 	f = fopen("/sys/class/power_supply/AC/online", "r");
299*6a54128fSAndroid Build Coastguard Worker 	if (f) {
300*6a54128fSAndroid Build Coastguard Worker 		if (fscanf(f, "%u\n", &acflag) == 1) {
301*6a54128fSAndroid Build Coastguard Worker 			fclose(f);
302*6a54128fSAndroid Build Coastguard Worker 			return (!acflag);
303*6a54128fSAndroid Build Coastguard Worker 		}
304*6a54128fSAndroid Build Coastguard Worker 		fclose(f);
305*6a54128fSAndroid Build Coastguard Worker 	}
306*6a54128fSAndroid Build Coastguard Worker 	f = fopen("/proc/apm", "r");
307*6a54128fSAndroid Build Coastguard Worker 	if (f) {
308*6a54128fSAndroid Build Coastguard Worker 		if (fscanf(f, "%79s %79s %79s %x", tmp, tmp, tmp, &acflag) != 4)
309*6a54128fSAndroid Build Coastguard Worker 			acflag = 1;
310*6a54128fSAndroid Build Coastguard Worker 		fclose(f);
311*6a54128fSAndroid Build Coastguard Worker 		return (acflag != 1);
312*6a54128fSAndroid Build Coastguard Worker 	}
313*6a54128fSAndroid Build Coastguard Worker 	d = opendir("/proc/acpi/ac_adapter");
314*6a54128fSAndroid Build Coastguard Worker 	if (d) {
315*6a54128fSAndroid Build Coastguard Worker 		while ((de=readdir(d)) != NULL) {
316*6a54128fSAndroid Build Coastguard Worker 			if (!strncmp(".", de->d_name, 1))
317*6a54128fSAndroid Build Coastguard Worker 				continue;
318*6a54128fSAndroid Build Coastguard Worker 			snprintf(fname, sizeof(fname),
319*6a54128fSAndroid Build Coastguard Worker 				 "/proc/acpi/ac_adapter/%s/state",
320*6a54128fSAndroid Build Coastguard Worker 				 de->d_name);
321*6a54128fSAndroid Build Coastguard Worker 			f = fopen(fname, "r");
322*6a54128fSAndroid Build Coastguard Worker 			if (!f)
323*6a54128fSAndroid Build Coastguard Worker 				continue;
324*6a54128fSAndroid Build Coastguard Worker 			if (fscanf(f, "%79s %79s", tmp2, tmp) != 2)
325*6a54128fSAndroid Build Coastguard Worker 				tmp[0] = 0;
326*6a54128fSAndroid Build Coastguard Worker 			fclose(f);
327*6a54128fSAndroid Build Coastguard Worker 			if (strncmp(tmp, "off-line", 8) == 0) {
328*6a54128fSAndroid Build Coastguard Worker 				closedir(d);
329*6a54128fSAndroid Build Coastguard Worker 				return 1;
330*6a54128fSAndroid Build Coastguard Worker 			}
331*6a54128fSAndroid Build Coastguard Worker 		}
332*6a54128fSAndroid Build Coastguard Worker 		closedir(d);
333*6a54128fSAndroid Build Coastguard Worker 	}
334*6a54128fSAndroid Build Coastguard Worker 	return 0;
335*6a54128fSAndroid Build Coastguard Worker }
336*6a54128fSAndroid Build Coastguard Worker 
337*6a54128fSAndroid Build Coastguard Worker /*
338*6a54128fSAndroid Build Coastguard Worker  * This routine checks to see if a filesystem can be skipped; if so,
339*6a54128fSAndroid Build Coastguard Worker  * it will exit with E2FSCK_OK.  Under some conditions it will print a
340*6a54128fSAndroid Build Coastguard Worker  * message explaining why a check is being forced.
341*6a54128fSAndroid Build Coastguard Worker  */
check_if_skip(e2fsck_t ctx)342*6a54128fSAndroid Build Coastguard Worker static void check_if_skip(e2fsck_t ctx)
343*6a54128fSAndroid Build Coastguard Worker {
344*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
345*6a54128fSAndroid Build Coastguard Worker 	struct problem_context pctx;
346*6a54128fSAndroid Build Coastguard Worker 	const char *reason = NULL;
347*6a54128fSAndroid Build Coastguard Worker 	unsigned int reason_arg = 0;
348*6a54128fSAndroid Build Coastguard Worker 	long next_check;
349*6a54128fSAndroid Build Coastguard Worker 	int batt = is_on_batt();
350*6a54128fSAndroid Build Coastguard Worker 	int defer_check_on_battery;
351*6a54128fSAndroid Build Coastguard Worker 	int broken_system_clock;
352*6a54128fSAndroid Build Coastguard Worker 	time_t lastcheck;
353*6a54128fSAndroid Build Coastguard Worker 
354*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_PROBLEMS_FIXED)
355*6a54128fSAndroid Build Coastguard Worker 		return;
356*6a54128fSAndroid Build Coastguard Worker 
357*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "broken_system_clock",
358*6a54128fSAndroid Build Coastguard Worker 			    0, 0, &broken_system_clock);
359*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_TIME_INSANE)
360*6a54128fSAndroid Build Coastguard Worker 		broken_system_clock = 1;
361*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options",
362*6a54128fSAndroid Build Coastguard Worker 			    "defer_check_on_battery", 0, 1,
363*6a54128fSAndroid Build Coastguard Worker 			    &defer_check_on_battery);
364*6a54128fSAndroid Build Coastguard Worker 	if (!defer_check_on_battery)
365*6a54128fSAndroid Build Coastguard Worker 		batt = 0;
366*6a54128fSAndroid Build Coastguard Worker 
367*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || cflag)
368*6a54128fSAndroid Build Coastguard Worker 		return;
369*6a54128fSAndroid Build Coastguard Worker 
370*6a54128fSAndroid Build Coastguard Worker 	if (ctx->options & E2F_OPT_JOURNAL_ONLY)
371*6a54128fSAndroid Build Coastguard Worker 		goto skip;
372*6a54128fSAndroid Build Coastguard Worker 
373*6a54128fSAndroid Build Coastguard Worker 	lastcheck = fs->super->s_lastcheck;
374*6a54128fSAndroid Build Coastguard Worker 	if (lastcheck > ctx->now)
375*6a54128fSAndroid Build Coastguard Worker 		lastcheck -= ctx->time_fudge;
376*6a54128fSAndroid Build Coastguard Worker 	if ((fs->super->s_state & EXT2_ERROR_FS) ||
377*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_test_valid(fs))
378*6a54128fSAndroid Build Coastguard Worker 		reason = _(" contains a file system with errors");
379*6a54128fSAndroid Build Coastguard Worker 	else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
380*6a54128fSAndroid Build Coastguard Worker 		reason = _(" was not cleanly unmounted");
381*6a54128fSAndroid Build Coastguard Worker 	else if (check_backup_super_block(ctx))
382*6a54128fSAndroid Build Coastguard Worker 		reason = _(" primary superblock features different from backup");
383*6a54128fSAndroid Build Coastguard Worker 	else if ((fs->super->s_max_mnt_count > 0) &&
384*6a54128fSAndroid Build Coastguard Worker 		 (fs->super->s_mnt_count >=
385*6a54128fSAndroid Build Coastguard Worker 		  (unsigned) fs->super->s_max_mnt_count)) {
386*6a54128fSAndroid Build Coastguard Worker 		reason = _(" has been mounted %u times without being checked");
387*6a54128fSAndroid Build Coastguard Worker 		reason_arg = fs->super->s_mnt_count;
388*6a54128fSAndroid Build Coastguard Worker 		if (batt && (fs->super->s_mnt_count <
389*6a54128fSAndroid Build Coastguard Worker 			     (unsigned) fs->super->s_max_mnt_count*2))
390*6a54128fSAndroid Build Coastguard Worker 			reason = 0;
391*6a54128fSAndroid Build Coastguard Worker 	} else if (!broken_system_clock && fs->super->s_checkinterval &&
392*6a54128fSAndroid Build Coastguard Worker 		   (ctx->now < lastcheck)) {
393*6a54128fSAndroid Build Coastguard Worker 		reason = _(" has filesystem last checked time in the future");
394*6a54128fSAndroid Build Coastguard Worker 		if (batt)
395*6a54128fSAndroid Build Coastguard Worker 			reason = 0;
396*6a54128fSAndroid Build Coastguard Worker 	} else if (!broken_system_clock && fs->super->s_checkinterval &&
397*6a54128fSAndroid Build Coastguard Worker 		   ((ctx->now - lastcheck) >=
398*6a54128fSAndroid Build Coastguard Worker 		    ((time_t) fs->super->s_checkinterval))) {
399*6a54128fSAndroid Build Coastguard Worker 		reason = _(" has gone %u days without being checked");
400*6a54128fSAndroid Build Coastguard Worker 		reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
401*6a54128fSAndroid Build Coastguard Worker 		if (batt && ((ctx->now - fs->super->s_lastcheck) <
402*6a54128fSAndroid Build Coastguard Worker 			     fs->super->s_checkinterval*2))
403*6a54128fSAndroid Build Coastguard Worker 			reason = 0;
404*6a54128fSAndroid Build Coastguard Worker 	} else if (broken_system_clock && fs->super->s_checkinterval) {
405*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s: ", ctx->device_name);
406*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s",
407*6a54128fSAndroid Build Coastguard Worker 			_("ignoring check interval, broken_system_clock set\n"));
408*6a54128fSAndroid Build Coastguard Worker 	}
409*6a54128fSAndroid Build Coastguard Worker 
410*6a54128fSAndroid Build Coastguard Worker 	if (reason) {
411*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", ctx->device_name);
412*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, reason, reason_arg);
413*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", _(", check forced.\n"));
414*6a54128fSAndroid Build Coastguard Worker 		return;
415*6a54128fSAndroid Build Coastguard Worker 	}
416*6a54128fSAndroid Build Coastguard Worker 
417*6a54128fSAndroid Build Coastguard Worker 	/*
418*6a54128fSAndroid Build Coastguard Worker 	 * Update the global counts from the block group counts.  This
419*6a54128fSAndroid Build Coastguard Worker 	 * is needed since modern kernels don't update the global
420*6a54128fSAndroid Build Coastguard Worker 	 * counts so as to avoid locking the entire file system.  So
421*6a54128fSAndroid Build Coastguard Worker 	 * if the filesystem is not unmounted cleanly, the global
422*6a54128fSAndroid Build Coastguard Worker 	 * counts may not be accurate.  Update them here if we can,
423*6a54128fSAndroid Build Coastguard Worker 	 * for the benefit of users who might examine the file system
424*6a54128fSAndroid Build Coastguard Worker 	 * using dumpe2fs.  (This is for cosmetic reasons only.)
425*6a54128fSAndroid Build Coastguard Worker 	 */
426*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
427*6a54128fSAndroid Build Coastguard Worker 	pctx.ino = fs->super->s_free_inodes_count;
428*6a54128fSAndroid Build Coastguard Worker 	pctx.ino2 = ctx->free_inodes;
429*6a54128fSAndroid Build Coastguard Worker 	if ((pctx.ino != pctx.ino2) &&
430*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_READONLY) &&
431*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_FREE_INODE_COUNT, &pctx)) {
432*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_free_inodes_count = ctx->free_inodes;
433*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
434*6a54128fSAndroid Build Coastguard Worker 	}
435*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
436*6a54128fSAndroid Build Coastguard Worker 	pctx.blk = ext2fs_free_blocks_count(fs->super);
437*6a54128fSAndroid Build Coastguard Worker 	pctx.blk2 = ctx->free_blocks;
438*6a54128fSAndroid Build Coastguard Worker 	if ((pctx.blk != pctx.blk2) &&
439*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_READONLY) &&
440*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_FREE_BLOCK_COUNT, &pctx)) {
441*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_blocks_count_set(fs->super, ctx->free_blocks);
442*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
443*6a54128fSAndroid Build Coastguard Worker 	}
444*6a54128fSAndroid Build Coastguard Worker 
445*6a54128fSAndroid Build Coastguard Worker 	/* Print the summary message when we're skipping a full check */
446*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, _("%s: clean, %u/%u files, %llu/%llu blocks"),
447*6a54128fSAndroid Build Coastguard Worker 		ctx->device_name,
448*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_inodes_count - fs->super->s_free_inodes_count,
449*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_inodes_count,
450*6a54128fSAndroid Build Coastguard Worker 		(unsigned long long) ext2fs_blocks_count(fs->super) -
451*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_blocks_count(fs->super),
452*6a54128fSAndroid Build Coastguard Worker 		(unsigned long long) ext2fs_blocks_count(fs->super));
453*6a54128fSAndroid Build Coastguard Worker 	next_check = 100000;
454*6a54128fSAndroid Build Coastguard Worker 	if (fs->super->s_max_mnt_count > 0) {
455*6a54128fSAndroid Build Coastguard Worker 		next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
456*6a54128fSAndroid Build Coastguard Worker 		if (next_check <= 0)
457*6a54128fSAndroid Build Coastguard Worker 			next_check = 1;
458*6a54128fSAndroid Build Coastguard Worker 	}
459*6a54128fSAndroid Build Coastguard Worker 	if (!broken_system_clock && fs->super->s_checkinterval &&
460*6a54128fSAndroid Build Coastguard Worker 	    ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
461*6a54128fSAndroid Build Coastguard Worker 		next_check = 1;
462*6a54128fSAndroid Build Coastguard Worker 	if (next_check <= 5) {
463*6a54128fSAndroid Build Coastguard Worker 		if (next_check == 1) {
464*6a54128fSAndroid Build Coastguard Worker 			if (batt)
465*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, "%s",
466*6a54128fSAndroid Build Coastguard Worker 					_(" (check deferred; on battery)"));
467*6a54128fSAndroid Build Coastguard Worker 			else
468*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, "%s",
469*6a54128fSAndroid Build Coastguard Worker 					_(" (check after next mount)"));
470*6a54128fSAndroid Build Coastguard Worker 		} else
471*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _(" (check in %ld mounts)"),
472*6a54128fSAndroid Build Coastguard Worker 				next_check);
473*6a54128fSAndroid Build Coastguard Worker 	}
474*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, "\n");
475*6a54128fSAndroid Build Coastguard Worker skip:
476*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close_free(&ctx->fs);
477*6a54128fSAndroid Build Coastguard Worker 	e2fsck_free_context(ctx);
478*6a54128fSAndroid Build Coastguard Worker 	exit(FSCK_OK);
479*6a54128fSAndroid Build Coastguard Worker }
480*6a54128fSAndroid Build Coastguard Worker 
481*6a54128fSAndroid Build Coastguard Worker /*
482*6a54128fSAndroid Build Coastguard Worker  * For completion notice
483*6a54128fSAndroid Build Coastguard Worker  */
484*6a54128fSAndroid Build Coastguard Worker struct percent_tbl {
485*6a54128fSAndroid Build Coastguard Worker 	int	max_pass;
486*6a54128fSAndroid Build Coastguard Worker 	int	table[32];
487*6a54128fSAndroid Build Coastguard Worker };
488*6a54128fSAndroid Build Coastguard Worker static struct percent_tbl e2fsck_tbl = {
489*6a54128fSAndroid Build Coastguard Worker 	5, { 0, 70, 90, 92,  95, 100 }
490*6a54128fSAndroid Build Coastguard Worker };
491*6a54128fSAndroid Build Coastguard Worker static char bar[128], spaces[128];
492*6a54128fSAndroid Build Coastguard Worker 
calc_percent(struct percent_tbl * tbl,int pass,int curr,int max)493*6a54128fSAndroid Build Coastguard Worker static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
494*6a54128fSAndroid Build Coastguard Worker 			  int max)
495*6a54128fSAndroid Build Coastguard Worker {
496*6a54128fSAndroid Build Coastguard Worker 	float	percent;
497*6a54128fSAndroid Build Coastguard Worker 
498*6a54128fSAndroid Build Coastguard Worker 	if (pass <= 0)
499*6a54128fSAndroid Build Coastguard Worker 		return 0.0;
500*6a54128fSAndroid Build Coastguard Worker 	if (pass > tbl->max_pass || max == 0)
501*6a54128fSAndroid Build Coastguard Worker 		return 100.0;
502*6a54128fSAndroid Build Coastguard Worker 	percent = ((float) curr) / ((float) max);
503*6a54128fSAndroid Build Coastguard Worker 	return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
504*6a54128fSAndroid Build Coastguard Worker 		+ tbl->table[pass-1]);
505*6a54128fSAndroid Build Coastguard Worker }
506*6a54128fSAndroid Build Coastguard Worker 
e2fsck_clear_progbar(e2fsck_t ctx)507*6a54128fSAndroid Build Coastguard Worker void e2fsck_clear_progbar(e2fsck_t ctx)
508*6a54128fSAndroid Build Coastguard Worker {
509*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->flags & E2F_FLAG_PROG_BAR))
510*6a54128fSAndroid Build Coastguard Worker 		return;
511*6a54128fSAndroid Build Coastguard Worker 
512*6a54128fSAndroid Build Coastguard Worker 	printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
513*6a54128fSAndroid Build Coastguard Worker 	       ctx->stop_meta);
514*6a54128fSAndroid Build Coastguard Worker 	fflush(stdout);
515*6a54128fSAndroid Build Coastguard Worker 	ctx->flags &= ~E2F_FLAG_PROG_BAR;
516*6a54128fSAndroid Build Coastguard Worker }
517*6a54128fSAndroid Build Coastguard Worker 
e2fsck_simple_progress(e2fsck_t ctx,const char * label,float percent,unsigned int dpynum)518*6a54128fSAndroid Build Coastguard Worker int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
519*6a54128fSAndroid Build Coastguard Worker 			   unsigned int dpynum)
520*6a54128fSAndroid Build Coastguard Worker {
521*6a54128fSAndroid Build Coastguard Worker 	static const char spinner[] = "\\|/-";
522*6a54128fSAndroid Build Coastguard Worker 	int	i;
523*6a54128fSAndroid Build Coastguard Worker 	unsigned int	tick;
524*6a54128fSAndroid Build Coastguard Worker 	struct timeval	tv;
525*6a54128fSAndroid Build Coastguard Worker 	int dpywidth;
526*6a54128fSAndroid Build Coastguard Worker 	int fixed_percent;
527*6a54128fSAndroid Build Coastguard Worker 
528*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
529*6a54128fSAndroid Build Coastguard Worker 		return 0;
530*6a54128fSAndroid Build Coastguard Worker 
531*6a54128fSAndroid Build Coastguard Worker 	/*
532*6a54128fSAndroid Build Coastguard Worker 	 * Calculate the new progress position.  If the
533*6a54128fSAndroid Build Coastguard Worker 	 * percentage hasn't changed, then we skip out right
534*6a54128fSAndroid Build Coastguard Worker 	 * away.
535*6a54128fSAndroid Build Coastguard Worker 	 */
536*6a54128fSAndroid Build Coastguard Worker 	fixed_percent = (int) ((10 * percent) + 0.5);
537*6a54128fSAndroid Build Coastguard Worker 	if (ctx->progress_last_percent == fixed_percent)
538*6a54128fSAndroid Build Coastguard Worker 		return 0;
539*6a54128fSAndroid Build Coastguard Worker 	ctx->progress_last_percent = fixed_percent;
540*6a54128fSAndroid Build Coastguard Worker 
541*6a54128fSAndroid Build Coastguard Worker 	/*
542*6a54128fSAndroid Build Coastguard Worker 	 * If we've already updated the spinner once within
543*6a54128fSAndroid Build Coastguard Worker 	 * the last 1/8th of a second, no point doing it
544*6a54128fSAndroid Build Coastguard Worker 	 * again.
545*6a54128fSAndroid Build Coastguard Worker 	 */
546*6a54128fSAndroid Build Coastguard Worker 	gettimeofday(&tv, NULL);
547*6a54128fSAndroid Build Coastguard Worker 	tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
548*6a54128fSAndroid Build Coastguard Worker 	if ((tick == ctx->progress_last_time) &&
549*6a54128fSAndroid Build Coastguard Worker 	    (fixed_percent != 0) && (fixed_percent != 1000))
550*6a54128fSAndroid Build Coastguard Worker 		return 0;
551*6a54128fSAndroid Build Coastguard Worker 	ctx->progress_last_time = tick;
552*6a54128fSAndroid Build Coastguard Worker 
553*6a54128fSAndroid Build Coastguard Worker 	/*
554*6a54128fSAndroid Build Coastguard Worker 	 * Advance the spinner, and note that the progress bar
555*6a54128fSAndroid Build Coastguard Worker 	 * will be on the screen
556*6a54128fSAndroid Build Coastguard Worker 	 */
557*6a54128fSAndroid Build Coastguard Worker 	ctx->progress_pos = (ctx->progress_pos+1) & 3;
558*6a54128fSAndroid Build Coastguard Worker 	ctx->flags |= E2F_FLAG_PROG_BAR;
559*6a54128fSAndroid Build Coastguard Worker 
560*6a54128fSAndroid Build Coastguard Worker 	dpywidth = 66 - strlen(label);
561*6a54128fSAndroid Build Coastguard Worker 	dpywidth = 8 * (dpywidth / 8);
562*6a54128fSAndroid Build Coastguard Worker 	if (dpynum)
563*6a54128fSAndroid Build Coastguard Worker 		dpywidth -= 8;
564*6a54128fSAndroid Build Coastguard Worker 
565*6a54128fSAndroid Build Coastguard Worker 	i = ((percent * dpywidth) + 50) / 100;
566*6a54128fSAndroid Build Coastguard Worker 	printf("%s%s: |%s%s", ctx->start_meta, label,
567*6a54128fSAndroid Build Coastguard Worker 	       bar + (sizeof(bar) - (i+1)),
568*6a54128fSAndroid Build Coastguard Worker 	       spaces + (sizeof(spaces) - (dpywidth - i + 1)));
569*6a54128fSAndroid Build Coastguard Worker 	if (fixed_percent == 1000)
570*6a54128fSAndroid Build Coastguard Worker 		fputc('|', stdout);
571*6a54128fSAndroid Build Coastguard Worker 	else
572*6a54128fSAndroid Build Coastguard Worker 		fputc(spinner[ctx->progress_pos & 3], stdout);
573*6a54128fSAndroid Build Coastguard Worker 	printf(" %4.1f%%  ", percent);
574*6a54128fSAndroid Build Coastguard Worker 	if (dpynum)
575*6a54128fSAndroid Build Coastguard Worker 		printf("%u\r", dpynum);
576*6a54128fSAndroid Build Coastguard Worker 	else
577*6a54128fSAndroid Build Coastguard Worker 		fputs(" \r", stdout);
578*6a54128fSAndroid Build Coastguard Worker 	fputs(ctx->stop_meta, stdout);
579*6a54128fSAndroid Build Coastguard Worker 
580*6a54128fSAndroid Build Coastguard Worker 	if (fixed_percent == 1000)
581*6a54128fSAndroid Build Coastguard Worker 		e2fsck_clear_progbar(ctx);
582*6a54128fSAndroid Build Coastguard Worker 	fflush(stdout);
583*6a54128fSAndroid Build Coastguard Worker 
584*6a54128fSAndroid Build Coastguard Worker 	return 0;
585*6a54128fSAndroid Build Coastguard Worker }
586*6a54128fSAndroid Build Coastguard Worker 
e2fsck_update_progress(e2fsck_t ctx,int pass,unsigned long cur,unsigned long max)587*6a54128fSAndroid Build Coastguard Worker static int e2fsck_update_progress(e2fsck_t ctx, int pass,
588*6a54128fSAndroid Build Coastguard Worker 				  unsigned long cur, unsigned long max)
589*6a54128fSAndroid Build Coastguard Worker {
590*6a54128fSAndroid Build Coastguard Worker 	char buf[1024];
591*6a54128fSAndroid Build Coastguard Worker 	float percent;
592*6a54128fSAndroid Build Coastguard Worker 
593*6a54128fSAndroid Build Coastguard Worker 	if (pass == 0)
594*6a54128fSAndroid Build Coastguard Worker 		return 0;
595*6a54128fSAndroid Build Coastguard Worker 
596*6a54128fSAndroid Build Coastguard Worker 	if (ctx->progress_fd) {
597*6a54128fSAndroid Build Coastguard Worker 		snprintf(buf, sizeof(buf), "%d %lu %lu %s\n",
598*6a54128fSAndroid Build Coastguard Worker 			 pass, cur, max, ctx->device_name);
599*6a54128fSAndroid Build Coastguard Worker 		write_all(ctx->progress_fd, buf, strlen(buf));
600*6a54128fSAndroid Build Coastguard Worker 	} else {
601*6a54128fSAndroid Build Coastguard Worker 		percent = calc_percent(&e2fsck_tbl, pass, cur, max);
602*6a54128fSAndroid Build Coastguard Worker 		e2fsck_simple_progress(ctx, ctx->device_name,
603*6a54128fSAndroid Build Coastguard Worker 				       percent, 0);
604*6a54128fSAndroid Build Coastguard Worker 	}
605*6a54128fSAndroid Build Coastguard Worker 	return 0;
606*6a54128fSAndroid Build Coastguard Worker }
607*6a54128fSAndroid Build Coastguard Worker 
608*6a54128fSAndroid Build Coastguard Worker #define PATH_SET "PATH=/sbin"
609*6a54128fSAndroid Build Coastguard Worker 
610*6a54128fSAndroid Build Coastguard Worker /*
611*6a54128fSAndroid Build Coastguard Worker  * Make sure 0,1,2 file descriptors are open, so that we don't open
612*6a54128fSAndroid Build Coastguard Worker  * the filesystem using the same file descriptor as stdout or stderr.
613*6a54128fSAndroid Build Coastguard Worker  */
reserve_stdio_fds(void)614*6a54128fSAndroid Build Coastguard Worker static void reserve_stdio_fds(void)
615*6a54128fSAndroid Build Coastguard Worker {
616*6a54128fSAndroid Build Coastguard Worker 	int	fd = 0;
617*6a54128fSAndroid Build Coastguard Worker 
618*6a54128fSAndroid Build Coastguard Worker 	while (fd <= 2) {
619*6a54128fSAndroid Build Coastguard Worker 		fd = open("/dev/null", O_RDWR);
620*6a54128fSAndroid Build Coastguard Worker 		if (fd < 0) {
621*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("ERROR: Couldn't open "
622*6a54128fSAndroid Build Coastguard Worker 				"/dev/null (%s)\n"),
623*6a54128fSAndroid Build Coastguard Worker 				strerror(errno));
624*6a54128fSAndroid Build Coastguard Worker 			return;
625*6a54128fSAndroid Build Coastguard Worker 		}
626*6a54128fSAndroid Build Coastguard Worker 	}
627*6a54128fSAndroid Build Coastguard Worker 	(void) close(fd);
628*6a54128fSAndroid Build Coastguard Worker }
629*6a54128fSAndroid Build Coastguard Worker 
630*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SIGNAL_H
signal_progress_on(int sig EXT2FS_ATTR ((unused)))631*6a54128fSAndroid Build Coastguard Worker static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
632*6a54128fSAndroid Build Coastguard Worker {
633*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t ctx = e2fsck_global_ctx;
634*6a54128fSAndroid Build Coastguard Worker 
635*6a54128fSAndroid Build Coastguard Worker 	if (!ctx)
636*6a54128fSAndroid Build Coastguard Worker 		return;
637*6a54128fSAndroid Build Coastguard Worker 
638*6a54128fSAndroid Build Coastguard Worker 	ctx->progress = e2fsck_update_progress;
639*6a54128fSAndroid Build Coastguard Worker }
640*6a54128fSAndroid Build Coastguard Worker 
signal_progress_off(int sig EXT2FS_ATTR ((unused)))641*6a54128fSAndroid Build Coastguard Worker static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
642*6a54128fSAndroid Build Coastguard Worker {
643*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t ctx = e2fsck_global_ctx;
644*6a54128fSAndroid Build Coastguard Worker 
645*6a54128fSAndroid Build Coastguard Worker 	if (!ctx)
646*6a54128fSAndroid Build Coastguard Worker 		return;
647*6a54128fSAndroid Build Coastguard Worker 
648*6a54128fSAndroid Build Coastguard Worker 	e2fsck_clear_progbar(ctx);
649*6a54128fSAndroid Build Coastguard Worker 	ctx->progress = 0;
650*6a54128fSAndroid Build Coastguard Worker }
651*6a54128fSAndroid Build Coastguard Worker 
signal_cancel(int sig EXT2FS_ATTR ((unused)))652*6a54128fSAndroid Build Coastguard Worker static void signal_cancel(int sig EXT2FS_ATTR((unused)))
653*6a54128fSAndroid Build Coastguard Worker {
654*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t ctx = e2fsck_global_ctx;
655*6a54128fSAndroid Build Coastguard Worker 
656*6a54128fSAndroid Build Coastguard Worker 	if (!ctx)
657*6a54128fSAndroid Build Coastguard Worker 		exit(FSCK_CANCELED);
658*6a54128fSAndroid Build Coastguard Worker 
659*6a54128fSAndroid Build Coastguard Worker 	ctx->flags |= E2F_FLAG_CANCEL;
660*6a54128fSAndroid Build Coastguard Worker }
661*6a54128fSAndroid Build Coastguard Worker #endif
662*6a54128fSAndroid Build Coastguard Worker 
parse_extended_opts(e2fsck_t ctx,const char * opts)663*6a54128fSAndroid Build Coastguard Worker static void parse_extended_opts(e2fsck_t ctx, const char *opts)
664*6a54128fSAndroid Build Coastguard Worker {
665*6a54128fSAndroid Build Coastguard Worker 	char	*buf, *token, *next, *p, *arg;
666*6a54128fSAndroid Build Coastguard Worker 	int	ea_ver;
667*6a54128fSAndroid Build Coastguard Worker 	int	extended_usage = 0;
668*6a54128fSAndroid Build Coastguard Worker 	unsigned long long reada_kb;
669*6a54128fSAndroid Build Coastguard Worker 
670*6a54128fSAndroid Build Coastguard Worker 	buf = string_copy(ctx, opts, 0);
671*6a54128fSAndroid Build Coastguard Worker 	for (token = buf; token && *token; token = next) {
672*6a54128fSAndroid Build Coastguard Worker 		p = strchr(token, ',');
673*6a54128fSAndroid Build Coastguard Worker 		next = 0;
674*6a54128fSAndroid Build Coastguard Worker 		if (p) {
675*6a54128fSAndroid Build Coastguard Worker 			*p = 0;
676*6a54128fSAndroid Build Coastguard Worker 			next = p+1;
677*6a54128fSAndroid Build Coastguard Worker 		}
678*6a54128fSAndroid Build Coastguard Worker 		arg = strchr(token, '=');
679*6a54128fSAndroid Build Coastguard Worker 		if (arg) {
680*6a54128fSAndroid Build Coastguard Worker 			*arg = 0;
681*6a54128fSAndroid Build Coastguard Worker 			arg++;
682*6a54128fSAndroid Build Coastguard Worker 		}
683*6a54128fSAndroid Build Coastguard Worker 		if (strcmp(token, "ea_ver") == 0) {
684*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
685*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
686*6a54128fSAndroid Build Coastguard Worker 				continue;
687*6a54128fSAndroid Build Coastguard Worker 			}
688*6a54128fSAndroid Build Coastguard Worker 			ea_ver = strtoul(arg, &p, 0);
689*6a54128fSAndroid Build Coastguard Worker 			if (*p ||
690*6a54128fSAndroid Build Coastguard Worker 			    ((ea_ver != 1) && (ea_ver != 2))) {
691*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr, "%s",
692*6a54128fSAndroid Build Coastguard Worker 					_("Invalid EA version.\n"));
693*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
694*6a54128fSAndroid Build Coastguard Worker 				continue;
695*6a54128fSAndroid Build Coastguard Worker 			}
696*6a54128fSAndroid Build Coastguard Worker 			ctx->ext_attr_ver = ea_ver;
697*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "readahead_kb") == 0) {
698*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
699*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
700*6a54128fSAndroid Build Coastguard Worker 				continue;
701*6a54128fSAndroid Build Coastguard Worker 			}
702*6a54128fSAndroid Build Coastguard Worker 			reada_kb = strtoull(arg, &p, 0);
703*6a54128fSAndroid Build Coastguard Worker 			if (*p) {
704*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr, "%s",
705*6a54128fSAndroid Build Coastguard Worker 					_("Invalid readahead buffer size.\n"));
706*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
707*6a54128fSAndroid Build Coastguard Worker 				continue;
708*6a54128fSAndroid Build Coastguard Worker 			}
709*6a54128fSAndroid Build Coastguard Worker 			ctx->readahead_kb = reada_kb;
710*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "fragcheck") == 0) {
711*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_FRAGCHECK;
712*6a54128fSAndroid Build Coastguard Worker 			continue;
713*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "journal_only") == 0) {
714*6a54128fSAndroid Build Coastguard Worker 			if (arg) {
715*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
716*6a54128fSAndroid Build Coastguard Worker 				continue;
717*6a54128fSAndroid Build Coastguard Worker 			}
718*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_JOURNAL_ONLY;
719*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "discard") == 0) {
720*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_DISCARD;
721*6a54128fSAndroid Build Coastguard Worker 			continue;
722*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "nodiscard") == 0) {
723*6a54128fSAndroid Build Coastguard Worker 			ctx->options &= ~E2F_OPT_DISCARD;
724*6a54128fSAndroid Build Coastguard Worker 			continue;
725*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "optimize_extents") == 0) {
726*6a54128fSAndroid Build Coastguard Worker 			ctx->options &= ~E2F_OPT_NOOPT_EXTENTS;
727*6a54128fSAndroid Build Coastguard Worker 			continue;
728*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "no_optimize_extents") == 0) {
729*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_NOOPT_EXTENTS;
730*6a54128fSAndroid Build Coastguard Worker 			continue;
731*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "inode_count_fullmap") == 0) {
732*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_ICOUNT_FULLMAP;
733*6a54128fSAndroid Build Coastguard Worker 			continue;
734*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "no_inode_count_fullmap") == 0) {
735*6a54128fSAndroid Build Coastguard Worker 			ctx->options &= ~E2F_OPT_ICOUNT_FULLMAP;
736*6a54128fSAndroid Build Coastguard Worker 			continue;
737*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "log_filename") == 0) {
738*6a54128fSAndroid Build Coastguard Worker 			if (!arg)
739*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
740*6a54128fSAndroid Build Coastguard Worker 			else
741*6a54128fSAndroid Build Coastguard Worker 				ctx->log_fn = string_copy(ctx, arg, 0);
742*6a54128fSAndroid Build Coastguard Worker 			continue;
743*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "problem_log") == 0) {
744*6a54128fSAndroid Build Coastguard Worker 			if (!arg)
745*6a54128fSAndroid Build Coastguard Worker 				extended_usage++;
746*6a54128fSAndroid Build Coastguard Worker 			else
747*6a54128fSAndroid Build Coastguard Worker 				ctx->problem_log_fn = string_copy(ctx, arg, 0);
748*6a54128fSAndroid Build Coastguard Worker 			continue;
749*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "bmap2extent") == 0) {
750*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_CONVERT_BMAP;
751*6a54128fSAndroid Build Coastguard Worker 			continue;
752*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "fixes_only") == 0) {
753*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_FIXES_ONLY;
754*6a54128fSAndroid Build Coastguard Worker 			continue;
755*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "unshare_blocks") == 0) {
756*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_UNSHARE_BLOCKS;
757*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_FORCE;
758*6a54128fSAndroid Build Coastguard Worker 			continue;
759*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "check_encoding") == 0) {
760*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_CHECK_ENCODING;
761*6a54128fSAndroid Build Coastguard Worker 			continue;
762*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_DEVELOPER_FEATURES
763*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "clear_all_uninit_bits") == 0) {
764*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_CLEAR_UNINIT;
765*6a54128fSAndroid Build Coastguard Worker 			continue;
766*6a54128fSAndroid Build Coastguard Worker #endif
767*6a54128fSAndroid Build Coastguard Worker 		} else {
768*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Unknown extended option: %s\n"),
769*6a54128fSAndroid Build Coastguard Worker 				token);
770*6a54128fSAndroid Build Coastguard Worker 			extended_usage++;
771*6a54128fSAndroid Build Coastguard Worker 		}
772*6a54128fSAndroid Build Coastguard Worker 	}
773*6a54128fSAndroid Build Coastguard Worker 	free(buf);
774*6a54128fSAndroid Build Coastguard Worker 
775*6a54128fSAndroid Build Coastguard Worker 	if (extended_usage) {
776*6a54128fSAndroid Build Coastguard Worker 		fputs(_("\nExtended options are separated by commas, "
777*6a54128fSAndroid Build Coastguard Worker 		       "and may take an argument which\n"
778*6a54128fSAndroid Build Coastguard Worker 		       "is set off by an equals ('=') sign.  "
779*6a54128fSAndroid Build Coastguard Worker 		       "Valid extended options are:\n\n"), stderr);
780*6a54128fSAndroid Build Coastguard Worker 		fputs(_("\tea_ver=<ea_version (1 or 2)>\n"), stderr);
781*6a54128fSAndroid Build Coastguard Worker 		fputs("\tfragcheck\n", stderr);
782*6a54128fSAndroid Build Coastguard Worker 		fputs("\tjournal_only\n", stderr);
783*6a54128fSAndroid Build Coastguard Worker 		fputs("\tdiscard\n", stderr);
784*6a54128fSAndroid Build Coastguard Worker 		fputs("\tnodiscard\n", stderr);
785*6a54128fSAndroid Build Coastguard Worker 		fputs("\toptimize_extents\n", stderr);
786*6a54128fSAndroid Build Coastguard Worker 		fputs("\tno_optimize_extents\n", stderr);
787*6a54128fSAndroid Build Coastguard Worker 		fputs("\tinode_count_fullmap\n", stderr);
788*6a54128fSAndroid Build Coastguard Worker 		fputs("\tno_inode_count_fullmap\n", stderr);
789*6a54128fSAndroid Build Coastguard Worker 		fputs(_("\treadahead_kb=<buffer size>\n"), stderr);
790*6a54128fSAndroid Build Coastguard Worker 		fputs("\tbmap2extent\n", stderr);
791*6a54128fSAndroid Build Coastguard Worker 		fputs("\tunshare_blocks\n", stderr);
792*6a54128fSAndroid Build Coastguard Worker 		fputs("\tfixes_only\n", stderr);
793*6a54128fSAndroid Build Coastguard Worker 		fputs("\tcheck_encoding\n", stderr);
794*6a54128fSAndroid Build Coastguard Worker 		fputc('\n', stderr);
795*6a54128fSAndroid Build Coastguard Worker 		exit(1);
796*6a54128fSAndroid Build Coastguard Worker 	}
797*6a54128fSAndroid Build Coastguard Worker }
798*6a54128fSAndroid Build Coastguard Worker 
syntax_err_report(const char * filename,long err,int line_num)799*6a54128fSAndroid Build Coastguard Worker static void syntax_err_report(const char *filename, long err, int line_num)
800*6a54128fSAndroid Build Coastguard Worker {
801*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr,
802*6a54128fSAndroid Build Coastguard Worker 		_("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
803*6a54128fSAndroid Build Coastguard Worker 		filename, line_num, error_message(err));
804*6a54128fSAndroid Build Coastguard Worker 	exit(FSCK_ERROR);
805*6a54128fSAndroid Build Coastguard Worker }
806*6a54128fSAndroid Build Coastguard Worker 
807*6a54128fSAndroid Build Coastguard Worker static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
808*6a54128fSAndroid Build Coastguard Worker 
PRS(int argc,char * argv[],e2fsck_t * ret_ctx)809*6a54128fSAndroid Build Coastguard Worker static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
810*6a54128fSAndroid Build Coastguard Worker {
811*6a54128fSAndroid Build Coastguard Worker 	int		flush = 0;
812*6a54128fSAndroid Build Coastguard Worker 	int		c, fd;
813*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
814*6a54128fSAndroid Build Coastguard Worker 	extern void	*mallwatch;
815*6a54128fSAndroid Build Coastguard Worker #endif
816*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t	ctx;
817*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
818*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SIGNAL_H
819*6a54128fSAndroid Build Coastguard Worker 	struct sigaction	sa;
820*6a54128fSAndroid Build Coastguard Worker #endif
821*6a54128fSAndroid Build Coastguard Worker 	char		*extended_opts = 0;
822*6a54128fSAndroid Build Coastguard Worker 	char		*cp;
823*6a54128fSAndroid Build Coastguard Worker 	int 		res;		/* result of sscanf */
824*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_JBD_DEBUG
825*6a54128fSAndroid Build Coastguard Worker 	char 		*jbd_debug;
826*6a54128fSAndroid Build Coastguard Worker #endif
827*6a54128fSAndroid Build Coastguard Worker 	unsigned long long phys_mem_kb, blk;
828*6a54128fSAndroid Build Coastguard Worker 
829*6a54128fSAndroid Build Coastguard Worker 	retval = e2fsck_allocate_context(&ctx);
830*6a54128fSAndroid Build Coastguard Worker 	if (retval)
831*6a54128fSAndroid Build Coastguard Worker 		return retval;
832*6a54128fSAndroid Build Coastguard Worker 
833*6a54128fSAndroid Build Coastguard Worker 	*ret_ctx = ctx;
834*6a54128fSAndroid Build Coastguard Worker 	e2fsck_global_ctx = ctx;
835*6a54128fSAndroid Build Coastguard Worker 
836*6a54128fSAndroid Build Coastguard Worker 	setvbuf(stdout, NULL, _IONBF, BUFSIZ);
837*6a54128fSAndroid Build Coastguard Worker 	setvbuf(stderr, NULL, _IONBF, BUFSIZ);
838*6a54128fSAndroid Build Coastguard Worker 	if (getenv("E2FSCK_FORCE_INTERACTIVE") || (isatty(0) && isatty(1))) {
839*6a54128fSAndroid Build Coastguard Worker 		ctx->interactive = 1;
840*6a54128fSAndroid Build Coastguard Worker 	} else {
841*6a54128fSAndroid Build Coastguard Worker 		ctx->start_meta[0] = '\001';
842*6a54128fSAndroid Build Coastguard Worker 		ctx->stop_meta[0] = '\002';
843*6a54128fSAndroid Build Coastguard Worker 	}
844*6a54128fSAndroid Build Coastguard Worker 	memset(bar, '=', sizeof(bar)-1);
845*6a54128fSAndroid Build Coastguard Worker 	memset(spaces, ' ', sizeof(spaces)-1);
846*6a54128fSAndroid Build Coastguard Worker 	add_error_table(&et_ext2_error_table);
847*6a54128fSAndroid Build Coastguard Worker 	add_error_table(&et_prof_error_table);
848*6a54128fSAndroid Build Coastguard Worker 	blkid_get_cache(&ctx->blkid, NULL);
849*6a54128fSAndroid Build Coastguard Worker 
850*6a54128fSAndroid Build Coastguard Worker 	if (argc && *argv)
851*6a54128fSAndroid Build Coastguard Worker 		ctx->program_name = *argv;
852*6a54128fSAndroid Build Coastguard Worker 	else
853*6a54128fSAndroid Build Coastguard Worker 		usage(NULL);
854*6a54128fSAndroid Build Coastguard Worker 
855*6a54128fSAndroid Build Coastguard Worker 	phys_mem_kb = get_memory_size() / 1024;
856*6a54128fSAndroid Build Coastguard Worker 	ctx->readahead_kb = ~0ULL;
857*6a54128fSAndroid Build Coastguard Worker 	while ((c = getopt(argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDkz:")) != EOF)
858*6a54128fSAndroid Build Coastguard Worker 		switch (c) {
859*6a54128fSAndroid Build Coastguard Worker 		case 'C':
860*6a54128fSAndroid Build Coastguard Worker 			ctx->progress = e2fsck_update_progress;
861*6a54128fSAndroid Build Coastguard Worker 			res = sscanf(optarg, "%d", &ctx->progress_fd);
862*6a54128fSAndroid Build Coastguard Worker 			if (res != 1)
863*6a54128fSAndroid Build Coastguard Worker 				goto sscanf_err;
864*6a54128fSAndroid Build Coastguard Worker 
865*6a54128fSAndroid Build Coastguard Worker 			if (ctx->progress_fd < 0) {
866*6a54128fSAndroid Build Coastguard Worker 				ctx->progress = 0;
867*6a54128fSAndroid Build Coastguard Worker 				ctx->progress_fd = ctx->progress_fd * -1;
868*6a54128fSAndroid Build Coastguard Worker 			}
869*6a54128fSAndroid Build Coastguard Worker 			if (!ctx->progress_fd)
870*6a54128fSAndroid Build Coastguard Worker 				break;
871*6a54128fSAndroid Build Coastguard Worker 			/* Validate the file descriptor to avoid disasters */
872*6a54128fSAndroid Build Coastguard Worker 			fd = dup(ctx->progress_fd);
873*6a54128fSAndroid Build Coastguard Worker 			if (fd < 0) {
874*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
875*6a54128fSAndroid Build Coastguard Worker 				_("Error validating file descriptor %d: %s\n"),
876*6a54128fSAndroid Build Coastguard Worker 					ctx->progress_fd,
877*6a54128fSAndroid Build Coastguard Worker 					error_message(errno));
878*6a54128fSAndroid Build Coastguard Worker 				fatal_error(ctx,
879*6a54128fSAndroid Build Coastguard Worker 			_("Invalid completion information file descriptor"));
880*6a54128fSAndroid Build Coastguard Worker 			} else
881*6a54128fSAndroid Build Coastguard Worker 				close(fd);
882*6a54128fSAndroid Build Coastguard Worker 			break;
883*6a54128fSAndroid Build Coastguard Worker 		case 'D':
884*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_COMPRESS_DIRS;
885*6a54128fSAndroid Build Coastguard Worker 			break;
886*6a54128fSAndroid Build Coastguard Worker 		case 'E':
887*6a54128fSAndroid Build Coastguard Worker 			extended_opts = optarg;
888*6a54128fSAndroid Build Coastguard Worker 			break;
889*6a54128fSAndroid Build Coastguard Worker 		case 'p':
890*6a54128fSAndroid Build Coastguard Worker 		case 'a':
891*6a54128fSAndroid Build Coastguard Worker 			if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
892*6a54128fSAndroid Build Coastguard Worker 			conflict_opt:
893*6a54128fSAndroid Build Coastguard Worker 				fatal_error(ctx,
894*6a54128fSAndroid Build Coastguard Worker 	_("Only one of the options -p/-a, -n or -y may be specified."));
895*6a54128fSAndroid Build Coastguard Worker 			}
896*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_PREEN;
897*6a54128fSAndroid Build Coastguard Worker 			break;
898*6a54128fSAndroid Build Coastguard Worker 		case 'n':
899*6a54128fSAndroid Build Coastguard Worker 			if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
900*6a54128fSAndroid Build Coastguard Worker 				goto conflict_opt;
901*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_NO;
902*6a54128fSAndroid Build Coastguard Worker 			break;
903*6a54128fSAndroid Build Coastguard Worker 		case 'y':
904*6a54128fSAndroid Build Coastguard Worker 			if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
905*6a54128fSAndroid Build Coastguard Worker 				goto conflict_opt;
906*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_YES;
907*6a54128fSAndroid Build Coastguard Worker 			break;
908*6a54128fSAndroid Build Coastguard Worker 		case 't':
909*6a54128fSAndroid Build Coastguard Worker #ifdef RESOURCE_TRACK
910*6a54128fSAndroid Build Coastguard Worker 			if (ctx->options & E2F_OPT_TIME)
911*6a54128fSAndroid Build Coastguard Worker 				ctx->options |= E2F_OPT_TIME2;
912*6a54128fSAndroid Build Coastguard Worker 			else
913*6a54128fSAndroid Build Coastguard Worker 				ctx->options |= E2F_OPT_TIME;
914*6a54128fSAndroid Build Coastguard Worker #else
915*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("The -t option is not "
916*6a54128fSAndroid Build Coastguard Worker 				"supported on this version of e2fsck.\n"));
917*6a54128fSAndroid Build Coastguard Worker #endif
918*6a54128fSAndroid Build Coastguard Worker 			break;
919*6a54128fSAndroid Build Coastguard Worker 		case 'c':
920*6a54128fSAndroid Build Coastguard Worker 			if (cflag++)
921*6a54128fSAndroid Build Coastguard Worker 				ctx->options |= E2F_OPT_WRITECHECK;
922*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_CHECKBLOCKS;
923*6a54128fSAndroid Build Coastguard Worker 			break;
924*6a54128fSAndroid Build Coastguard Worker 		case 'r':
925*6a54128fSAndroid Build Coastguard Worker 			/* What we do by default, anyway! */
926*6a54128fSAndroid Build Coastguard Worker 			break;
927*6a54128fSAndroid Build Coastguard Worker 		case 'b':
928*6a54128fSAndroid Build Coastguard Worker 			res = sscanf(optarg, "%llu", &blk);
929*6a54128fSAndroid Build Coastguard Worker 			ctx->use_superblock = blk;
930*6a54128fSAndroid Build Coastguard Worker 			if (res != 1)
931*6a54128fSAndroid Build Coastguard Worker 				goto sscanf_err;
932*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_SB_SPECIFIED;
933*6a54128fSAndroid Build Coastguard Worker 			break;
934*6a54128fSAndroid Build Coastguard Worker 		case 'B':
935*6a54128fSAndroid Build Coastguard Worker 			ctx->blocksize = atoi(optarg);
936*6a54128fSAndroid Build Coastguard Worker 			break;
937*6a54128fSAndroid Build Coastguard Worker 		case 'I':
938*6a54128fSAndroid Build Coastguard Worker 			res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
939*6a54128fSAndroid Build Coastguard Worker 			if (res != 1)
940*6a54128fSAndroid Build Coastguard Worker 				goto sscanf_err;
941*6a54128fSAndroid Build Coastguard Worker 			break;
942*6a54128fSAndroid Build Coastguard Worker 		case 'j':
943*6a54128fSAndroid Build Coastguard Worker 			ctx->journal_name = get_devname(ctx->blkid,
944*6a54128fSAndroid Build Coastguard Worker 							optarg, NULL);
945*6a54128fSAndroid Build Coastguard Worker 			if (!ctx->journal_name) {
946*6a54128fSAndroid Build Coastguard Worker 				com_err(ctx->program_name, 0,
947*6a54128fSAndroid Build Coastguard Worker 					_("Unable to resolve '%s'"),
948*6a54128fSAndroid Build Coastguard Worker 					optarg);
949*6a54128fSAndroid Build Coastguard Worker 				fatal_error(ctx, 0);
950*6a54128fSAndroid Build Coastguard Worker 			}
951*6a54128fSAndroid Build Coastguard Worker 			break;
952*6a54128fSAndroid Build Coastguard Worker 		case 'P':
953*6a54128fSAndroid Build Coastguard Worker 			res = sscanf(optarg, "%d", &ctx->process_inode_size);
954*6a54128fSAndroid Build Coastguard Worker 			if (res != 1)
955*6a54128fSAndroid Build Coastguard Worker 				goto sscanf_err;
956*6a54128fSAndroid Build Coastguard Worker 			break;
957*6a54128fSAndroid Build Coastguard Worker 		case 'L':
958*6a54128fSAndroid Build Coastguard Worker 			replace_bad_blocks++;
959*6a54128fSAndroid Build Coastguard Worker 			/* fall through */
960*6a54128fSAndroid Build Coastguard Worker 		case 'l':
961*6a54128fSAndroid Build Coastguard Worker 			if (bad_blocks_file)
962*6a54128fSAndroid Build Coastguard Worker 				free(bad_blocks_file);
963*6a54128fSAndroid Build Coastguard Worker 			bad_blocks_file = string_copy(ctx, optarg, 0);
964*6a54128fSAndroid Build Coastguard Worker 			break;
965*6a54128fSAndroid Build Coastguard Worker 		case 'd':
966*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_DEBUG;
967*6a54128fSAndroid Build Coastguard Worker 			break;
968*6a54128fSAndroid Build Coastguard Worker 		case 'f':
969*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_FORCE;
970*6a54128fSAndroid Build Coastguard Worker 			break;
971*6a54128fSAndroid Build Coastguard Worker 		case 'F':
972*6a54128fSAndroid Build Coastguard Worker 			flush = 1;
973*6a54128fSAndroid Build Coastguard Worker 			break;
974*6a54128fSAndroid Build Coastguard Worker 		case 'v':
975*6a54128fSAndroid Build Coastguard Worker 			verbose = 1;
976*6a54128fSAndroid Build Coastguard Worker 			break;
977*6a54128fSAndroid Build Coastguard Worker 		case 'V':
978*6a54128fSAndroid Build Coastguard Worker 			show_version_only = 1;
979*6a54128fSAndroid Build Coastguard Worker 			break;
980*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
981*6a54128fSAndroid Build Coastguard Worker 		case 'M':
982*6a54128fSAndroid Build Coastguard Worker 			mallwatch = (void *) strtol(optarg, NULL, 0);
983*6a54128fSAndroid Build Coastguard Worker 			break;
984*6a54128fSAndroid Build Coastguard Worker #endif
985*6a54128fSAndroid Build Coastguard Worker 		case 'N':
986*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name = string_copy(ctx, optarg, 0);
987*6a54128fSAndroid Build Coastguard Worker 			break;
988*6a54128fSAndroid Build Coastguard Worker 		case 'k':
989*6a54128fSAndroid Build Coastguard Worker 			keep_bad_blocks++;
990*6a54128fSAndroid Build Coastguard Worker 			break;
991*6a54128fSAndroid Build Coastguard Worker 		case 'z':
992*6a54128fSAndroid Build Coastguard Worker 			ctx->undo_file = optarg;
993*6a54128fSAndroid Build Coastguard Worker 			break;
994*6a54128fSAndroid Build Coastguard Worker 		default:
995*6a54128fSAndroid Build Coastguard Worker 			usage(ctx);
996*6a54128fSAndroid Build Coastguard Worker 		}
997*6a54128fSAndroid Build Coastguard Worker 	if (show_version_only)
998*6a54128fSAndroid Build Coastguard Worker 		return 0;
999*6a54128fSAndroid Build Coastguard Worker 	if (optind != argc - 1)
1000*6a54128fSAndroid Build Coastguard Worker 		usage(ctx);
1001*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_NO) &&
1002*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_COMPRESS_DIRS)) {
1003*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0, "%s",
1004*6a54128fSAndroid Build Coastguard Worker 			_("The -n and -D options are incompatible."));
1005*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1006*6a54128fSAndroid Build Coastguard Worker 	}
1007*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_NO) && cflag) {
1008*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0, "%s",
1009*6a54128fSAndroid Build Coastguard Worker 			_("The -n and -c options are incompatible."));
1010*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1011*6a54128fSAndroid Build Coastguard Worker 	}
1012*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_NO) && bad_blocks_file) {
1013*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0, "%s",
1014*6a54128fSAndroid Build Coastguard Worker 			_("The -n and -l/-L options are incompatible."));
1015*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1016*6a54128fSAndroid Build Coastguard Worker 	}
1017*6a54128fSAndroid Build Coastguard Worker 	if (ctx->options & E2F_OPT_NO)
1018*6a54128fSAndroid Build Coastguard Worker 		ctx->options |= E2F_OPT_READONLY;
1019*6a54128fSAndroid Build Coastguard Worker 
1020*6a54128fSAndroid Build Coastguard Worker 	ctx->io_options = strchr(argv[optind], '?');
1021*6a54128fSAndroid Build Coastguard Worker 	if (ctx->io_options)
1022*6a54128fSAndroid Build Coastguard Worker 		*ctx->io_options++ = 0;
1023*6a54128fSAndroid Build Coastguard Worker 	ctx->filesystem_name = get_devname(ctx->blkid, argv[optind], 0);
1024*6a54128fSAndroid Build Coastguard Worker 	if (!ctx->filesystem_name) {
1025*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
1026*6a54128fSAndroid Build Coastguard Worker 			argv[optind]);
1027*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1028*6a54128fSAndroid Build Coastguard Worker 	}
1029*6a54128fSAndroid Build Coastguard Worker 	if (extended_opts)
1030*6a54128fSAndroid Build Coastguard Worker 		parse_extended_opts(ctx, extended_opts);
1031*6a54128fSAndroid Build Coastguard Worker 
1032*6a54128fSAndroid Build Coastguard Worker 	/* Complain about mutually exclusive rebuilding activities */
1033*6a54128fSAndroid Build Coastguard Worker 	if (getenv("E2FSCK_FIXES_ONLY"))
1034*6a54128fSAndroid Build Coastguard Worker 		ctx->options |= E2F_OPT_FIXES_ONLY;
1035*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_COMPRESS_DIRS) &&
1036*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_FIXES_ONLY)) {
1037*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0, "%s",
1038*6a54128fSAndroid Build Coastguard Worker 			_("The -D and -E fixes_only options are incompatible."));
1039*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1040*6a54128fSAndroid Build Coastguard Worker 	}
1041*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_CONVERT_BMAP) &&
1042*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_FIXES_ONLY)) {
1043*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0, "%s",
1044*6a54128fSAndroid Build Coastguard Worker 			_("The -E bmap2extent and fixes_only options are incompatible."));
1045*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1046*6a54128fSAndroid Build Coastguard Worker 	}
1047*6a54128fSAndroid Build Coastguard Worker 
1048*6a54128fSAndroid Build Coastguard Worker 	if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
1049*6a54128fSAndroid Build Coastguard Worker 		config_fn[0] = cp;
1050*6a54128fSAndroid Build Coastguard Worker 	profile_set_syntax_err_cb(syntax_err_report);
1051*6a54128fSAndroid Build Coastguard Worker 	profile_init(config_fn, &ctx->profile);
1052*6a54128fSAndroid Build Coastguard Worker 
1053*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "report_time", 0, 0,
1054*6a54128fSAndroid Build Coastguard Worker 			    &c);
1055*6a54128fSAndroid Build Coastguard Worker 	if (c)
1056*6a54128fSAndroid Build Coastguard Worker 		ctx->options |= E2F_OPT_TIME | E2F_OPT_TIME2;
1057*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "report_verbose", 0, 0,
1058*6a54128fSAndroid Build Coastguard Worker 			    &c);
1059*6a54128fSAndroid Build Coastguard Worker 	if (c)
1060*6a54128fSAndroid Build Coastguard Worker 		verbose = 1;
1061*6a54128fSAndroid Build Coastguard Worker 
1062*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "no_optimize_extents",
1063*6a54128fSAndroid Build Coastguard Worker 			    0, 0, &c);
1064*6a54128fSAndroid Build Coastguard Worker 	if (c)
1065*6a54128fSAndroid Build Coastguard Worker 		ctx->options |= E2F_OPT_NOOPT_EXTENTS;
1066*6a54128fSAndroid Build Coastguard Worker 
1067*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "inode_count_fullmap",
1068*6a54128fSAndroid Build Coastguard Worker 			    0, 0, &c);
1069*6a54128fSAndroid Build Coastguard Worker 	if (c)
1070*6a54128fSAndroid Build Coastguard Worker 		ctx->options |= E2F_OPT_ICOUNT_FULLMAP;
1071*6a54128fSAndroid Build Coastguard Worker 
1072*6a54128fSAndroid Build Coastguard Worker 	if (ctx->readahead_kb == ~0ULL) {
1073*6a54128fSAndroid Build Coastguard Worker 		profile_get_integer(ctx->profile, "options",
1074*6a54128fSAndroid Build Coastguard Worker 				    "readahead_mem_pct", 0, -1, &c);
1075*6a54128fSAndroid Build Coastguard Worker 		if (c >= 0 && c <= 100)
1076*6a54128fSAndroid Build Coastguard Worker 			ctx->readahead_kb = phys_mem_kb * c / 100;
1077*6a54128fSAndroid Build Coastguard Worker 		profile_get_integer(ctx->profile, "options",
1078*6a54128fSAndroid Build Coastguard Worker 				    "readahead_kb", 0, -1, &c);
1079*6a54128fSAndroid Build Coastguard Worker 		if (c >= 0)
1080*6a54128fSAndroid Build Coastguard Worker 			ctx->readahead_kb = c;
1081*6a54128fSAndroid Build Coastguard Worker 		if (ctx->readahead_kb != ~0ULL &&
1082*6a54128fSAndroid Build Coastguard Worker 		    ctx->readahead_kb > phys_mem_kb)
1083*6a54128fSAndroid Build Coastguard Worker 			ctx->readahead_kb = phys_mem_kb;
1084*6a54128fSAndroid Build Coastguard Worker 	}
1085*6a54128fSAndroid Build Coastguard Worker 
1086*6a54128fSAndroid Build Coastguard Worker 	/* Turn off discard in read-only mode */
1087*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_NO) &&
1088*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_DISCARD))
1089*6a54128fSAndroid Build Coastguard Worker 		ctx->options &= ~E2F_OPT_DISCARD;
1090*6a54128fSAndroid Build Coastguard Worker 
1091*6a54128fSAndroid Build Coastguard Worker 	if (flush) {
1092*6a54128fSAndroid Build Coastguard Worker 		fd = open(ctx->filesystem_name, O_RDONLY, 0);
1093*6a54128fSAndroid Build Coastguard Worker 		if (fd < 0) {
1094*6a54128fSAndroid Build Coastguard Worker 			com_err("open", errno,
1095*6a54128fSAndroid Build Coastguard Worker 				_("while opening %s for flushing"),
1096*6a54128fSAndroid Build Coastguard Worker 				ctx->filesystem_name);
1097*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
1098*6a54128fSAndroid Build Coastguard Worker 		}
1099*6a54128fSAndroid Build Coastguard Worker 		if ((retval = ext2fs_sync_device(fd, 1))) {
1100*6a54128fSAndroid Build Coastguard Worker 			com_err("ext2fs_sync_device", retval,
1101*6a54128fSAndroid Build Coastguard Worker 				_("while trying to flush %s"),
1102*6a54128fSAndroid Build Coastguard Worker 				ctx->filesystem_name);
1103*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
1104*6a54128fSAndroid Build Coastguard Worker 		}
1105*6a54128fSAndroid Build Coastguard Worker 		close(fd);
1106*6a54128fSAndroid Build Coastguard Worker 	}
1107*6a54128fSAndroid Build Coastguard Worker 	if (cflag && bad_blocks_file) {
1108*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "%s", _("The -c and the -l/-L options may not "
1109*6a54128fSAndroid Build Coastguard Worker 					"be both used at the same time.\n"));
1110*6a54128fSAndroid Build Coastguard Worker 		exit(FSCK_USAGE);
1111*6a54128fSAndroid Build Coastguard Worker 	}
1112*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SIGNAL_H
1113*6a54128fSAndroid Build Coastguard Worker 	/*
1114*6a54128fSAndroid Build Coastguard Worker 	 * Set up signal action
1115*6a54128fSAndroid Build Coastguard Worker 	 */
1116*6a54128fSAndroid Build Coastguard Worker 	memset(&sa, 0, sizeof(struct sigaction));
1117*6a54128fSAndroid Build Coastguard Worker 	sa.sa_handler = signal_cancel;
1118*6a54128fSAndroid Build Coastguard Worker 	sigaction(SIGINT, &sa, 0);
1119*6a54128fSAndroid Build Coastguard Worker 	sigaction(SIGTERM, &sa, 0);
1120*6a54128fSAndroid Build Coastguard Worker #ifdef SA_RESTART
1121*6a54128fSAndroid Build Coastguard Worker 	sa.sa_flags = SA_RESTART;
1122*6a54128fSAndroid Build Coastguard Worker #endif
1123*6a54128fSAndroid Build Coastguard Worker 	sa.sa_handler = signal_progress_on;
1124*6a54128fSAndroid Build Coastguard Worker 	sigaction(SIGUSR1, &sa, 0);
1125*6a54128fSAndroid Build Coastguard Worker 	sa.sa_handler = signal_progress_off;
1126*6a54128fSAndroid Build Coastguard Worker 	sigaction(SIGUSR2, &sa, 0);
1127*6a54128fSAndroid Build Coastguard Worker #endif
1128*6a54128fSAndroid Build Coastguard Worker 
1129*6a54128fSAndroid Build Coastguard Worker 	/* Update our PATH to include /sbin if we need to run badblocks  */
1130*6a54128fSAndroid Build Coastguard Worker 	if (cflag) {
1131*6a54128fSAndroid Build Coastguard Worker 		char *oldpath = getenv("PATH");
1132*6a54128fSAndroid Build Coastguard Worker 		char *newpath;
1133*6a54128fSAndroid Build Coastguard Worker 		int len = sizeof(PATH_SET) + 1;
1134*6a54128fSAndroid Build Coastguard Worker 
1135*6a54128fSAndroid Build Coastguard Worker 		if (oldpath)
1136*6a54128fSAndroid Build Coastguard Worker 			len += strlen(oldpath);
1137*6a54128fSAndroid Build Coastguard Worker 
1138*6a54128fSAndroid Build Coastguard Worker 		newpath = malloc(len);
1139*6a54128fSAndroid Build Coastguard Worker 		if (!newpath)
1140*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, "Couldn't malloc() newpath");
1141*6a54128fSAndroid Build Coastguard Worker 		strcpy(newpath, PATH_SET);
1142*6a54128fSAndroid Build Coastguard Worker 
1143*6a54128fSAndroid Build Coastguard Worker 		if (oldpath) {
1144*6a54128fSAndroid Build Coastguard Worker 			strcat(newpath, ":");
1145*6a54128fSAndroid Build Coastguard Worker 			strcat(newpath, oldpath);
1146*6a54128fSAndroid Build Coastguard Worker 		}
1147*6a54128fSAndroid Build Coastguard Worker 		putenv(newpath);
1148*6a54128fSAndroid Build Coastguard Worker 	}
1149*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_JBD_DEBUG
1150*6a54128fSAndroid Build Coastguard Worker 	jbd_debug = getenv("E2FSCK_JBD_DEBUG");
1151*6a54128fSAndroid Build Coastguard Worker 	if (jbd_debug) {
1152*6a54128fSAndroid Build Coastguard Worker 		res = sscanf(jbd_debug, "%d", &journal_enable_debug);
1153*6a54128fSAndroid Build Coastguard Worker 		if (res != 1) {
1154*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr,
1155*6a54128fSAndroid Build Coastguard Worker 			        _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
1156*6a54128fSAndroid Build Coastguard Worker 			        jbd_debug);
1157*6a54128fSAndroid Build Coastguard Worker 			exit (1);
1158*6a54128fSAndroid Build Coastguard Worker 		}
1159*6a54128fSAndroid Build Coastguard Worker 	}
1160*6a54128fSAndroid Build Coastguard Worker #endif
1161*6a54128fSAndroid Build Coastguard Worker 	return 0;
1162*6a54128fSAndroid Build Coastguard Worker 
1163*6a54128fSAndroid Build Coastguard Worker sscanf_err:
1164*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
1165*6a54128fSAndroid Build Coastguard Worker 	        c, optarg);
1166*6a54128fSAndroid Build Coastguard Worker 	exit (1);
1167*6a54128fSAndroid Build Coastguard Worker }
1168*6a54128fSAndroid Build Coastguard Worker 
try_open_fs(e2fsck_t ctx,int flags,io_manager io_ptr,ext2_filsys * ret_fs)1169*6a54128fSAndroid Build Coastguard Worker static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr,
1170*6a54128fSAndroid Build Coastguard Worker 			     ext2_filsys *ret_fs)
1171*6a54128fSAndroid Build Coastguard Worker {
1172*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
1173*6a54128fSAndroid Build Coastguard Worker 
1174*6a54128fSAndroid Build Coastguard Worker 	*ret_fs = NULL;
1175*6a54128fSAndroid Build Coastguard Worker 
1176*6a54128fSAndroid Build Coastguard Worker 	if (ctx->superblock) {
1177*6a54128fSAndroid Build Coastguard Worker 		unsigned long blocksize = ctx->blocksize;
1178*6a54128fSAndroid Build Coastguard Worker 
1179*6a54128fSAndroid Build Coastguard Worker 		if (!blocksize) {
1180*6a54128fSAndroid Build Coastguard Worker 			for (blocksize = EXT2_MIN_BLOCK_SIZE;
1181*6a54128fSAndroid Build Coastguard Worker 			     blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
1182*6a54128fSAndroid Build Coastguard Worker 
1183*6a54128fSAndroid Build Coastguard Worker 				retval = ext2fs_open2(ctx->filesystem_name,
1184*6a54128fSAndroid Build Coastguard Worker 						      ctx->io_options, flags,
1185*6a54128fSAndroid Build Coastguard Worker 						      ctx->superblock, blocksize,
1186*6a54128fSAndroid Build Coastguard Worker 						      unix_io_manager, ret_fs);
1187*6a54128fSAndroid Build Coastguard Worker 				if (*ret_fs) {
1188*6a54128fSAndroid Build Coastguard Worker 					ext2fs_free(*ret_fs);
1189*6a54128fSAndroid Build Coastguard Worker 					*ret_fs = NULL;
1190*6a54128fSAndroid Build Coastguard Worker 				}
1191*6a54128fSAndroid Build Coastguard Worker 				if (!retval)
1192*6a54128fSAndroid Build Coastguard Worker 					break;
1193*6a54128fSAndroid Build Coastguard Worker 			}
1194*6a54128fSAndroid Build Coastguard Worker 			if (retval)
1195*6a54128fSAndroid Build Coastguard Worker 				return retval;
1196*6a54128fSAndroid Build Coastguard Worker 		}
1197*6a54128fSAndroid Build Coastguard Worker 
1198*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1199*6a54128fSAndroid Build Coastguard Worker 				      flags, ctx->superblock, blocksize,
1200*6a54128fSAndroid Build Coastguard Worker 				      io_ptr, ret_fs);
1201*6a54128fSAndroid Build Coastguard Worker 	} else
1202*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1203*6a54128fSAndroid Build Coastguard Worker 				      flags, 0, 0, io_ptr, ret_fs);
1204*6a54128fSAndroid Build Coastguard Worker 
1205*6a54128fSAndroid Build Coastguard Worker 	if (retval == 0) {
1206*6a54128fSAndroid Build Coastguard Worker 		(*ret_fs)->priv_data = ctx;
1207*6a54128fSAndroid Build Coastguard Worker 		e2fsck_set_bitmap_type(*ret_fs, EXT2FS_BMAP64_RBTREE,
1208*6a54128fSAndroid Build Coastguard Worker 				       "default", NULL);
1209*6a54128fSAndroid Build Coastguard Worker 	}
1210*6a54128fSAndroid Build Coastguard Worker 	return retval;
1211*6a54128fSAndroid Build Coastguard Worker }
1212*6a54128fSAndroid Build Coastguard Worker 
1213*6a54128fSAndroid Build Coastguard Worker static const char *my_ver_string = E2FSPROGS_VERSION;
1214*6a54128fSAndroid Build Coastguard Worker static const char *my_ver_date = E2FSPROGS_DATE;
1215*6a54128fSAndroid Build Coastguard Worker 
e2fsck_check_mmp(ext2_filsys fs,e2fsck_t ctx)1216*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_check_mmp(ext2_filsys fs, e2fsck_t ctx)
1217*6a54128fSAndroid Build Coastguard Worker {
1218*6a54128fSAndroid Build Coastguard Worker 	struct mmp_struct *mmp_s;
1219*6a54128fSAndroid Build Coastguard Worker 	unsigned int mmp_check_interval;
1220*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
1221*6a54128fSAndroid Build Coastguard Worker 	struct problem_context pctx;
1222*6a54128fSAndroid Build Coastguard Worker 	unsigned int wait_time = 0;
1223*6a54128fSAndroid Build Coastguard Worker 
1224*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
1225*6a54128fSAndroid Build Coastguard Worker 	if (fs->mmp_buf == NULL) {
1226*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
1227*6a54128fSAndroid Build Coastguard Worker 		if (retval)
1228*6a54128fSAndroid Build Coastguard Worker 			goto check_error;
1229*6a54128fSAndroid Build Coastguard Worker 	}
1230*6a54128fSAndroid Build Coastguard Worker 
1231*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
1232*6a54128fSAndroid Build Coastguard Worker 	if (retval)
1233*6a54128fSAndroid Build Coastguard Worker 		goto check_error;
1234*6a54128fSAndroid Build Coastguard Worker 
1235*6a54128fSAndroid Build Coastguard Worker 	mmp_s = fs->mmp_buf;
1236*6a54128fSAndroid Build Coastguard Worker 
1237*6a54128fSAndroid Build Coastguard Worker 	mmp_check_interval = fs->super->s_mmp_update_interval;
1238*6a54128fSAndroid Build Coastguard Worker 	if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
1239*6a54128fSAndroid Build Coastguard Worker 		mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
1240*6a54128fSAndroid Build Coastguard Worker 
1241*6a54128fSAndroid Build Coastguard Worker 	/*
1242*6a54128fSAndroid Build Coastguard Worker 	 * If check_interval in MMP block is larger, use that instead of
1243*6a54128fSAndroid Build Coastguard Worker 	 * check_interval from the superblock.
1244*6a54128fSAndroid Build Coastguard Worker 	 */
1245*6a54128fSAndroid Build Coastguard Worker 	if (mmp_s->mmp_check_interval > mmp_check_interval)
1246*6a54128fSAndroid Build Coastguard Worker 		mmp_check_interval = mmp_s->mmp_check_interval;
1247*6a54128fSAndroid Build Coastguard Worker 
1248*6a54128fSAndroid Build Coastguard Worker 	wait_time = mmp_check_interval * 2 + 1;
1249*6a54128fSAndroid Build Coastguard Worker 
1250*6a54128fSAndroid Build Coastguard Worker 	if (mmp_s->mmp_seq == EXT4_MMP_SEQ_CLEAN)
1251*6a54128fSAndroid Build Coastguard Worker 		retval = 0;
1252*6a54128fSAndroid Build Coastguard Worker 	else if (mmp_s->mmp_seq == EXT4_MMP_SEQ_FSCK)
1253*6a54128fSAndroid Build Coastguard Worker 		retval = EXT2_ET_MMP_FSCK_ON;
1254*6a54128fSAndroid Build Coastguard Worker 	else if (mmp_s->mmp_seq > EXT4_MMP_SEQ_MAX)
1255*6a54128fSAndroid Build Coastguard Worker 		retval = EXT2_ET_MMP_UNKNOWN_SEQ;
1256*6a54128fSAndroid Build Coastguard Worker 
1257*6a54128fSAndroid Build Coastguard Worker 	if (retval)
1258*6a54128fSAndroid Build Coastguard Worker 		goto check_error;
1259*6a54128fSAndroid Build Coastguard Worker 
1260*6a54128fSAndroid Build Coastguard Worker 	/* Print warning if e2fsck will wait for more than 20 secs. */
1261*6a54128fSAndroid Build Coastguard Worker 	if (verbose || wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4) {
1262*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("MMP interval is %u seconds and total wait "
1263*6a54128fSAndroid Build Coastguard Worker 			       "time is %u seconds. Please wait...\n"),
1264*6a54128fSAndroid Build Coastguard Worker 			mmp_check_interval, wait_time * 2);
1265*6a54128fSAndroid Build Coastguard Worker 	}
1266*6a54128fSAndroid Build Coastguard Worker 
1267*6a54128fSAndroid Build Coastguard Worker 	return 0;
1268*6a54128fSAndroid Build Coastguard Worker 
1269*6a54128fSAndroid Build Coastguard Worker check_error:
1270*6a54128fSAndroid Build Coastguard Worker 
1271*6a54128fSAndroid Build Coastguard Worker 	if (retval == EXT2_ET_MMP_BAD_BLOCK) {
1272*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_MMP_INVALID_BLK, &pctx)) {
1273*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_mmp_block = 0;
1274*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
1275*6a54128fSAndroid Build Coastguard Worker 			retval = 0;
1276*6a54128fSAndroid Build Coastguard Worker 		}
1277*6a54128fSAndroid Build Coastguard Worker 	} else if (retval == EXT2_ET_MMP_FAILED) {
1278*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval, "%s",
1279*6a54128fSAndroid Build Coastguard Worker 			_("while checking MMP block"));
1280*6a54128fSAndroid Build Coastguard Worker 		dump_mmp_msg(fs->mmp_buf, NULL);
1281*6a54128fSAndroid Build Coastguard Worker 	} else if (retval == EXT2_ET_MMP_FSCK_ON ||
1282*6a54128fSAndroid Build Coastguard Worker 		   retval == EXT2_ET_MMP_UNKNOWN_SEQ) {
1283*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval, "%s",
1284*6a54128fSAndroid Build Coastguard Worker 			_("while checking MMP block"));
1285*6a54128fSAndroid Build Coastguard Worker 		dump_mmp_msg(fs->mmp_buf,
1286*6a54128fSAndroid Build Coastguard Worker 			     _("If you are sure the filesystem is not "
1287*6a54128fSAndroid Build Coastguard Worker 			       "in use on any node, run:\n"
1288*6a54128fSAndroid Build Coastguard Worker 			       "'tune2fs -f -E clear_mmp %s'\n"),
1289*6a54128fSAndroid Build Coastguard Worker 			     ctx->device_name);
1290*6a54128fSAndroid Build Coastguard Worker 	} else if (retval == EXT2_ET_MMP_MAGIC_INVALID) {
1291*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_MMP_INVALID_MAGIC, &pctx)) {
1292*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mmp_clear(fs);
1293*6a54128fSAndroid Build Coastguard Worker 			retval = 0;
1294*6a54128fSAndroid Build Coastguard Worker 		}
1295*6a54128fSAndroid Build Coastguard Worker 	} else if (retval == EXT2_ET_MMP_CSUM_INVALID) {
1296*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_MMP_CSUM_INVALID, &pctx)) {
1297*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mmp_clear(fs);
1298*6a54128fSAndroid Build Coastguard Worker 			retval = 0;
1299*6a54128fSAndroid Build Coastguard Worker 		}
1300*6a54128fSAndroid Build Coastguard Worker 	} else
1301*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval, "%s",
1302*6a54128fSAndroid Build Coastguard Worker 			_("while reading MMP block"));
1303*6a54128fSAndroid Build Coastguard Worker 	return retval;
1304*6a54128fSAndroid Build Coastguard Worker }
1305*6a54128fSAndroid Build Coastguard Worker 
e2fsck_setup_tdb(e2fsck_t ctx,io_manager * io_ptr)1306*6a54128fSAndroid Build Coastguard Worker static int e2fsck_setup_tdb(e2fsck_t ctx, io_manager *io_ptr)
1307*6a54128fSAndroid Build Coastguard Worker {
1308*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = ENOMEM;
1309*6a54128fSAndroid Build Coastguard Worker 	char *tdb_dir = NULL, *tdb_file = NULL;
1310*6a54128fSAndroid Build Coastguard Worker 	char *dev_name, *tmp_name;
1311*6a54128fSAndroid Build Coastguard Worker 	int free_tdb_dir = 0;
1312*6a54128fSAndroid Build Coastguard Worker 
1313*6a54128fSAndroid Build Coastguard Worker 	/* (re)open a specific undo file */
1314*6a54128fSAndroid Build Coastguard Worker 	if (ctx->undo_file && ctx->undo_file[0] != 0) {
1315*6a54128fSAndroid Build Coastguard Worker 		retval = set_undo_io_backing_manager(*io_ptr);
1316*6a54128fSAndroid Build Coastguard Worker 		if (retval)
1317*6a54128fSAndroid Build Coastguard Worker 			goto err;
1318*6a54128fSAndroid Build Coastguard Worker 		*io_ptr = undo_io_manager;
1319*6a54128fSAndroid Build Coastguard Worker 		retval = set_undo_io_backup_file(ctx->undo_file);
1320*6a54128fSAndroid Build Coastguard Worker 		if (retval)
1321*6a54128fSAndroid Build Coastguard Worker 			goto err;
1322*6a54128fSAndroid Build Coastguard Worker 		printf(_("Overwriting existing filesystem; this can be undone "
1323*6a54128fSAndroid Build Coastguard Worker 			 "using the command:\n"
1324*6a54128fSAndroid Build Coastguard Worker 			 "    e2undo %s %s\n\n"),
1325*6a54128fSAndroid Build Coastguard Worker 			ctx->undo_file, ctx->filesystem_name);
1326*6a54128fSAndroid Build Coastguard Worker 		return retval;
1327*6a54128fSAndroid Build Coastguard Worker 	}
1328*6a54128fSAndroid Build Coastguard Worker 
1329*6a54128fSAndroid Build Coastguard Worker 	/*
1330*6a54128fSAndroid Build Coastguard Worker 	 * Configuration via a conf file would be
1331*6a54128fSAndroid Build Coastguard Worker 	 * nice
1332*6a54128fSAndroid Build Coastguard Worker 	 */
1333*6a54128fSAndroid Build Coastguard Worker 	tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1334*6a54128fSAndroid Build Coastguard Worker 	if (!tdb_dir) {
1335*6a54128fSAndroid Build Coastguard Worker 		profile_get_string(ctx->profile, "defaults",
1336*6a54128fSAndroid Build Coastguard Worker 				   "undo_dir", 0, "/var/lib/e2fsprogs",
1337*6a54128fSAndroid Build Coastguard Worker 				   &tdb_dir);
1338*6a54128fSAndroid Build Coastguard Worker 		free_tdb_dir = 1;
1339*6a54128fSAndroid Build Coastguard Worker 	}
1340*6a54128fSAndroid Build Coastguard Worker 
1341*6a54128fSAndroid Build Coastguard Worker 	if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1342*6a54128fSAndroid Build Coastguard Worker 	    access(tdb_dir, W_OK)) {
1343*6a54128fSAndroid Build Coastguard Worker 		if (free_tdb_dir)
1344*6a54128fSAndroid Build Coastguard Worker 			free(tdb_dir);
1345*6a54128fSAndroid Build Coastguard Worker 		return 0;
1346*6a54128fSAndroid Build Coastguard Worker 	}
1347*6a54128fSAndroid Build Coastguard Worker 
1348*6a54128fSAndroid Build Coastguard Worker 	tmp_name = strdup(ctx->filesystem_name);
1349*6a54128fSAndroid Build Coastguard Worker 	if (!tmp_name)
1350*6a54128fSAndroid Build Coastguard Worker 		goto errout;
1351*6a54128fSAndroid Build Coastguard Worker 	dev_name = basename(tmp_name);
1352*6a54128fSAndroid Build Coastguard Worker 	tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
1353*6a54128fSAndroid Build Coastguard Worker 	if (!tdb_file) {
1354*6a54128fSAndroid Build Coastguard Worker 		free(tmp_name);
1355*6a54128fSAndroid Build Coastguard Worker 		goto errout;
1356*6a54128fSAndroid Build Coastguard Worker 	}
1357*6a54128fSAndroid Build Coastguard Worker 	sprintf(tdb_file, "%s/e2fsck-%s.e2undo", tdb_dir, dev_name);
1358*6a54128fSAndroid Build Coastguard Worker 	free(tmp_name);
1359*6a54128fSAndroid Build Coastguard Worker 
1360*6a54128fSAndroid Build Coastguard Worker 	if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
1361*6a54128fSAndroid Build Coastguard Worker 		retval = errno;
1362*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval,
1363*6a54128fSAndroid Build Coastguard Worker 			_("while trying to delete %s"), tdb_file);
1364*6a54128fSAndroid Build Coastguard Worker 		goto errout;
1365*6a54128fSAndroid Build Coastguard Worker 	}
1366*6a54128fSAndroid Build Coastguard Worker 
1367*6a54128fSAndroid Build Coastguard Worker 	retval = set_undo_io_backing_manager(*io_ptr);
1368*6a54128fSAndroid Build Coastguard Worker 	if (retval)
1369*6a54128fSAndroid Build Coastguard Worker 		goto errout;
1370*6a54128fSAndroid Build Coastguard Worker 	*io_ptr = undo_io_manager;
1371*6a54128fSAndroid Build Coastguard Worker 	retval = set_undo_io_backup_file(tdb_file);
1372*6a54128fSAndroid Build Coastguard Worker 	if (retval)
1373*6a54128fSAndroid Build Coastguard Worker 		goto errout;
1374*6a54128fSAndroid Build Coastguard Worker 	printf(_("Overwriting existing filesystem; this can be undone "
1375*6a54128fSAndroid Build Coastguard Worker 		 "using the command:\n"
1376*6a54128fSAndroid Build Coastguard Worker 		 "    e2undo %s %s\n\n"), tdb_file, ctx->filesystem_name);
1377*6a54128fSAndroid Build Coastguard Worker 
1378*6a54128fSAndroid Build Coastguard Worker 	if (free_tdb_dir)
1379*6a54128fSAndroid Build Coastguard Worker 		free(tdb_dir);
1380*6a54128fSAndroid Build Coastguard Worker 	free(tdb_file);
1381*6a54128fSAndroid Build Coastguard Worker 	return 0;
1382*6a54128fSAndroid Build Coastguard Worker 
1383*6a54128fSAndroid Build Coastguard Worker errout:
1384*6a54128fSAndroid Build Coastguard Worker 	if (free_tdb_dir)
1385*6a54128fSAndroid Build Coastguard Worker 		free(tdb_dir);
1386*6a54128fSAndroid Build Coastguard Worker 	free(tdb_file);
1387*6a54128fSAndroid Build Coastguard Worker err:
1388*6a54128fSAndroid Build Coastguard Worker 	com_err(ctx->program_name, retval, "%s",
1389*6a54128fSAndroid Build Coastguard Worker 		_("while trying to setup undo file\n"));
1390*6a54128fSAndroid Build Coastguard Worker 	return retval;
1391*6a54128fSAndroid Build Coastguard Worker }
1392*6a54128fSAndroid Build Coastguard Worker 
main(int argc,char * argv[])1393*6a54128fSAndroid Build Coastguard Worker int main (int argc, char *argv[])
1394*6a54128fSAndroid Build Coastguard Worker {
1395*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0, retval2 = 0, orig_retval = 0;
1396*6a54128fSAndroid Build Coastguard Worker 	int		exit_value = FSCK_OK;
1397*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys	fs = 0;
1398*6a54128fSAndroid Build Coastguard Worker 	io_manager	io_ptr;
1399*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb;
1400*6a54128fSAndroid Build Coastguard Worker 	const char	*lib_ver_date;
1401*6a54128fSAndroid Build Coastguard Worker 	int		my_ver, lib_ver;
1402*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t	ctx;
1403*6a54128fSAndroid Build Coastguard Worker 	blk64_t		orig_superblock = ~(blk64_t)0;
1404*6a54128fSAndroid Build Coastguard Worker 	struct problem_context pctx;
1405*6a54128fSAndroid Build Coastguard Worker 	int flags, run_result, was_changed;
1406*6a54128fSAndroid Build Coastguard Worker 	int journal_size;
1407*6a54128fSAndroid Build Coastguard Worker 	int sysval, sys_page_size = 4096;
1408*6a54128fSAndroid Build Coastguard Worker 	int old_bitmaps;
1409*6a54128fSAndroid Build Coastguard Worker 	__u32 features[3];
1410*6a54128fSAndroid Build Coastguard Worker 	char *cp;
1411*6a54128fSAndroid Build Coastguard Worker 	enum quota_type qtype;
1412*6a54128fSAndroid Build Coastguard Worker 	struct ext2fs_journal_params jparams;
1413*6a54128fSAndroid Build Coastguard Worker 
1414*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
1415*6a54128fSAndroid Build Coastguard Worker 	sigcatcher_setup();
1416*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
1417*6a54128fSAndroid Build Coastguard Worker 	mtrace();
1418*6a54128fSAndroid Build Coastguard Worker #endif
1419*6a54128fSAndroid Build Coastguard Worker #ifdef MCHECK
1420*6a54128fSAndroid Build Coastguard Worker 	mcheck(0);
1421*6a54128fSAndroid Build Coastguard Worker #endif
1422*6a54128fSAndroid Build Coastguard Worker #ifdef ENABLE_NLS
1423*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_MESSAGES, "");
1424*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_CTYPE, "");
1425*6a54128fSAndroid Build Coastguard Worker 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1426*6a54128fSAndroid Build Coastguard Worker 	textdomain(NLS_CAT_NAME);
1427*6a54128fSAndroid Build Coastguard Worker 	set_com_err_gettext(gettext);
1428*6a54128fSAndroid Build Coastguard Worker #endif
1429*6a54128fSAndroid Build Coastguard Worker 	my_ver = ext2fs_parse_version_string(my_ver_string);
1430*6a54128fSAndroid Build Coastguard Worker 	lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
1431*6a54128fSAndroid Build Coastguard Worker 	if (my_ver > lib_ver) {
1432*6a54128fSAndroid Build Coastguard Worker 		fprintf( stderr, "%s",
1433*6a54128fSAndroid Build Coastguard Worker 			 _("Error: ext2fs library version out of date!\n"));
1434*6a54128fSAndroid Build Coastguard Worker 		show_version_only++;
1435*6a54128fSAndroid Build Coastguard Worker 	}
1436*6a54128fSAndroid Build Coastguard Worker 
1437*6a54128fSAndroid Build Coastguard Worker 	retval = PRS(argc, argv, &ctx);
1438*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
1439*6a54128fSAndroid Build Coastguard Worker 		com_err("e2fsck", retval, "%s",
1440*6a54128fSAndroid Build Coastguard Worker 			_("while trying to initialize program"));
1441*6a54128fSAndroid Build Coastguard Worker 		exit(FSCK_ERROR);
1442*6a54128fSAndroid Build Coastguard Worker 	}
1443*6a54128fSAndroid Build Coastguard Worker 	reserve_stdio_fds();
1444*6a54128fSAndroid Build Coastguard Worker 
1445*6a54128fSAndroid Build Coastguard Worker 	set_up_logging(ctx);
1446*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf) {
1447*6a54128fSAndroid Build Coastguard Worker 		int i;
1448*6a54128fSAndroid Build Coastguard Worker 
1449*6a54128fSAndroid Build Coastguard Worker 		fputs("E2fsck run: ", ctx->logf);
1450*6a54128fSAndroid Build Coastguard Worker 		for (i = 0; i < argc; i++) {
1451*6a54128fSAndroid Build Coastguard Worker 			if (i)
1452*6a54128fSAndroid Build Coastguard Worker 				fputc(' ', ctx->logf);
1453*6a54128fSAndroid Build Coastguard Worker 			fputs(argv[i], ctx->logf);
1454*6a54128fSAndroid Build Coastguard Worker 		}
1455*6a54128fSAndroid Build Coastguard Worker 		fputc('\n', ctx->logf);
1456*6a54128fSAndroid Build Coastguard Worker 	}
1457*6a54128fSAndroid Build Coastguard Worker 	if (ctx->problem_logf) {
1458*6a54128fSAndroid Build Coastguard Worker 		int i;
1459*6a54128fSAndroid Build Coastguard Worker 
1460*6a54128fSAndroid Build Coastguard Worker 		fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
1461*6a54128fSAndroid Build Coastguard Worker 		      ctx->problem_logf);
1462*6a54128fSAndroid Build Coastguard Worker 		fprintf(ctx->problem_logf, "<problem_log time=\"%lu\">\n",
1463*6a54128fSAndroid Build Coastguard Worker 			(unsigned long) ctx->now);
1464*6a54128fSAndroid Build Coastguard Worker 		fprintf(ctx->problem_logf, "<invocation prog=\"%s\"",
1465*6a54128fSAndroid Build Coastguard Worker 			argv[0]);
1466*6a54128fSAndroid Build Coastguard Worker 		for (i = 1; i < argc; i++)
1467*6a54128fSAndroid Build Coastguard Worker 			fprintf(ctx->problem_logf, " arg%d=\"%s\"", i, argv[i]);
1468*6a54128fSAndroid Build Coastguard Worker 		fputs("/>\n", ctx->problem_logf);
1469*6a54128fSAndroid Build Coastguard Worker 	}
1470*6a54128fSAndroid Build Coastguard Worker 
1471*6a54128fSAndroid Build Coastguard Worker 	init_resource_track(&ctx->global_rtrack, NULL);
1472*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
1473*6a54128fSAndroid Build Coastguard Worker 		log_err(ctx, "e2fsck %s (%s)\n", my_ver_string,
1474*6a54128fSAndroid Build Coastguard Worker 			 my_ver_date);
1475*6a54128fSAndroid Build Coastguard Worker 
1476*6a54128fSAndroid Build Coastguard Worker 	if (show_version_only) {
1477*6a54128fSAndroid Build Coastguard Worker 		log_err(ctx, _("\tUsing %s, %s\n"),
1478*6a54128fSAndroid Build Coastguard Worker 			error_message(EXT2_ET_BASE), lib_ver_date);
1479*6a54128fSAndroid Build Coastguard Worker 		exit(FSCK_OK);
1480*6a54128fSAndroid Build Coastguard Worker 	}
1481*6a54128fSAndroid Build Coastguard Worker 
1482*6a54128fSAndroid Build Coastguard Worker 	check_mount(ctx);
1483*6a54128fSAndroid Build Coastguard Worker 
1484*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_PREEN) &&
1485*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_NO) &&
1486*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_YES)) {
1487*6a54128fSAndroid Build Coastguard Worker 		if (!ctx->interactive)
1488*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx,
1489*6a54128fSAndroid Build Coastguard Worker 				    _("need terminal for interactive repairs"));
1490*6a54128fSAndroid Build Coastguard Worker 	}
1491*6a54128fSAndroid Build Coastguard Worker 	ctx->superblock = ctx->use_superblock;
1492*6a54128fSAndroid Build Coastguard Worker 
1493*6a54128fSAndroid Build Coastguard Worker 	flags = EXT2_FLAG_SKIP_MMP | EXT2_FLAG_THREADS;
1494*6a54128fSAndroid Build Coastguard Worker restart:
1495*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TESTIO_DEBUG
1496*6a54128fSAndroid Build Coastguard Worker 	if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1497*6a54128fSAndroid Build Coastguard Worker 		io_ptr = test_io_manager;
1498*6a54128fSAndroid Build Coastguard Worker 		test_io_backing_manager = unix_io_manager;
1499*6a54128fSAndroid Build Coastguard Worker 	} else
1500*6a54128fSAndroid Build Coastguard Worker #endif
1501*6a54128fSAndroid Build Coastguard Worker 		io_ptr = unix_io_manager;
1502*6a54128fSAndroid Build Coastguard Worker 	flags |= EXT2_FLAG_NOFREE_ON_ERROR;
1503*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "old_bitmaps", 0, 0,
1504*6a54128fSAndroid Build Coastguard Worker 			    &old_bitmaps);
1505*6a54128fSAndroid Build Coastguard Worker 	if (!old_bitmaps)
1506*6a54128fSAndroid Build Coastguard Worker 		flags |= EXT2_FLAG_64BITS;
1507*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_READONLY) == 0) {
1508*6a54128fSAndroid Build Coastguard Worker 		flags |= EXT2_FLAG_RW;
1509*6a54128fSAndroid Build Coastguard Worker 		if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
1510*6a54128fSAndroid Build Coastguard Worker 		      ctx->mount_flags & EXT2_MF_READONLY))
1511*6a54128fSAndroid Build Coastguard Worker 			flags |= EXT2_FLAG_EXCLUSIVE;
1512*6a54128fSAndroid Build Coastguard Worker 		if ((ctx->mount_flags & EXT2_MF_READONLY) &&
1513*6a54128fSAndroid Build Coastguard Worker 		    (ctx->options & E2F_OPT_FORCE))
1514*6a54128fSAndroid Build Coastguard Worker 			flags &= ~EXT2_FLAG_EXCLUSIVE;
1515*6a54128fSAndroid Build Coastguard Worker 	}
1516*6a54128fSAndroid Build Coastguard Worker 
1517*6a54128fSAndroid Build Coastguard Worker 	if (ctx->undo_file) {
1518*6a54128fSAndroid Build Coastguard Worker 		retval = e2fsck_setup_tdb(ctx, &io_ptr);
1519*6a54128fSAndroid Build Coastguard Worker 		if (retval)
1520*6a54128fSAndroid Build Coastguard Worker 			exit(FSCK_ERROR);
1521*6a54128fSAndroid Build Coastguard Worker 	}
1522*6a54128fSAndroid Build Coastguard Worker 
1523*6a54128fSAndroid Build Coastguard Worker 	ctx->openfs_flags = flags;
1524*6a54128fSAndroid Build Coastguard Worker 	retval = try_open_fs(ctx, flags, io_ptr, &fs);
1525*6a54128fSAndroid Build Coastguard Worker 
1526*6a54128fSAndroid Build Coastguard Worker 	if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
1527*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1528*6a54128fSAndroid Build Coastguard Worker 	    ((retval == EXT2_ET_BAD_MAGIC) ||
1529*6a54128fSAndroid Build Coastguard Worker 	     (retval == EXT2_ET_SB_CSUM_INVALID) ||
1530*6a54128fSAndroid Build Coastguard Worker 	     (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
1531*6a54128fSAndroid Build Coastguard Worker 	     ((retval == 0) && (retval2 = ext2fs_check_desc(fs))))) {
1532*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1533*6a54128fSAndroid Build Coastguard Worker 			pctx.errcode = retval;
1534*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_OPEN_FAILED, &pctx);
1535*6a54128fSAndroid Build Coastguard Worker 		}
1536*6a54128fSAndroid Build Coastguard Worker 		if (retval2) {
1537*6a54128fSAndroid Build Coastguard Worker 			pctx.errcode = retval2;
1538*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_CHECK_DESC_FAILED, &pctx);
1539*6a54128fSAndroid Build Coastguard Worker 		}
1540*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = 0;
1541*6a54128fSAndroid Build Coastguard Worker 		if (retval2 == ENOMEM || retval2 == EXT2_ET_NO_MEMORY) {
1542*6a54128fSAndroid Build Coastguard Worker 			retval = retval2;
1543*6a54128fSAndroid Build Coastguard Worker 			goto failure;
1544*6a54128fSAndroid Build Coastguard Worker 		}
1545*6a54128fSAndroid Build Coastguard Worker 		if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) {
1546*6a54128fSAndroid Build Coastguard Worker 			ext2fs_free(fs);
1547*6a54128fSAndroid Build Coastguard Worker 			fs = NULL;
1548*6a54128fSAndroid Build Coastguard Worker 		}
1549*6a54128fSAndroid Build Coastguard Worker 		if (!fs || (fs->group_desc_count > 1)) {
1550*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _("%s: %s trying backup blocks...\n"),
1551*6a54128fSAndroid Build Coastguard Worker 				ctx->program_name,
1552*6a54128fSAndroid Build Coastguard Worker 				retval ? _("Superblock invalid,") :
1553*6a54128fSAndroid Build Coastguard Worker 				_("Group descriptors look bad..."));
1554*6a54128fSAndroid Build Coastguard Worker 			orig_superblock = ctx->superblock;
1555*6a54128fSAndroid Build Coastguard Worker 			get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
1556*6a54128fSAndroid Build Coastguard Worker 			if (fs)
1557*6a54128fSAndroid Build Coastguard Worker 				ext2fs_close_free(&fs);
1558*6a54128fSAndroid Build Coastguard Worker 			orig_retval = retval;
1559*6a54128fSAndroid Build Coastguard Worker 			retval = try_open_fs(ctx, flags, io_ptr, &fs);
1560*6a54128fSAndroid Build Coastguard Worker 			if ((orig_retval == 0) && retval != 0) {
1561*6a54128fSAndroid Build Coastguard Worker 				if (fs)
1562*6a54128fSAndroid Build Coastguard Worker 					ext2fs_close_free(&fs);
1563*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, _("%s: %s while using the "
1564*6a54128fSAndroid Build Coastguard Worker 					       "backup blocks"),
1565*6a54128fSAndroid Build Coastguard Worker 					ctx->program_name,
1566*6a54128fSAndroid Build Coastguard Worker 					error_message(retval));
1567*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, _("%s: going back to original "
1568*6a54128fSAndroid Build Coastguard Worker 					       "superblock\n"),
1569*6a54128fSAndroid Build Coastguard Worker 					ctx->program_name);
1570*6a54128fSAndroid Build Coastguard Worker 				ctx->superblock = orig_superblock;
1571*6a54128fSAndroid Build Coastguard Worker 				retval = try_open_fs(ctx, flags, io_ptr, &fs);
1572*6a54128fSAndroid Build Coastguard Worker 			}
1573*6a54128fSAndroid Build Coastguard Worker 		}
1574*6a54128fSAndroid Build Coastguard Worker 	}
1575*6a54128fSAndroid Build Coastguard Worker 	if (((retval == EXT2_ET_UNSUPP_FEATURE) ||
1576*6a54128fSAndroid Build Coastguard Worker 	     (retval == EXT2_ET_RO_UNSUPP_FEATURE)) &&
1577*6a54128fSAndroid Build Coastguard Worker 	    fs && fs->super) {
1578*6a54128fSAndroid Build Coastguard Worker 		sb = fs->super;
1579*6a54128fSAndroid Build Coastguard Worker 		features[0] = (sb->s_feature_compat &
1580*6a54128fSAndroid Build Coastguard Worker 			       ~EXT2_LIB_FEATURE_COMPAT_SUPP);
1581*6a54128fSAndroid Build Coastguard Worker 		features[1] = (sb->s_feature_incompat &
1582*6a54128fSAndroid Build Coastguard Worker 			       ~EXT2_LIB_FEATURE_INCOMPAT_SUPP);
1583*6a54128fSAndroid Build Coastguard Worker 		features[2] = (sb->s_feature_ro_compat &
1584*6a54128fSAndroid Build Coastguard Worker 			       ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1585*6a54128fSAndroid Build Coastguard Worker 		if (features[0] || features[1] || features[2])
1586*6a54128fSAndroid Build Coastguard Worker 			goto print_unsupp_features;
1587*6a54128fSAndroid Build Coastguard Worker 	}
1588*6a54128fSAndroid Build Coastguard Worker failure:
1589*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
1590*6a54128fSAndroid Build Coastguard Worker 		if (orig_retval)
1591*6a54128fSAndroid Build Coastguard Worker 			retval = orig_retval;
1592*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval, _("while trying to open %s"),
1593*6a54128fSAndroid Build Coastguard Worker 			ctx->filesystem_name);
1594*6a54128fSAndroid Build Coastguard Worker 		if (retval == EXT2_ET_REV_TOO_HIGH) {
1595*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s",
1596*6a54128fSAndroid Build Coastguard Worker 				_("The filesystem revision is apparently "
1597*6a54128fSAndroid Build Coastguard Worker 				  "too high for this version of e2fsck.\n"
1598*6a54128fSAndroid Build Coastguard Worker 				  "(Or the filesystem superblock "
1599*6a54128fSAndroid Build Coastguard Worker 				  "is corrupt)\n\n"));
1600*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1601*6a54128fSAndroid Build Coastguard Worker 		} else if (retval == EXT2_ET_SHORT_READ)
1602*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s",
1603*6a54128fSAndroid Build Coastguard Worker 				_("Could this be a zero-length partition?\n"));
1604*6a54128fSAndroid Build Coastguard Worker 		else if ((retval == EPERM) || (retval == EACCES))
1605*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _("You must have %s access to the "
1606*6a54128fSAndroid Build Coastguard Worker 				       "filesystem or be root\n"),
1607*6a54128fSAndroid Build Coastguard Worker 			       (ctx->options & E2F_OPT_READONLY) ?
1608*6a54128fSAndroid Build Coastguard Worker 			       "r/o" : "r/w");
1609*6a54128fSAndroid Build Coastguard Worker 		else if (retval == ENXIO)
1610*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s",
1611*6a54128fSAndroid Build Coastguard Worker 				_("Possibly non-existent or swap device?\n"));
1612*6a54128fSAndroid Build Coastguard Worker 		else if (retval == EBUSY)
1613*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s", _("Filesystem mounted or opened "
1614*6a54128fSAndroid Build Coastguard Worker 					 "exclusively by another program?\n"));
1615*6a54128fSAndroid Build Coastguard Worker 		else if (retval == ENOENT)
1616*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s",
1617*6a54128fSAndroid Build Coastguard Worker 				_("Possibly non-existent device?\n"));
1618*6a54128fSAndroid Build Coastguard Worker #ifdef EROFS
1619*6a54128fSAndroid Build Coastguard Worker 		else if (retval == EROFS)
1620*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s", _("Disk write-protected; use the "
1621*6a54128fSAndroid Build Coastguard Worker 					     "-n option to do a read-only\n"
1622*6a54128fSAndroid Build Coastguard Worker 					     "check of the device.\n"));
1623*6a54128fSAndroid Build Coastguard Worker #endif
1624*6a54128fSAndroid Build Coastguard Worker 		else {
1625*6a54128fSAndroid Build Coastguard Worker 			/*
1626*6a54128fSAndroid Build Coastguard Worker 			 * Let's try once more will less consistency checking
1627*6a54128fSAndroid Build Coastguard Worker 			 * so that we are able to recover from more errors
1628*6a54128fSAndroid Build Coastguard Worker 			 * (e.g. some tool messing up some value in the sb).
1629*6a54128fSAndroid Build Coastguard Worker 			 */
1630*6a54128fSAndroid Build Coastguard Worker 			if (((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
1631*6a54128fSAndroid Build Coastguard Worker 			     (retval == EXT2_ET_BAD_DESC_SIZE)) &&
1632*6a54128fSAndroid Build Coastguard Worker 			    !(flags & EXT2_FLAG_IGNORE_SB_ERRORS)) {
1633*6a54128fSAndroid Build Coastguard Worker 				if (fs)
1634*6a54128fSAndroid Build Coastguard Worker 					ext2fs_close_free(&fs);
1635*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, _("%s: Trying to load superblock "
1636*6a54128fSAndroid Build Coastguard Worker 					"despite errors...\n"),
1637*6a54128fSAndroid Build Coastguard Worker 					ctx->program_name);
1638*6a54128fSAndroid Build Coastguard Worker 				flags |= EXT2_FLAG_IGNORE_SB_ERRORS;
1639*6a54128fSAndroid Build Coastguard Worker 				/*
1640*6a54128fSAndroid Build Coastguard Worker 				 * If we tried backup sb, revert to the
1641*6a54128fSAndroid Build Coastguard Worker 				 * original one now.
1642*6a54128fSAndroid Build Coastguard Worker 				 */
1643*6a54128fSAndroid Build Coastguard Worker 				if (orig_superblock != ~(blk64_t)0)
1644*6a54128fSAndroid Build Coastguard Worker 					ctx->superblock = orig_superblock;
1645*6a54128fSAndroid Build Coastguard Worker 				goto restart;
1646*6a54128fSAndroid Build Coastguard Worker 			}
1647*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1648*6a54128fSAndroid Build Coastguard Worker 			if (retval == EXT2_ET_BAD_MAGIC)
1649*6a54128fSAndroid Build Coastguard Worker 				check_plausibility(ctx->filesystem_name,
1650*6a54128fSAndroid Build Coastguard Worker 						   CHECK_FS_EXIST, NULL);
1651*6a54128fSAndroid Build Coastguard Worker 		}
1652*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1653*6a54128fSAndroid Build Coastguard Worker 	}
1654*6a54128fSAndroid Build Coastguard Worker 	/*
1655*6a54128fSAndroid Build Coastguard Worker 	 * We only update the master superblock because (a) paranoia;
1656*6a54128fSAndroid Build Coastguard Worker 	 * we don't want to corrupt the backup superblocks, and (b) we
1657*6a54128fSAndroid Build Coastguard Worker 	 * don't need to update the mount count and last checked
1658*6a54128fSAndroid Build Coastguard Worker 	 * fields in the backup superblock (the kernel doesn't update
1659*6a54128fSAndroid Build Coastguard Worker 	 * the backup superblocks anyway).  With newer versions of the
1660*6a54128fSAndroid Build Coastguard Worker 	 * library this flag is set by ext2fs_open2(), but we set this
1661*6a54128fSAndroid Build Coastguard Worker 	 * here just to be sure.  (No, we don't support e2fsck running
1662*6a54128fSAndroid Build Coastguard Worker 	 * with some other libext2fs than the one that it was shipped
1663*6a54128fSAndroid Build Coastguard Worker 	 * with, but just in case....)
1664*6a54128fSAndroid Build Coastguard Worker 	 */
1665*6a54128fSAndroid Build Coastguard Worker 	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1666*6a54128fSAndroid Build Coastguard Worker 
1667*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1668*6a54128fSAndroid Build Coastguard Worker 		__u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1669*6a54128fSAndroid Build Coastguard Worker 		int need_restart = 0;
1670*6a54128fSAndroid Build Coastguard Worker 
1671*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = ext2fs_get_device_size2(ctx->filesystem_name,
1672*6a54128fSAndroid Build Coastguard Worker 						       blocksize,
1673*6a54128fSAndroid Build Coastguard Worker 						       &ctx->num_blocks);
1674*6a54128fSAndroid Build Coastguard Worker 		/*
1675*6a54128fSAndroid Build Coastguard Worker 		 * The floppy driver refuses to allow anyone else to
1676*6a54128fSAndroid Build Coastguard Worker 		 * open the device if has been opened with O_EXCL;
1677*6a54128fSAndroid Build Coastguard Worker 		 * this is unlike other block device drivers in Linux.
1678*6a54128fSAndroid Build Coastguard Worker 		 * To handle this, we close the filesystem and then
1679*6a54128fSAndroid Build Coastguard Worker 		 * reopen the filesystem after we get the device size.
1680*6a54128fSAndroid Build Coastguard Worker 		 */
1681*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode == EBUSY) {
1682*6a54128fSAndroid Build Coastguard Worker 			ext2fs_close_free(&fs);
1683*6a54128fSAndroid Build Coastguard Worker 			need_restart++;
1684*6a54128fSAndroid Build Coastguard Worker 			pctx.errcode =
1685*6a54128fSAndroid Build Coastguard Worker 				ext2fs_get_device_size2(ctx->filesystem_name,
1686*6a54128fSAndroid Build Coastguard Worker 							blocksize,
1687*6a54128fSAndroid Build Coastguard Worker 							&ctx->num_blocks);
1688*6a54128fSAndroid Build Coastguard Worker 		}
1689*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1690*6a54128fSAndroid Build Coastguard Worker 			ctx->num_blocks = 0;
1691*6a54128fSAndroid Build Coastguard Worker 		else if (pctx.errcode) {
1692*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1693*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_ABORT;
1694*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
1695*6a54128fSAndroid Build Coastguard Worker 		}
1696*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1697*6a54128fSAndroid Build Coastguard Worker 		if (need_restart)
1698*6a54128fSAndroid Build Coastguard Worker 			goto restart;
1699*6a54128fSAndroid Build Coastguard Worker 	}
1700*6a54128fSAndroid Build Coastguard Worker 
1701*6a54128fSAndroid Build Coastguard Worker 	ctx->fs = fs;
1702*6a54128fSAndroid Build Coastguard Worker 	fs->now = ctx->now;
1703*6a54128fSAndroid Build Coastguard Worker 	sb = fs->super;
1704*6a54128fSAndroid Build Coastguard Worker 
1705*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1706*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
1707*6a54128fSAndroid Build Coastguard Worker 			_("while trying to open %s"),
1708*6a54128fSAndroid Build Coastguard Worker 			ctx->filesystem_name);
1709*6a54128fSAndroid Build Coastguard Worker 	get_newer:
1710*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, _("Get a newer version of e2fsck!"));
1711*6a54128fSAndroid Build Coastguard Worker 	}
1712*6a54128fSAndroid Build Coastguard Worker 
1713*6a54128fSAndroid Build Coastguard Worker 	/*
1714*6a54128fSAndroid Build Coastguard Worker 	 * Set the device name, which is used whenever we print error
1715*6a54128fSAndroid Build Coastguard Worker 	 * or informational messages to the user.
1716*6a54128fSAndroid Build Coastguard Worker 	 */
1717*6a54128fSAndroid Build Coastguard Worker 	if (ctx->device_name == 0 && sb->s_volume_name[0])
1718*6a54128fSAndroid Build Coastguard Worker 		ctx->device_name = string_copy(ctx, (char *) sb->s_volume_name,
1719*6a54128fSAndroid Build Coastguard Worker 					       sizeof(sb->s_volume_name));
1720*6a54128fSAndroid Build Coastguard Worker 
1721*6a54128fSAndroid Build Coastguard Worker 	if (ctx->device_name == 0)
1722*6a54128fSAndroid Build Coastguard Worker 		ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
1723*6a54128fSAndroid Build Coastguard Worker 	for (cp = ctx->device_name; *cp; cp++)
1724*6a54128fSAndroid Build Coastguard Worker 		if (isspace(*cp) || *cp == ':')
1725*6a54128fSAndroid Build Coastguard Worker 			*cp = '_';
1726*6a54128fSAndroid Build Coastguard Worker 
1727*6a54128fSAndroid Build Coastguard Worker 	if (ctx->problem_logf) {
1728*6a54128fSAndroid Build Coastguard Worker 
1729*6a54128fSAndroid Build Coastguard Worker 		fprintf(ctx->problem_logf, "<filesystem dev=\"%s\"",
1730*6a54128fSAndroid Build Coastguard Worker 			ctx->filesystem_name);
1731*6a54128fSAndroid Build Coastguard Worker 		if (!uuid_is_null(sb->s_uuid)) {
1732*6a54128fSAndroid Build Coastguard Worker 			char buf[48];
1733*6a54128fSAndroid Build Coastguard Worker 
1734*6a54128fSAndroid Build Coastguard Worker 			uuid_unparse(sb->s_uuid, buf);
1735*6a54128fSAndroid Build Coastguard Worker 			fprintf(ctx->problem_logf, " uuid=\"%s\"", buf);
1736*6a54128fSAndroid Build Coastguard Worker 		}
1737*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_volume_name[0])
1738*6a54128fSAndroid Build Coastguard Worker 			fprintf(ctx->problem_logf, " label=\"%.*s\"",
1739*6a54128fSAndroid Build Coastguard Worker 				EXT2_LEN_STR(sb->s_volume_name));
1740*6a54128fSAndroid Build Coastguard Worker 
1741*6a54128fSAndroid Build Coastguard Worker 		fputs("/>\n", ctx->problem_logf);
1742*6a54128fSAndroid Build Coastguard Worker 	}
1743*6a54128fSAndroid Build Coastguard Worker 
1744*6a54128fSAndroid Build Coastguard Worker 	ehandler_init(fs->io);
1745*6a54128fSAndroid Build Coastguard Worker 
1746*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_mmp(fs->super) &&
1747*6a54128fSAndroid Build Coastguard Worker 	    (flags & EXT2_FLAG_SKIP_MMP)) {
1748*6a54128fSAndroid Build Coastguard Worker 		if (e2fsck_check_mmp(fs, ctx))
1749*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
1750*6a54128fSAndroid Build Coastguard Worker 
1751*6a54128fSAndroid Build Coastguard Worker 		/*
1752*6a54128fSAndroid Build Coastguard Worker 		 * Restart in order to reopen fs but this time start mmp.
1753*6a54128fSAndroid Build Coastguard Worker 		 */
1754*6a54128fSAndroid Build Coastguard Worker 		ext2fs_close_free(&ctx->fs);
1755*6a54128fSAndroid Build Coastguard Worker 		flags &= ~EXT2_FLAG_SKIP_MMP;
1756*6a54128fSAndroid Build Coastguard Worker 		goto restart;
1757*6a54128fSAndroid Build Coastguard Worker 	}
1758*6a54128fSAndroid Build Coastguard Worker 
1759*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf)
1760*6a54128fSAndroid Build Coastguard Worker 		fprintf(ctx->logf, "Filesystem UUID: %s\n",
1761*6a54128fSAndroid Build Coastguard Worker 			e2p_uuid2str(sb->s_uuid));
1762*6a54128fSAndroid Build Coastguard Worker 
1763*6a54128fSAndroid Build Coastguard Worker 	/*
1764*6a54128fSAndroid Build Coastguard Worker 	 * Make sure the ext3 superblock fields are consistent.
1765*6a54128fSAndroid Build Coastguard Worker 	 */
1766*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->mount_flags & (EXT2_MF_MOUNTED | EXT2_MF_BUSY)) == 0) {
1767*6a54128fSAndroid Build Coastguard Worker 		retval = e2fsck_check_ext3_journal(ctx);
1768*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1769*6a54128fSAndroid Build Coastguard Worker 			com_err(ctx->program_name, retval,
1770*6a54128fSAndroid Build Coastguard Worker 				_("while checking journal for %s"),
1771*6a54128fSAndroid Build Coastguard Worker 				ctx->device_name);
1772*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx,
1773*6a54128fSAndroid Build Coastguard Worker 				_("Cannot proceed with file system check"));
1774*6a54128fSAndroid Build Coastguard Worker 		}
1775*6a54128fSAndroid Build Coastguard Worker 	}
1776*6a54128fSAndroid Build Coastguard Worker 
1777*6a54128fSAndroid Build Coastguard Worker 	/*
1778*6a54128fSAndroid Build Coastguard Worker 	 * Check to see if we need to do ext3-style recovery.  If so,
1779*6a54128fSAndroid Build Coastguard Worker 	 * do it, and then restart the fsck.
1780*6a54128fSAndroid Build Coastguard Worker 	 */
1781*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_journal_needs_recovery(sb)) {
1782*6a54128fSAndroid Build Coastguard Worker 		if (ctx->options & E2F_OPT_READONLY) {
1783*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s",
1784*6a54128fSAndroid Build Coastguard Worker 				_("Warning: skipping journal recovery because "
1785*6a54128fSAndroid Build Coastguard Worker 				  "doing a read-only filesystem check.\n"));
1786*6a54128fSAndroid Build Coastguard Worker 			io_channel_flush(ctx->fs->io);
1787*6a54128fSAndroid Build Coastguard Worker 		} else {
1788*6a54128fSAndroid Build Coastguard Worker 			if (ctx->flags & E2F_FLAG_RESTARTED) {
1789*6a54128fSAndroid Build Coastguard Worker 				/*
1790*6a54128fSAndroid Build Coastguard Worker 				 * Whoops, we attempted to run the
1791*6a54128fSAndroid Build Coastguard Worker 				 * journal twice.  This should never
1792*6a54128fSAndroid Build Coastguard Worker 				 * happen, unless the hardware or
1793*6a54128fSAndroid Build Coastguard Worker 				 * device driver is being bogus.
1794*6a54128fSAndroid Build Coastguard Worker 				 */
1795*6a54128fSAndroid Build Coastguard Worker 				com_err(ctx->program_name, 0,
1796*6a54128fSAndroid Build Coastguard Worker 					_("unable to set superblock flags "
1797*6a54128fSAndroid Build Coastguard Worker 					  "on %s\n"), ctx->device_name);
1798*6a54128fSAndroid Build Coastguard Worker 				fatal_error(ctx, 0);
1799*6a54128fSAndroid Build Coastguard Worker 			}
1800*6a54128fSAndroid Build Coastguard Worker 			retval = e2fsck_run_ext3_journal(ctx);
1801*6a54128fSAndroid Build Coastguard Worker 			if (retval == EFSBADCRC) {
1802*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, _("Journal checksum error "
1803*6a54128fSAndroid Build Coastguard Worker 					       "found in %s\n"),
1804*6a54128fSAndroid Build Coastguard Worker 					ctx->device_name);
1805*6a54128fSAndroid Build Coastguard Worker 			} else if (retval == EFSCORRUPTED) {
1806*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, _("Journal corrupted in %s\n"),
1807*6a54128fSAndroid Build Coastguard Worker 					ctx->device_name);
1808*6a54128fSAndroid Build Coastguard Worker 			} else if (retval) {
1809*6a54128fSAndroid Build Coastguard Worker 				com_err(ctx->program_name, retval,
1810*6a54128fSAndroid Build Coastguard Worker 				_("while recovering journal of %s"),
1811*6a54128fSAndroid Build Coastguard Worker 					ctx->device_name);
1812*6a54128fSAndroid Build Coastguard Worker 			}
1813*6a54128fSAndroid Build Coastguard Worker 			ext2fs_close_free(&ctx->fs);
1814*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_RESTARTED;
1815*6a54128fSAndroid Build Coastguard Worker 			goto restart;
1816*6a54128fSAndroid Build Coastguard Worker 		}
1817*6a54128fSAndroid Build Coastguard Worker 	}
1818*6a54128fSAndroid Build Coastguard Worker 
1819*6a54128fSAndroid Build Coastguard Worker 	/*
1820*6a54128fSAndroid Build Coastguard Worker 	 * Check for compatibility with the feature sets.  We need to
1821*6a54128fSAndroid Build Coastguard Worker 	 * be more stringent than ext2fs_open().
1822*6a54128fSAndroid Build Coastguard Worker 	 */
1823*6a54128fSAndroid Build Coastguard Worker 	features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP;
1824*6a54128fSAndroid Build Coastguard Worker 	features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP;
1825*6a54128fSAndroid Build Coastguard Worker 	features[2] = (sb->s_feature_ro_compat &
1826*6a54128fSAndroid Build Coastguard Worker 		       ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1827*6a54128fSAndroid Build Coastguard Worker print_unsupp_features:
1828*6a54128fSAndroid Build Coastguard Worker 	if (features[0] || features[1] || features[2]) {
1829*6a54128fSAndroid Build Coastguard Worker 		int	i, j;
1830*6a54128fSAndroid Build Coastguard Worker 		__u32	*mask = features, m;
1831*6a54128fSAndroid Build Coastguard Worker 
1832*6a54128fSAndroid Build Coastguard Worker 		log_err(ctx, _("%s has unsupported feature(s):"),
1833*6a54128fSAndroid Build Coastguard Worker 			ctx->filesystem_name);
1834*6a54128fSAndroid Build Coastguard Worker 
1835*6a54128fSAndroid Build Coastguard Worker 		for (i=0; i <3; i++,mask++) {
1836*6a54128fSAndroid Build Coastguard Worker 			for (j=0,m=1; j < 32; j++, m<<=1) {
1837*6a54128fSAndroid Build Coastguard Worker 				if (*mask & m)
1838*6a54128fSAndroid Build Coastguard Worker 					log_err(ctx, " %s",
1839*6a54128fSAndroid Build Coastguard Worker 						e2p_feature2string(i, m));
1840*6a54128fSAndroid Build Coastguard Worker 			}
1841*6a54128fSAndroid Build Coastguard Worker 		}
1842*6a54128fSAndroid Build Coastguard Worker 		log_err(ctx, "\n");
1843*6a54128fSAndroid Build Coastguard Worker 		goto get_newer;
1844*6a54128fSAndroid Build Coastguard Worker 	}
1845*6a54128fSAndroid Build Coastguard Worker 
1846*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_casefold(sb) && !fs->encoding) {
1847*6a54128fSAndroid Build Coastguard Worker 		log_err(ctx, _("%s has unsupported encoding: %0x\n"),
1848*6a54128fSAndroid Build Coastguard Worker 			ctx->filesystem_name, sb->s_encoding);
1849*6a54128fSAndroid Build Coastguard Worker 		goto get_newer;
1850*6a54128fSAndroid Build Coastguard Worker 	}
1851*6a54128fSAndroid Build Coastguard Worker 
1852*6a54128fSAndroid Build Coastguard Worker 	/*
1853*6a54128fSAndroid Build Coastguard Worker 	 * If the user specified a specific superblock, presumably the
1854*6a54128fSAndroid Build Coastguard Worker 	 * master superblock has been trashed.  So we mark the
1855*6a54128fSAndroid Build Coastguard Worker 	 * superblock as dirty, so it can be written out.
1856*6a54128fSAndroid Build Coastguard Worker 	 */
1857*6a54128fSAndroid Build Coastguard Worker 	if (ctx->superblock &&
1858*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_READONLY))
1859*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
1860*6a54128fSAndroid Build Coastguard Worker 
1861*6a54128fSAndroid Build Coastguard Worker 	/*
1862*6a54128fSAndroid Build Coastguard Worker 	 * Calculate the number of filesystem blocks per pagesize.  If
1863*6a54128fSAndroid Build Coastguard Worker 	 * fs->blocksize > page_size, set the number of blocks per
1864*6a54128fSAndroid Build Coastguard Worker 	 * pagesize to 1 to avoid division by zero errors.
1865*6a54128fSAndroid Build Coastguard Worker 	 */
1866*6a54128fSAndroid Build Coastguard Worker #ifdef _SC_PAGESIZE
1867*6a54128fSAndroid Build Coastguard Worker 	sysval = sysconf(_SC_PAGESIZE);
1868*6a54128fSAndroid Build Coastguard Worker 	if (sysval > 0)
1869*6a54128fSAndroid Build Coastguard Worker 		sys_page_size = sysval;
1870*6a54128fSAndroid Build Coastguard Worker #endif /* _SC_PAGESIZE */
1871*6a54128fSAndroid Build Coastguard Worker 	ctx->blocks_per_page = sys_page_size / fs->blocksize;
1872*6a54128fSAndroid Build Coastguard Worker 	if (ctx->blocks_per_page == 0)
1873*6a54128fSAndroid Build Coastguard Worker 		ctx->blocks_per_page = 1;
1874*6a54128fSAndroid Build Coastguard Worker 
1875*6a54128fSAndroid Build Coastguard Worker 	if (ctx->superblock)
1876*6a54128fSAndroid Build Coastguard Worker 		set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1877*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_valid(fs);
1878*6a54128fSAndroid Build Coastguard Worker 	check_super_block(ctx);
1879*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1880*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1881*6a54128fSAndroid Build Coastguard Worker 	check_if_skip(ctx);
1882*6a54128fSAndroid Build Coastguard Worker 	check_resize_inode(ctx);
1883*6a54128fSAndroid Build Coastguard Worker 	if (bad_blocks_file)
1884*6a54128fSAndroid Build Coastguard Worker 		read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1885*6a54128fSAndroid Build Coastguard Worker 	else if (cflag)
1886*6a54128fSAndroid Build Coastguard Worker 		read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1887*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1888*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
1889*6a54128fSAndroid Build Coastguard Worker 
1890*6a54128fSAndroid Build Coastguard Worker 	/*
1891*6a54128fSAndroid Build Coastguard Worker 	 * Mark the system as valid, 'til proven otherwise
1892*6a54128fSAndroid Build Coastguard Worker 	 */
1893*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_valid(fs);
1894*6a54128fSAndroid Build Coastguard Worker 
1895*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1896*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
1897*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s: %s while reading bad blocks inode\n"),
1898*6a54128fSAndroid Build Coastguard Worker 			ctx->program_name, error_message(retval));
1899*6a54128fSAndroid Build Coastguard Worker 		preenhalt(ctx);
1900*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", _("This doesn't bode well, "
1901*6a54128fSAndroid Build Coastguard Worker 				     "but we'll try to go on...\n"));
1902*6a54128fSAndroid Build Coastguard Worker 	}
1903*6a54128fSAndroid Build Coastguard Worker 
1904*6a54128fSAndroid Build Coastguard Worker 	/*
1905*6a54128fSAndroid Build Coastguard Worker 	 * Save the journal size in megabytes.
1906*6a54128fSAndroid Build Coastguard Worker 	 * Try and use the journal size from the backup else let e2fsck
1907*6a54128fSAndroid Build Coastguard Worker 	 * find the default journal size. If fast commit feature is enabled,
1908*6a54128fSAndroid Build Coastguard Worker 	 * it is not clear how many of the journal blocks were fast commit
1909*6a54128fSAndroid Build Coastguard Worker 	 * blocks. So, ignore the size of journal found in backup.
1910*6a54128fSAndroid Build Coastguard Worker 	 *
1911*6a54128fSAndroid Build Coastguard Worker 	 * TODO: Add a new backup type that captures fast commit info as
1912*6a54128fSAndroid Build Coastguard Worker 	 * well.
1913*6a54128fSAndroid Build Coastguard Worker 	 */
1914*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS &&
1915*6a54128fSAndroid Build Coastguard Worker 		!ext2fs_has_feature_fast_commit(sb))
1916*6a54128fSAndroid Build Coastguard Worker 		journal_size = (sb->s_jnl_blocks[15] << (32 - 20)) |
1917*6a54128fSAndroid Build Coastguard Worker 			       (sb->s_jnl_blocks[16] >> 20);
1918*6a54128fSAndroid Build Coastguard Worker 	else
1919*6a54128fSAndroid Build Coastguard Worker 		journal_size = -1;
1920*6a54128fSAndroid Build Coastguard Worker 
1921*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_quota(sb)) {
1922*6a54128fSAndroid Build Coastguard Worker 		/* Quotas were enabled. Do quota accounting during fsck. */
1923*6a54128fSAndroid Build Coastguard Worker 		clear_problem_context(&pctx);
1924*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = quota_init_context(&ctx->qctx, ctx->fs, 0);
1925*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode) {
1926*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_QUOTA_INIT_CTX, &pctx);
1927*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
1928*6a54128fSAndroid Build Coastguard Worker 		}
1929*6a54128fSAndroid Build Coastguard Worker 	}
1930*6a54128fSAndroid Build Coastguard Worker 
1931*6a54128fSAndroid Build Coastguard Worker 	run_result = e2fsck_run(ctx);
1932*6a54128fSAndroid Build Coastguard Worker 	e2fsck_clear_progbar(ctx);
1933*6a54128fSAndroid Build Coastguard Worker 
1934*6a54128fSAndroid Build Coastguard Worker 	if (!ctx->invalid_bitmaps &&
1935*6a54128fSAndroid Build Coastguard Worker 	    (ctx->flags & E2F_FLAG_JOURNAL_INODE)) {
1936*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1937*6a54128fSAndroid Build Coastguard Worker 			if (journal_size < 1024) {
1938*6a54128fSAndroid Build Coastguard Worker 				ext2fs_get_journal_params(&jparams, fs);
1939*6a54128fSAndroid Build Coastguard Worker 			} else {
1940*6a54128fSAndroid Build Coastguard Worker 				jparams.num_journal_blocks = journal_size;
1941*6a54128fSAndroid Build Coastguard Worker 				jparams.num_fc_blocks = 0;
1942*6a54128fSAndroid Build Coastguard Worker 			}
1943*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _("Creating journal (%d blocks): "),
1944*6a54128fSAndroid Build Coastguard Worker 			       jparams.num_journal_blocks);
1945*6a54128fSAndroid Build Coastguard Worker 			fflush(stdout);
1946*6a54128fSAndroid Build Coastguard Worker 			retval = ext2fs_add_journal_inode3(fs, &jparams, ~0ULL, 0);
1947*6a54128fSAndroid Build Coastguard Worker 			if (retval) {
1948*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, "%s: while trying to create "
1949*6a54128fSAndroid Build Coastguard Worker 					"journal\n", error_message(retval));
1950*6a54128fSAndroid Build Coastguard Worker 				goto no_journal;
1951*6a54128fSAndroid Build Coastguard Worker 			}
1952*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s", _(" Done.\n"));
1953*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s",
1954*6a54128fSAndroid Build Coastguard Worker 				_("\n*** journal has been regenerated ***\n"));
1955*6a54128fSAndroid Build Coastguard Worker 		}
1956*6a54128fSAndroid Build Coastguard Worker 	}
1957*6a54128fSAndroid Build Coastguard Worker no_journal:
1958*6a54128fSAndroid Build Coastguard Worker 
1959*6a54128fSAndroid Build Coastguard Worker 	if (run_result & E2F_FLAG_ABORT) {
1960*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, _("aborted"));
1961*6a54128fSAndroid Build Coastguard Worker 	} else if (run_result & E2F_FLAG_CANCEL) {
1962*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s: e2fsck canceled.\n"), ctx->device_name ?
1963*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name : ctx->filesystem_name);
1964*6a54128fSAndroid Build Coastguard Worker 		exit_value |= FSCK_CANCELED;
1965*6a54128fSAndroid Build Coastguard Worker 	} else if (ctx->qctx && !ctx->invalid_bitmaps) {
1966*6a54128fSAndroid Build Coastguard Worker 		int needs_writeout;
1967*6a54128fSAndroid Build Coastguard Worker 
1968*6a54128fSAndroid Build Coastguard Worker 		for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
1969*6a54128fSAndroid Build Coastguard Worker 			if (*quota_sb_inump(sb, qtype) == 0)
1970*6a54128fSAndroid Build Coastguard Worker 				continue;
1971*6a54128fSAndroid Build Coastguard Worker 			needs_writeout = 0;
1972*6a54128fSAndroid Build Coastguard Worker 			pctx.num = qtype;
1973*6a54128fSAndroid Build Coastguard Worker 			retval = quota_compare_and_update(ctx->qctx, qtype,
1974*6a54128fSAndroid Build Coastguard Worker 							  &needs_writeout);
1975*6a54128fSAndroid Build Coastguard Worker 			if ((retval || needs_writeout) &&
1976*6a54128fSAndroid Build Coastguard Worker 			    fix_problem(ctx, PR_6_UPDATE_QUOTAS, &pctx)) {
1977*6a54128fSAndroid Build Coastguard Worker 				pctx.errcode = quota_write_inode(ctx->qctx,
1978*6a54128fSAndroid Build Coastguard Worker 								 1 << qtype);
1979*6a54128fSAndroid Build Coastguard Worker 				if (pctx.errcode)
1980*6a54128fSAndroid Build Coastguard Worker 					(void) fix_problem(ctx,
1981*6a54128fSAndroid Build Coastguard Worker 						PR_6_WRITE_QUOTAS, &pctx);
1982*6a54128fSAndroid Build Coastguard Worker 			}
1983*6a54128fSAndroid Build Coastguard Worker 		}
1984*6a54128fSAndroid Build Coastguard Worker 		quota_release_context(&ctx->qctx);
1985*6a54128fSAndroid Build Coastguard Worker 	}
1986*6a54128fSAndroid Build Coastguard Worker 
1987*6a54128fSAndroid Build Coastguard Worker 	if (run_result == E2F_FLAG_RESTART) {
1988*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s",
1989*6a54128fSAndroid Build Coastguard Worker 			_("Restarting e2fsck from the beginning...\n"));
1990*6a54128fSAndroid Build Coastguard Worker 		retval = e2fsck_reset_context(ctx);
1991*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1992*6a54128fSAndroid Build Coastguard Worker 			com_err(ctx->program_name, retval, "%s",
1993*6a54128fSAndroid Build Coastguard Worker 				_("while resetting context"));
1994*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
1995*6a54128fSAndroid Build Coastguard Worker 		}
1996*6a54128fSAndroid Build Coastguard Worker 		ext2fs_close_free(&ctx->fs);
1997*6a54128fSAndroid Build Coastguard Worker 		goto restart;
1998*6a54128fSAndroid Build Coastguard Worker 	}
1999*6a54128fSAndroid Build Coastguard Worker 
2000*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
2001*6a54128fSAndroid Build Coastguard Worker 	mtrace_print("Cleanup");
2002*6a54128fSAndroid Build Coastguard Worker #endif
2003*6a54128fSAndroid Build Coastguard Worker 	was_changed = ext2fs_test_changed(fs);
2004*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->flags & E2F_FLAG_RUN_RETURN) &&
2005*6a54128fSAndroid Build Coastguard Worker 	    !(ctx->options & E2F_OPT_READONLY)) {
2006*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_valid(fs)) {
2007*6a54128fSAndroid Build Coastguard Worker 			if (!(sb->s_state & EXT2_VALID_FS))
2008*6a54128fSAndroid Build Coastguard Worker 				exit_value |= FSCK_NONDESTRUCT;
2009*6a54128fSAndroid Build Coastguard Worker 			sb->s_state = EXT2_VALID_FS;
2010*6a54128fSAndroid Build Coastguard Worker 			if (check_backup_super_block(ctx))
2011*6a54128fSAndroid Build Coastguard Worker 				fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2012*6a54128fSAndroid Build Coastguard Worker 		} else
2013*6a54128fSAndroid Build Coastguard Worker 			sb->s_state &= ~EXT2_VALID_FS;
2014*6a54128fSAndroid Build Coastguard Worker 		if (!(ctx->flags & E2F_FLAG_TIME_INSANE))
2015*6a54128fSAndroid Build Coastguard Worker 			sb->s_lastcheck = ctx->now;
2016*6a54128fSAndroid Build Coastguard Worker 		sb->s_mnt_count = 0;
2017*6a54128fSAndroid Build Coastguard Worker 		memset(((char *) sb) + EXT4_S_ERR_START, 0, EXT4_S_ERR_LEN);
2018*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = ext2fs_set_gdt_csum(ctx->fs);
2019*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode)
2020*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_6_SET_BG_CHECKSUM, &pctx);
2021*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
2022*6a54128fSAndroid Build Coastguard Worker 	}
2023*6a54128fSAndroid Build Coastguard Worker 
2024*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
2025*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
2026*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_NO))
2027*6a54128fSAndroid Build Coastguard Worker 		/* Don't try to write or flush I/O, we just wanted to know whether or
2028*6a54128fSAndroid Build Coastguard Worker 		 * not there were enough free blocks to undo deduplication.
2029*6a54128fSAndroid Build Coastguard Worker 		 */
2030*6a54128fSAndroid Build Coastguard Worker 		goto skip_write;
2031*6a54128fSAndroid Build Coastguard Worker 
2032*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_READONLY)) {
2033*6a54128fSAndroid Build Coastguard Worker 		e2fsck_write_bitmaps(ctx);
2034*6a54128fSAndroid Build Coastguard Worker 		if (fs->flags & EXT2_FLAG_DIRTY) {
2035*6a54128fSAndroid Build Coastguard Worker 			pctx.errcode = ext2fs_flush(ctx->fs);
2036*6a54128fSAndroid Build Coastguard Worker 			if (pctx.errcode)
2037*6a54128fSAndroid Build Coastguard Worker 				fix_problem(ctx, PR_6_FLUSH_FILESYSTEM, &pctx);
2038*6a54128fSAndroid Build Coastguard Worker 		}
2039*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = io_channel_flush(ctx->fs->io);
2040*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode)
2041*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_6_IO_FLUSH, &pctx);
2042*6a54128fSAndroid Build Coastguard Worker 	}
2043*6a54128fSAndroid Build Coastguard Worker 
2044*6a54128fSAndroid Build Coastguard Worker 	if (was_changed) {
2045*6a54128fSAndroid Build Coastguard Worker 		int fs_fixed = (ctx->flags & E2F_FLAG_PROBLEMS_FIXED);
2046*6a54128fSAndroid Build Coastguard Worker 
2047*6a54128fSAndroid Build Coastguard Worker 		if (fs_fixed)
2048*6a54128fSAndroid Build Coastguard Worker 			exit_value |= FSCK_NONDESTRUCT;
2049*6a54128fSAndroid Build Coastguard Worker 		if (!(ctx->options & E2F_OPT_PREEN)) {
2050*6a54128fSAndroid Build Coastguard Worker #if 0	/* Do this later; it breaks too many tests' golden outputs */
2051*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, fs_fixed ?
2052*6a54128fSAndroid Build Coastguard Worker 				_("\n%s: ***** FILE SYSTEM ERRORS "
2053*6a54128fSAndroid Build Coastguard Worker 				  "CORRECTED *****\n") :
2054*6a54128fSAndroid Build Coastguard Worker 				_("%s: File system was modified.\n"),
2055*6a54128fSAndroid Build Coastguard Worker 				ctx->device_name);
2056*6a54128fSAndroid Build Coastguard Worker #else
2057*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx,
2058*6a54128fSAndroid Build Coastguard Worker 				_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
2059*6a54128fSAndroid Build Coastguard Worker 				ctx->device_name);
2060*6a54128fSAndroid Build Coastguard Worker #endif
2061*6a54128fSAndroid Build Coastguard Worker 		}
2062*6a54128fSAndroid Build Coastguard Worker 		if (ctx->mount_flags & EXT2_MF_ISROOT) {
2063*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, _("%s: ***** REBOOT SYSTEM *****\n"),
2064*6a54128fSAndroid Build Coastguard Worker 				ctx->device_name);
2065*6a54128fSAndroid Build Coastguard Worker 			exit_value |= FSCK_REBOOT;
2066*6a54128fSAndroid Build Coastguard Worker 		}
2067*6a54128fSAndroid Build Coastguard Worker 	}
2068*6a54128fSAndroid Build Coastguard Worker 
2069*6a54128fSAndroid Build Coastguard Worker skip_write:
2070*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_test_valid(fs) ||
2071*6a54128fSAndroid Build Coastguard Worker 	    ((exit_value & FSCK_CANCELED) &&
2072*6a54128fSAndroid Build Coastguard Worker 	     (sb->s_state & EXT2_ERROR_FS))) {
2073*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
2074*6a54128fSAndroid Build Coastguard Worker 			       "errors **********\n\n"), ctx->device_name);
2075*6a54128fSAndroid Build Coastguard Worker 		exit_value |= FSCK_UNCORRECTED;
2076*6a54128fSAndroid Build Coastguard Worker 		exit_value &= ~FSCK_NONDESTRUCT;
2077*6a54128fSAndroid Build Coastguard Worker 	}
2078*6a54128fSAndroid Build Coastguard Worker 	if (exit_value & FSCK_CANCELED) {
2079*6a54128fSAndroid Build Coastguard Worker 		int	allow_cancellation;
2080*6a54128fSAndroid Build Coastguard Worker 
2081*6a54128fSAndroid Build Coastguard Worker 		profile_get_boolean(ctx->profile, "options",
2082*6a54128fSAndroid Build Coastguard Worker 				    "allow_cancellation", 0, 0,
2083*6a54128fSAndroid Build Coastguard Worker 				    &allow_cancellation);
2084*6a54128fSAndroid Build Coastguard Worker 		exit_value &= ~FSCK_NONDESTRUCT;
2085*6a54128fSAndroid Build Coastguard Worker 		if (allow_cancellation && ext2fs_test_valid(fs) &&
2086*6a54128fSAndroid Build Coastguard Worker 		    (sb->s_state & EXT2_VALID_FS) &&
2087*6a54128fSAndroid Build Coastguard Worker 		    !(sb->s_state & EXT2_ERROR_FS))
2088*6a54128fSAndroid Build Coastguard Worker 			exit_value = 0;
2089*6a54128fSAndroid Build Coastguard Worker 	} else
2090*6a54128fSAndroid Build Coastguard Worker 		show_stats(ctx);
2091*6a54128fSAndroid Build Coastguard Worker 
2092*6a54128fSAndroid Build Coastguard Worker 	print_resource_track(ctx, NULL, &ctx->global_rtrack, ctx->fs->io);
2093*6a54128fSAndroid Build Coastguard Worker 
2094*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close_free(&ctx->fs);
2095*6a54128fSAndroid Build Coastguard Worker 	free(ctx->journal_name);
2096*6a54128fSAndroid Build Coastguard Worker 
2097*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf)
2098*6a54128fSAndroid Build Coastguard Worker 		fprintf(ctx->logf, "Exit status: %d\n", exit_value);
2099*6a54128fSAndroid Build Coastguard Worker 	e2fsck_free_context(ctx);
2100*6a54128fSAndroid Build Coastguard Worker 	remove_error_table(&et_ext2_error_table);
2101*6a54128fSAndroid Build Coastguard Worker 	remove_error_table(&et_prof_error_table);
2102*6a54128fSAndroid Build Coastguard Worker 	return exit_value;
2103*6a54128fSAndroid Build Coastguard Worker }
2104