xref: /aosp_15_r20/external/e2fsprogs/e2fsck/util.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * util.c --- miscellaneous utilities
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 #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
14*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
15*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
16*6a54128fSAndroid Build Coastguard Worker #include <string.h>
17*6a54128fSAndroid Build Coastguard Worker #include <ctype.h>
18*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
19*6a54128fSAndroid Build Coastguard Worker #include <sys/utsname.h>
20*6a54128fSAndroid Build Coastguard Worker #endif
21*6a54128fSAndroid Build Coastguard Worker 
22*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_CONIO_H
23*6a54128fSAndroid Build Coastguard Worker #undef HAVE_TERMIOS_H
24*6a54128fSAndroid Build Coastguard Worker #include <conio.h>
25*6a54128fSAndroid Build Coastguard Worker #define read_a_char()	getch()
26*6a54128fSAndroid Build Coastguard Worker #else
27*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_TERMIOS_H
28*6a54128fSAndroid Build Coastguard Worker #include <termios.h>
29*6a54128fSAndroid Build Coastguard Worker #endif
30*6a54128fSAndroid Build Coastguard Worker #endif
31*6a54128fSAndroid Build Coastguard Worker 
32*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MALLOC_H
33*6a54128fSAndroid Build Coastguard Worker #include <malloc.h>
34*6a54128fSAndroid Build Coastguard Worker #endif
35*6a54128fSAndroid Build Coastguard Worker 
36*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_ERRNO_H
37*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
38*6a54128fSAndroid Build Coastguard Worker #endif
39*6a54128fSAndroid Build Coastguard Worker 
40*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
41*6a54128fSAndroid Build Coastguard Worker 
42*6a54128fSAndroid Build Coastguard Worker extern e2fsck_t e2fsck_global_ctx;   /* Try your very best not to use this! */
43*6a54128fSAndroid Build Coastguard Worker 
44*6a54128fSAndroid Build Coastguard Worker #include <stdarg.h>
45*6a54128fSAndroid Build Coastguard Worker #include <time.h>
46*6a54128fSAndroid Build Coastguard Worker #include <sys/time.h>
47*6a54128fSAndroid Build Coastguard Worker #include <sys/resource.h>
48*6a54128fSAndroid Build Coastguard Worker 
fatal_error(e2fsck_t ctx,const char * msg)49*6a54128fSAndroid Build Coastguard Worker void fatal_error(e2fsck_t ctx, const char *msg)
50*6a54128fSAndroid Build Coastguard Worker {
51*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
52*6a54128fSAndroid Build Coastguard Worker 	int exit_value = FSCK_ERROR;
53*6a54128fSAndroid Build Coastguard Worker 
54*6a54128fSAndroid Build Coastguard Worker 	if (msg)
55*6a54128fSAndroid Build Coastguard Worker 		fprintf (stderr, "e2fsck: %s\n", msg);
56*6a54128fSAndroid Build Coastguard Worker 	if (!fs)
57*6a54128fSAndroid Build Coastguard Worker 		goto out;
58*6a54128fSAndroid Build Coastguard Worker 	if (fs->io && fs->super) {
59*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mmp_stop(ctx->fs);
60*6a54128fSAndroid Build Coastguard Worker 		if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
61*6a54128fSAndroid Build Coastguard Worker 			io_channel_flush(ctx->fs->io);
62*6a54128fSAndroid Build Coastguard Worker 		else
63*6a54128fSAndroid Build Coastguard Worker 			log_err(ctx, "e2fsck: io manager magic bad!\n");
64*6a54128fSAndroid Build Coastguard Worker 	}
65*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_test_changed(fs)) {
66*6a54128fSAndroid Build Coastguard Worker 		exit_value |= FSCK_NONDESTRUCT;
67*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
68*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name);
69*6a54128fSAndroid Build Coastguard Worker 		if (ctx->mount_flags & EXT2_MF_ISROOT)
70*6a54128fSAndroid Build Coastguard Worker 			exit_value |= FSCK_REBOOT;
71*6a54128fSAndroid Build Coastguard Worker 	}
72*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_test_valid(fs)) {
73*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
74*6a54128fSAndroid Build Coastguard Worker 			       "errors **********\n\n"), ctx->device_name);
75*6a54128fSAndroid Build Coastguard Worker 		exit_value |= FSCK_UNCORRECTED;
76*6a54128fSAndroid Build Coastguard Worker 		exit_value &= ~FSCK_NONDESTRUCT;
77*6a54128fSAndroid Build Coastguard Worker 	}
78*6a54128fSAndroid Build Coastguard Worker out:
79*6a54128fSAndroid Build Coastguard Worker 	ctx->flags |= E2F_FLAG_ABORT;
80*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_SETJMP_OK)
81*6a54128fSAndroid Build Coastguard Worker 		longjmp(ctx->abort_loc, 1);
82*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf)
83*6a54128fSAndroid Build Coastguard Worker 		fprintf(ctx->logf, "Exit status: %d\n", exit_value);
84*6a54128fSAndroid Build Coastguard Worker 	exit(exit_value);
85*6a54128fSAndroid Build Coastguard Worker }
86*6a54128fSAndroid Build Coastguard Worker 
log_out(e2fsck_t ctx,const char * fmt,...)87*6a54128fSAndroid Build Coastguard Worker void log_out(e2fsck_t ctx, const char *fmt, ...)
88*6a54128fSAndroid Build Coastguard Worker {
89*6a54128fSAndroid Build Coastguard Worker 	va_list pvar;
90*6a54128fSAndroid Build Coastguard Worker 
91*6a54128fSAndroid Build Coastguard Worker 	va_start(pvar, fmt);
92*6a54128fSAndroid Build Coastguard Worker 	vprintf(fmt, pvar);
93*6a54128fSAndroid Build Coastguard Worker 	va_end(pvar);
94*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf) {
95*6a54128fSAndroid Build Coastguard Worker 		va_start(pvar, fmt);
96*6a54128fSAndroid Build Coastguard Worker 		vfprintf(ctx->logf, fmt, pvar);
97*6a54128fSAndroid Build Coastguard Worker 		va_end(pvar);
98*6a54128fSAndroid Build Coastguard Worker 	}
99*6a54128fSAndroid Build Coastguard Worker }
100*6a54128fSAndroid Build Coastguard Worker 
log_err(e2fsck_t ctx,const char * fmt,...)101*6a54128fSAndroid Build Coastguard Worker void log_err(e2fsck_t ctx, const char *fmt, ...)
102*6a54128fSAndroid Build Coastguard Worker {
103*6a54128fSAndroid Build Coastguard Worker 	va_list pvar;
104*6a54128fSAndroid Build Coastguard Worker 
105*6a54128fSAndroid Build Coastguard Worker 	va_start(pvar, fmt);
106*6a54128fSAndroid Build Coastguard Worker 	vfprintf(stderr, fmt, pvar);
107*6a54128fSAndroid Build Coastguard Worker 	va_end(pvar);
108*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf) {
109*6a54128fSAndroid Build Coastguard Worker 		va_start(pvar, fmt);
110*6a54128fSAndroid Build Coastguard Worker 		vfprintf(ctx->logf, fmt, pvar);
111*6a54128fSAndroid Build Coastguard Worker 		va_end(pvar);
112*6a54128fSAndroid Build Coastguard Worker 	}
113*6a54128fSAndroid Build Coastguard Worker }
114*6a54128fSAndroid Build Coastguard Worker 
e2fsck_allocate_memory(e2fsck_t ctx,unsigned long size,const char * description)115*6a54128fSAndroid Build Coastguard Worker void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned long size,
116*6a54128fSAndroid Build Coastguard Worker 			     const char *description)
117*6a54128fSAndroid Build Coastguard Worker {
118*6a54128fSAndroid Build Coastguard Worker 	void *ret;
119*6a54128fSAndroid Build Coastguard Worker 	char buf[256];
120*6a54128fSAndroid Build Coastguard Worker 
121*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_ALLOCATE_MEMORY
122*6a54128fSAndroid Build Coastguard Worker 	printf("Allocating %lu bytes for %s...\n", size, description);
123*6a54128fSAndroid Build Coastguard Worker #endif
124*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_get_memzero(size, &ret)) {
125*6a54128fSAndroid Build Coastguard Worker 		sprintf(buf, "Can't allocate %lu bytes for %s\n",
126*6a54128fSAndroid Build Coastguard Worker 			size, description);
127*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, buf);
128*6a54128fSAndroid Build Coastguard Worker 	}
129*6a54128fSAndroid Build Coastguard Worker 
130*6a54128fSAndroid Build Coastguard Worker 	return ret;
131*6a54128fSAndroid Build Coastguard Worker }
132*6a54128fSAndroid Build Coastguard Worker 
string_copy(e2fsck_t ctx EXT2FS_ATTR ((unused)),const char * str,size_t len)133*6a54128fSAndroid Build Coastguard Worker char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
134*6a54128fSAndroid Build Coastguard Worker 		  const char *str, size_t len)
135*6a54128fSAndroid Build Coastguard Worker {
136*6a54128fSAndroid Build Coastguard Worker 	char	*ret;
137*6a54128fSAndroid Build Coastguard Worker 
138*6a54128fSAndroid Build Coastguard Worker 	if (!str)
139*6a54128fSAndroid Build Coastguard Worker 		return NULL;
140*6a54128fSAndroid Build Coastguard Worker 	if (!len)
141*6a54128fSAndroid Build Coastguard Worker 		len = strlen(str);
142*6a54128fSAndroid Build Coastguard Worker 	ret = malloc(len+1);
143*6a54128fSAndroid Build Coastguard Worker 	if (ret) {
144*6a54128fSAndroid Build Coastguard Worker 		strncpy(ret, str, len);
145*6a54128fSAndroid Build Coastguard Worker 		ret[len] = 0;
146*6a54128fSAndroid Build Coastguard Worker 	}
147*6a54128fSAndroid Build Coastguard Worker 	return ret;
148*6a54128fSAndroid Build Coastguard Worker }
149*6a54128fSAndroid Build Coastguard Worker 
150*6a54128fSAndroid Build Coastguard Worker #ifndef HAVE_STRNLEN
151*6a54128fSAndroid Build Coastguard Worker /*
152*6a54128fSAndroid Build Coastguard Worker  * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
153*6a54128fSAndroid Build Coastguard Worker  * provide our own.
154*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_strnlen(const char * s,int count)155*6a54128fSAndroid Build Coastguard Worker int e2fsck_strnlen(const char * s, int count)
156*6a54128fSAndroid Build Coastguard Worker {
157*6a54128fSAndroid Build Coastguard Worker 	const char *cp = s;
158*6a54128fSAndroid Build Coastguard Worker 
159*6a54128fSAndroid Build Coastguard Worker 	while (count-- && *cp)
160*6a54128fSAndroid Build Coastguard Worker 		cp++;
161*6a54128fSAndroid Build Coastguard Worker 	return cp - s;
162*6a54128fSAndroid Build Coastguard Worker }
163*6a54128fSAndroid Build Coastguard Worker #endif
164*6a54128fSAndroid Build Coastguard Worker 
165*6a54128fSAndroid Build Coastguard Worker #ifndef HAVE_CONIO_H
read_a_char(void)166*6a54128fSAndroid Build Coastguard Worker static int read_a_char(void)
167*6a54128fSAndroid Build Coastguard Worker {
168*6a54128fSAndroid Build Coastguard Worker 	char	c;
169*6a54128fSAndroid Build Coastguard Worker 	int	r;
170*6a54128fSAndroid Build Coastguard Worker 	int	fail = 0;
171*6a54128fSAndroid Build Coastguard Worker 
172*6a54128fSAndroid Build Coastguard Worker 	while(1) {
173*6a54128fSAndroid Build Coastguard Worker 		if (e2fsck_global_ctx &&
174*6a54128fSAndroid Build Coastguard Worker 		    (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
175*6a54128fSAndroid Build Coastguard Worker 			return 3;
176*6a54128fSAndroid Build Coastguard Worker 		}
177*6a54128fSAndroid Build Coastguard Worker 		r = read(0, &c, 1);
178*6a54128fSAndroid Build Coastguard Worker 		if (r == 1)
179*6a54128fSAndroid Build Coastguard Worker 			return c;
180*6a54128fSAndroid Build Coastguard Worker 		if (fail++ > 100)
181*6a54128fSAndroid Build Coastguard Worker 			break;
182*6a54128fSAndroid Build Coastguard Worker 	}
183*6a54128fSAndroid Build Coastguard Worker 	return EOF;
184*6a54128fSAndroid Build Coastguard Worker }
185*6a54128fSAndroid Build Coastguard Worker #endif
186*6a54128fSAndroid Build Coastguard Worker 
ask_yn(e2fsck_t ctx,const char * string,int def)187*6a54128fSAndroid Build Coastguard Worker int ask_yn(e2fsck_t ctx, const char * string, int def)
188*6a54128fSAndroid Build Coastguard Worker {
189*6a54128fSAndroid Build Coastguard Worker 	int		c;
190*6a54128fSAndroid Build Coastguard Worker 	const char	*defstr;
191*6a54128fSAndroid Build Coastguard Worker 	const char	*short_yes = _("yY");
192*6a54128fSAndroid Build Coastguard Worker 	const char	*short_no = _("nN");
193*6a54128fSAndroid Build Coastguard Worker 	const char	*short_yesall = _("aA");
194*6a54128fSAndroid Build Coastguard Worker 	const char	*english_yes = "yY";
195*6a54128fSAndroid Build Coastguard Worker 	const char	*english_no = "nN";
196*6a54128fSAndroid Build Coastguard Worker 	const char	*english_yesall = "aA";
197*6a54128fSAndroid Build Coastguard Worker 	const char	*yesall_prompt = _(" ('a' enables 'yes' to all) ");
198*6a54128fSAndroid Build Coastguard Worker 	const char	*extra_prompt = "";
199*6a54128fSAndroid Build Coastguard Worker 	static int	yes_answers;
200*6a54128fSAndroid Build Coastguard Worker 
201*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_TERMIOS_H
202*6a54128fSAndroid Build Coastguard Worker 	struct termios	termios, tmp;
203*6a54128fSAndroid Build Coastguard Worker 
204*6a54128fSAndroid Build Coastguard Worker 	if (tcgetattr (0, &termios) < 0)
205*6a54128fSAndroid Build Coastguard Worker 		memset(&termios, 0, sizeof(termios));
206*6a54128fSAndroid Build Coastguard Worker 	tmp = termios;
207*6a54128fSAndroid Build Coastguard Worker 	tmp.c_lflag &= ~(ICANON | ECHO);
208*6a54128fSAndroid Build Coastguard Worker 	tmp.c_cc[VMIN] = 1;
209*6a54128fSAndroid Build Coastguard Worker 	tmp.c_cc[VTIME] = 0;
210*6a54128fSAndroid Build Coastguard Worker 	tcsetattr (0, TCSANOW, &tmp);
211*6a54128fSAndroid Build Coastguard Worker #endif
212*6a54128fSAndroid Build Coastguard Worker 
213*6a54128fSAndroid Build Coastguard Worker 	if (def == 1)
214*6a54128fSAndroid Build Coastguard Worker 		defstr = _(_("<y>"));
215*6a54128fSAndroid Build Coastguard Worker 	else if (def == 0)
216*6a54128fSAndroid Build Coastguard Worker 		defstr = _(_("<n>"));
217*6a54128fSAndroid Build Coastguard Worker 	else
218*6a54128fSAndroid Build Coastguard Worker 		defstr = _(" (y/n)");
219*6a54128fSAndroid Build Coastguard Worker 	/*
220*6a54128fSAndroid Build Coastguard Worker 	 * If the user presses 'y' more than 8 (but less than 12) times in
221*6a54128fSAndroid Build Coastguard Worker 	 * succession without pressing anything else, display a hint about
222*6a54128fSAndroid Build Coastguard Worker 	 * yes-to-all mode.
223*6a54128fSAndroid Build Coastguard Worker 	 */
224*6a54128fSAndroid Build Coastguard Worker 	if (yes_answers > 12)
225*6a54128fSAndroid Build Coastguard Worker 		yes_answers = -1;
226*6a54128fSAndroid Build Coastguard Worker 	else if (yes_answers > 8)
227*6a54128fSAndroid Build Coastguard Worker 		extra_prompt = yesall_prompt;
228*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
229*6a54128fSAndroid Build Coastguard Worker 	while (1) {
230*6a54128fSAndroid Build Coastguard Worker 		fflush (stdout);
231*6a54128fSAndroid Build Coastguard Worker 		if ((c = read_a_char()) == EOF)
232*6a54128fSAndroid Build Coastguard Worker 			break;
233*6a54128fSAndroid Build Coastguard Worker 		if (c == 3) {
234*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_TERMIOS_H
235*6a54128fSAndroid Build Coastguard Worker 			tcsetattr (0, TCSANOW, &termios);
236*6a54128fSAndroid Build Coastguard Worker #endif
237*6a54128fSAndroid Build Coastguard Worker 			if (ctx->flags & E2F_FLAG_SETJMP_OK) {
238*6a54128fSAndroid Build Coastguard Worker 				log_out(ctx, "\n");
239*6a54128fSAndroid Build Coastguard Worker 				longjmp(e2fsck_global_ctx->abort_loc, 1);
240*6a54128fSAndroid Build Coastguard Worker 			}
241*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s", _("cancelled!\n"));
242*6a54128fSAndroid Build Coastguard Worker 			yes_answers = 0;
243*6a54128fSAndroid Build Coastguard Worker 			return 0;
244*6a54128fSAndroid Build Coastguard Worker 		}
245*6a54128fSAndroid Build Coastguard Worker 		if (strchr(short_yes, (char) c)) {
246*6a54128fSAndroid Build Coastguard Worker 		do_yes:
247*6a54128fSAndroid Build Coastguard Worker 			def = 1;
248*6a54128fSAndroid Build Coastguard Worker 			if (yes_answers >= 0)
249*6a54128fSAndroid Build Coastguard Worker 				yes_answers++;
250*6a54128fSAndroid Build Coastguard Worker 			break;
251*6a54128fSAndroid Build Coastguard Worker 		} else if (strchr(short_no, (char) c)) {
252*6a54128fSAndroid Build Coastguard Worker 		do_no:
253*6a54128fSAndroid Build Coastguard Worker 			def = 0;
254*6a54128fSAndroid Build Coastguard Worker 			yes_answers = -1;
255*6a54128fSAndroid Build Coastguard Worker 			break;
256*6a54128fSAndroid Build Coastguard Worker 		} else if (strchr(short_yesall, (char)c)) {
257*6a54128fSAndroid Build Coastguard Worker 		do_all:
258*6a54128fSAndroid Build Coastguard Worker 			def = 2;
259*6a54128fSAndroid Build Coastguard Worker 			yes_answers = -1;
260*6a54128fSAndroid Build Coastguard Worker 			ctx->options |= E2F_OPT_YES;
261*6a54128fSAndroid Build Coastguard Worker 			break;
262*6a54128fSAndroid Build Coastguard Worker 		} else if (strchr(english_yes, (char) c)) {
263*6a54128fSAndroid Build Coastguard Worker 			goto do_yes;
264*6a54128fSAndroid Build Coastguard Worker 		} else if (strchr(english_no, (char) c)) {
265*6a54128fSAndroid Build Coastguard Worker 			goto do_no;
266*6a54128fSAndroid Build Coastguard Worker 		} else if (strchr(english_yesall, (char) c)) {
267*6a54128fSAndroid Build Coastguard Worker 			goto do_all;
268*6a54128fSAndroid Build Coastguard Worker 		} else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
269*6a54128fSAndroid Build Coastguard Worker 			yes_answers = -1;
270*6a54128fSAndroid Build Coastguard Worker 			break;
271*6a54128fSAndroid Build Coastguard Worker 		}
272*6a54128fSAndroid Build Coastguard Worker 	}
273*6a54128fSAndroid Build Coastguard Worker 	if (def == 2)
274*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", _("yes to all\n"));
275*6a54128fSAndroid Build Coastguard Worker 	else if (def)
276*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", _("yes\n"));
277*6a54128fSAndroid Build Coastguard Worker 	else
278*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s", _("no\n"));
279*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_TERMIOS_H
280*6a54128fSAndroid Build Coastguard Worker 	tcsetattr (0, TCSANOW, &termios);
281*6a54128fSAndroid Build Coastguard Worker #endif
282*6a54128fSAndroid Build Coastguard Worker 	return def;
283*6a54128fSAndroid Build Coastguard Worker }
284*6a54128fSAndroid Build Coastguard Worker 
ask(e2fsck_t ctx,const char * string,int def)285*6a54128fSAndroid Build Coastguard Worker int ask (e2fsck_t ctx, const char * string, int def)
286*6a54128fSAndroid Build Coastguard Worker {
287*6a54128fSAndroid Build Coastguard Worker 	if (ctx->options & E2F_OPT_NO) {
288*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s? no\n\n"), string);
289*6a54128fSAndroid Build Coastguard Worker 		return 0;
290*6a54128fSAndroid Build Coastguard Worker 	}
291*6a54128fSAndroid Build Coastguard Worker 	if (ctx->options & E2F_OPT_YES) {
292*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("%s? yes\n\n"), string);
293*6a54128fSAndroid Build Coastguard Worker 		return 1;
294*6a54128fSAndroid Build Coastguard Worker 	}
295*6a54128fSAndroid Build Coastguard Worker 	if (ctx->options & E2F_OPT_PREEN) {
296*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
297*6a54128fSAndroid Build Coastguard Worker 		return def;
298*6a54128fSAndroid Build Coastguard Worker 	}
299*6a54128fSAndroid Build Coastguard Worker 	return ask_yn(ctx, string, def);
300*6a54128fSAndroid Build Coastguard Worker }
301*6a54128fSAndroid Build Coastguard Worker 
e2fsck_read_bitmaps(e2fsck_t ctx)302*6a54128fSAndroid Build Coastguard Worker void e2fsck_read_bitmaps(e2fsck_t ctx)
303*6a54128fSAndroid Build Coastguard Worker {
304*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
305*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
306*6a54128fSAndroid Build Coastguard Worker 	const char	*old_op;
307*6a54128fSAndroid Build Coastguard Worker 	unsigned int	save_type;
308*6a54128fSAndroid Build Coastguard Worker 	int		flags;
309*6a54128fSAndroid Build Coastguard Worker 
310*6a54128fSAndroid Build Coastguard Worker 	if (ctx->invalid_bitmaps) {
311*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, 0,
312*6a54128fSAndroid Build Coastguard Worker 		    _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
313*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name);
314*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
315*6a54128fSAndroid Build Coastguard Worker 	}
316*6a54128fSAndroid Build Coastguard Worker 
317*6a54128fSAndroid Build Coastguard Worker 	old_op = ehandler_operation(_("reading inode and block bitmaps"));
318*6a54128fSAndroid Build Coastguard Worker 	e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
319*6a54128fSAndroid Build Coastguard Worker 			       &save_type);
320*6a54128fSAndroid Build Coastguard Worker 	flags = ctx->fs->flags;
321*6a54128fSAndroid Build Coastguard Worker 	ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
322*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_bitmaps(fs);
323*6a54128fSAndroid Build Coastguard Worker 	ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
324*6a54128fSAndroid Build Coastguard Worker 			 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
325*6a54128fSAndroid Build Coastguard Worker 	fs->default_bitmap_type = save_type;
326*6a54128fSAndroid Build Coastguard Worker 	ehandler_operation(old_op);
327*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
328*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval,
329*6a54128fSAndroid Build Coastguard Worker 			_("while retrying to read bitmaps for %s"),
330*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name);
331*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
332*6a54128fSAndroid Build Coastguard Worker 	}
333*6a54128fSAndroid Build Coastguard Worker }
334*6a54128fSAndroid Build Coastguard Worker 
e2fsck_write_bitmaps(e2fsck_t ctx)335*6a54128fSAndroid Build Coastguard Worker void e2fsck_write_bitmaps(e2fsck_t ctx)
336*6a54128fSAndroid Build Coastguard Worker {
337*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
338*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
339*6a54128fSAndroid Build Coastguard Worker 	const char	*old_op;
340*6a54128fSAndroid Build Coastguard Worker 
341*6a54128fSAndroid Build Coastguard Worker 	old_op = ehandler_operation(_("writing block and inode bitmaps"));
342*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_bitmaps(fs);
343*6a54128fSAndroid Build Coastguard Worker 	ehandler_operation(old_op);
344*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
345*6a54128fSAndroid Build Coastguard Worker 		com_err(ctx->program_name, retval,
346*6a54128fSAndroid Build Coastguard Worker 			_("while rewriting block and inode bitmaps for %s"),
347*6a54128fSAndroid Build Coastguard Worker 			ctx->device_name);
348*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
349*6a54128fSAndroid Build Coastguard Worker 	}
350*6a54128fSAndroid Build Coastguard Worker }
351*6a54128fSAndroid Build Coastguard Worker 
preenhalt(e2fsck_t ctx)352*6a54128fSAndroid Build Coastguard Worker void preenhalt(e2fsck_t ctx)
353*6a54128fSAndroid Build Coastguard Worker {
354*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
355*6a54128fSAndroid Build Coastguard Worker 
356*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_PREEN))
357*6a54128fSAndroid Build Coastguard Worker 		return;
358*6a54128fSAndroid Build Coastguard Worker 	log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
359*6a54128fSAndroid Build Coastguard Worker 		"RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
360*6a54128fSAndroid Build Coastguard Worker 	       ctx->device_name);
361*6a54128fSAndroid Build Coastguard Worker 	ctx->flags |= E2F_FLAG_EXITING;
362*6a54128fSAndroid Build Coastguard Worker 	if (fs != NULL) {
363*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_state |= EXT2_ERROR_FS;
364*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
365*6a54128fSAndroid Build Coastguard Worker 		ext2fs_close_free(&fs);
366*6a54128fSAndroid Build Coastguard Worker 	}
367*6a54128fSAndroid Build Coastguard Worker 	exit(FSCK_UNCORRECTED);
368*6a54128fSAndroid Build Coastguard Worker }
369*6a54128fSAndroid Build Coastguard Worker 
370*6a54128fSAndroid Build Coastguard Worker #ifdef RESOURCE_TRACK
init_resource_track(struct resource_track * track,io_channel channel)371*6a54128fSAndroid Build Coastguard Worker void init_resource_track(struct resource_track *track, io_channel channel)
372*6a54128fSAndroid Build Coastguard Worker {
373*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETRUSAGE
374*6a54128fSAndroid Build Coastguard Worker 	struct rusage r;
375*6a54128fSAndroid Build Coastguard Worker #endif
376*6a54128fSAndroid Build Coastguard Worker 	io_stats io_start = 0;
377*6a54128fSAndroid Build Coastguard Worker 
378*6a54128fSAndroid Build Coastguard Worker 	track->brk_start = sbrk(0);
379*6a54128fSAndroid Build Coastguard Worker 	gettimeofday(&track->time_start, 0);
380*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETRUSAGE
381*6a54128fSAndroid Build Coastguard Worker #ifdef sun
382*6a54128fSAndroid Build Coastguard Worker 	memset(&r, 0, sizeof(struct rusage));
383*6a54128fSAndroid Build Coastguard Worker #endif
384*6a54128fSAndroid Build Coastguard Worker 	getrusage(RUSAGE_SELF, &r);
385*6a54128fSAndroid Build Coastguard Worker 	track->user_start = r.ru_utime;
386*6a54128fSAndroid Build Coastguard Worker 	track->system_start = r.ru_stime;
387*6a54128fSAndroid Build Coastguard Worker #else
388*6a54128fSAndroid Build Coastguard Worker 	track->user_start.tv_sec = track->user_start.tv_usec = 0;
389*6a54128fSAndroid Build Coastguard Worker 	track->system_start.tv_sec = track->system_start.tv_usec = 0;
390*6a54128fSAndroid Build Coastguard Worker #endif
391*6a54128fSAndroid Build Coastguard Worker 	track->bytes_read = 0;
392*6a54128fSAndroid Build Coastguard Worker 	track->bytes_written = 0;
393*6a54128fSAndroid Build Coastguard Worker 	if (channel && channel->manager && channel->manager->get_stats)
394*6a54128fSAndroid Build Coastguard Worker 		channel->manager->get_stats(channel, &io_start);
395*6a54128fSAndroid Build Coastguard Worker 	if (io_start) {
396*6a54128fSAndroid Build Coastguard Worker 		track->bytes_read = io_start->bytes_read;
397*6a54128fSAndroid Build Coastguard Worker 		track->bytes_written = io_start->bytes_written;
398*6a54128fSAndroid Build Coastguard Worker 	}
399*6a54128fSAndroid Build Coastguard Worker }
400*6a54128fSAndroid Build Coastguard Worker 
401*6a54128fSAndroid Build Coastguard Worker #ifdef __GNUC__
402*6a54128fSAndroid Build Coastguard Worker #define _INLINE_ __inline__
403*6a54128fSAndroid Build Coastguard Worker #else
404*6a54128fSAndroid Build Coastguard Worker #define _INLINE_
405*6a54128fSAndroid Build Coastguard Worker #endif
406*6a54128fSAndroid Build Coastguard Worker 
timeval_subtract(struct timeval * tv1,struct timeval * tv2)407*6a54128fSAndroid Build Coastguard Worker static _INLINE_ float timeval_subtract(struct timeval *tv1,
408*6a54128fSAndroid Build Coastguard Worker 				       struct timeval *tv2)
409*6a54128fSAndroid Build Coastguard Worker {
410*6a54128fSAndroid Build Coastguard Worker 	return ((tv1->tv_sec - tv2->tv_sec) +
411*6a54128fSAndroid Build Coastguard Worker 		((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
412*6a54128fSAndroid Build Coastguard Worker }
413*6a54128fSAndroid Build Coastguard Worker 
print_resource_track(e2fsck_t ctx,const char * desc,struct resource_track * track,io_channel channel)414*6a54128fSAndroid Build Coastguard Worker void print_resource_track(e2fsck_t ctx, const char *desc,
415*6a54128fSAndroid Build Coastguard Worker 			  struct resource_track *track, io_channel channel)
416*6a54128fSAndroid Build Coastguard Worker {
417*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETRUSAGE
418*6a54128fSAndroid Build Coastguard Worker 	struct rusage r;
419*6a54128fSAndroid Build Coastguard Worker #endif
420*6a54128fSAndroid Build Coastguard Worker 	struct timeval time_end;
421*6a54128fSAndroid Build Coastguard Worker 
422*6a54128fSAndroid Build Coastguard Worker 	if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
423*6a54128fSAndroid Build Coastguard Worker 	    (!desc && !(ctx->options & E2F_OPT_TIME)))
424*6a54128fSAndroid Build Coastguard Worker 		return;
425*6a54128fSAndroid Build Coastguard Worker 
426*6a54128fSAndroid Build Coastguard Worker 	e2fsck_clear_progbar(ctx);
427*6a54128fSAndroid Build Coastguard Worker 	gettimeofday(&time_end, 0);
428*6a54128fSAndroid Build Coastguard Worker 
429*6a54128fSAndroid Build Coastguard Worker 	if (desc)
430*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "%s: ", desc);
431*6a54128fSAndroid Build Coastguard Worker 
432*6a54128fSAndroid Build Coastguard Worker #define kbytes(x)	(((unsigned long long)(x) + 1023) / 1024)
433*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MALLINFO2
434*6a54128fSAndroid Build Coastguard Worker 	if (1) {
435*6a54128fSAndroid Build Coastguard Worker 		struct mallinfo2 malloc_info = mallinfo2();
436*6a54128fSAndroid Build Coastguard Worker 
437*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("Memory used: %lluk/%lluk (%lluk/%lluk), "),
438*6a54128fSAndroid Build Coastguard Worker 			kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
439*6a54128fSAndroid Build Coastguard Worker 			kbytes(malloc_info.uordblks),
440*6a54128fSAndroid Build Coastguard Worker 			kbytes(malloc_info.fordblks));
441*6a54128fSAndroid Build Coastguard Worker 	} else
442*6a54128fSAndroid Build Coastguard Worker #elif defined HAVE_MALLINFO
443*6a54128fSAndroid Build Coastguard Worker 	/* don't use mallinfo() if over 2GB used, since it returns "int" */
444*6a54128fSAndroid Build Coastguard Worker 	if ((char *)sbrk(0) - (char *)track->brk_start < 2LL << 30) {
445*6a54128fSAndroid Build Coastguard Worker 		struct mallinfo	malloc_info = mallinfo();
446*6a54128fSAndroid Build Coastguard Worker 
447*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, _("Memory used: %lluk/%lluk (%lluk/%lluk), "),
448*6a54128fSAndroid Build Coastguard Worker 			kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
449*6a54128fSAndroid Build Coastguard Worker 			kbytes(malloc_info.uordblks),
450*6a54128fSAndroid Build Coastguard Worker 			kbytes(malloc_info.fordblks));
451*6a54128fSAndroid Build Coastguard Worker 	} else
452*6a54128fSAndroid Build Coastguard Worker #endif
453*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, _("Memory used: %lluk, "),
454*6a54128fSAndroid Build Coastguard Worker 		kbytes(((char *)sbrk(0)) - ((char *)track->brk_start)));
455*6a54128fSAndroid Build Coastguard Worker 
456*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETRUSAGE
457*6a54128fSAndroid Build Coastguard Worker 	getrusage(RUSAGE_SELF, &r);
458*6a54128fSAndroid Build Coastguard Worker 
459*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
460*6a54128fSAndroid Build Coastguard Worker 		timeval_subtract(&time_end, &track->time_start),
461*6a54128fSAndroid Build Coastguard Worker 		timeval_subtract(&r.ru_utime, &track->user_start),
462*6a54128fSAndroid Build Coastguard Worker 		timeval_subtract(&r.ru_stime, &track->system_start));
463*6a54128fSAndroid Build Coastguard Worker #else
464*6a54128fSAndroid Build Coastguard Worker 	log_out(ctx, _("elapsed time: %6.3f\n"),
465*6a54128fSAndroid Build Coastguard Worker 		timeval_subtract(&time_end, &track->time_start));
466*6a54128fSAndroid Build Coastguard Worker #endif
467*6a54128fSAndroid Build Coastguard Worker #define mbytes(x)	(((x) + 1048575) / 1048576)
468*6a54128fSAndroid Build Coastguard Worker 	if (channel && channel->manager && channel->manager->get_stats) {
469*6a54128fSAndroid Build Coastguard Worker 		io_stats delta = 0;
470*6a54128fSAndroid Build Coastguard Worker 		unsigned long long bytes_read = 0;
471*6a54128fSAndroid Build Coastguard Worker 		unsigned long long bytes_written = 0;
472*6a54128fSAndroid Build Coastguard Worker 
473*6a54128fSAndroid Build Coastguard Worker 		if (desc)
474*6a54128fSAndroid Build Coastguard Worker 			log_out(ctx, "%s: ", desc);
475*6a54128fSAndroid Build Coastguard Worker 
476*6a54128fSAndroid Build Coastguard Worker 		channel->manager->get_stats(channel, &delta);
477*6a54128fSAndroid Build Coastguard Worker 		if (delta) {
478*6a54128fSAndroid Build Coastguard Worker 			bytes_read = delta->bytes_read - track->bytes_read;
479*6a54128fSAndroid Build Coastguard Worker 			bytes_written = delta->bytes_written -
480*6a54128fSAndroid Build Coastguard Worker 				track->bytes_written;
481*6a54128fSAndroid Build Coastguard Worker 		}
482*6a54128fSAndroid Build Coastguard Worker 		log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
483*6a54128fSAndroid Build Coastguard Worker 			"rate: %.2fMB/s\n",
484*6a54128fSAndroid Build Coastguard Worker 			mbytes(bytes_read), mbytes(bytes_written),
485*6a54128fSAndroid Build Coastguard Worker 			(double)mbytes(bytes_read + bytes_written) /
486*6a54128fSAndroid Build Coastguard Worker 			timeval_subtract(&time_end, &track->time_start));
487*6a54128fSAndroid Build Coastguard Worker 	}
488*6a54128fSAndroid Build Coastguard Worker }
489*6a54128fSAndroid Build Coastguard Worker #endif /* RESOURCE_TRACK */
490*6a54128fSAndroid Build Coastguard Worker 
e2fsck_read_inode(e2fsck_t ctx,unsigned long ino,struct ext2_inode * inode,const char * proc)491*6a54128fSAndroid Build Coastguard Worker void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
492*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_inode * inode, const char *proc)
493*6a54128fSAndroid Build Coastguard Worker {
494*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
495*6a54128fSAndroid Build Coastguard Worker 
496*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_inode(ctx->fs, ino, inode);
497*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
498*6a54128fSAndroid Build Coastguard Worker 		com_err("ext2fs_read_inode", retval,
499*6a54128fSAndroid Build Coastguard Worker 			_("while reading inode %lu in %s"), ino, proc);
500*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
501*6a54128fSAndroid Build Coastguard Worker 	}
502*6a54128fSAndroid Build Coastguard Worker }
503*6a54128fSAndroid Build Coastguard Worker 
e2fsck_read_inode_full(e2fsck_t ctx,unsigned long ino,struct ext2_inode * inode,int bufsize,const char * proc)504*6a54128fSAndroid Build Coastguard Worker void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
505*6a54128fSAndroid Build Coastguard Worker 			    struct ext2_inode *inode, int bufsize,
506*6a54128fSAndroid Build Coastguard Worker 			    const char *proc)
507*6a54128fSAndroid Build Coastguard Worker {
508*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
509*6a54128fSAndroid Build Coastguard Worker 
510*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
511*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
512*6a54128fSAndroid Build Coastguard Worker 		com_err("ext2fs_read_inode_full", retval,
513*6a54128fSAndroid Build Coastguard Worker 			_("while reading inode %lu in %s"), ino, proc);
514*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
515*6a54128fSAndroid Build Coastguard Worker 	}
516*6a54128fSAndroid Build Coastguard Worker }
517*6a54128fSAndroid Build Coastguard Worker 
e2fsck_write_inode_full(e2fsck_t ctx,unsigned long ino,struct ext2_inode * inode,int bufsize,const char * proc)518*6a54128fSAndroid Build Coastguard Worker void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
519*6a54128fSAndroid Build Coastguard Worker 			     struct ext2_inode * inode, int bufsize,
520*6a54128fSAndroid Build Coastguard Worker 			     const char *proc)
521*6a54128fSAndroid Build Coastguard Worker {
522*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
523*6a54128fSAndroid Build Coastguard Worker 
524*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
525*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
526*6a54128fSAndroid Build Coastguard Worker 		com_err("ext2fs_write_inode", retval,
527*6a54128fSAndroid Build Coastguard Worker 			_("while writing inode %lu in %s"), ino, proc);
528*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
529*6a54128fSAndroid Build Coastguard Worker 	}
530*6a54128fSAndroid Build Coastguard Worker }
531*6a54128fSAndroid Build Coastguard Worker 
e2fsck_write_inode(e2fsck_t ctx,unsigned long ino,struct ext2_inode * inode,const char * proc)532*6a54128fSAndroid Build Coastguard Worker void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
533*6a54128fSAndroid Build Coastguard Worker 			struct ext2_inode * inode, const char *proc)
534*6a54128fSAndroid Build Coastguard Worker {
535*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
536*6a54128fSAndroid Build Coastguard Worker 
537*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_inode(ctx->fs, ino, inode);
538*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
539*6a54128fSAndroid Build Coastguard Worker 		com_err("ext2fs_write_inode", retval,
540*6a54128fSAndroid Build Coastguard Worker 			_("while writing inode %lu in %s"), ino, proc);
541*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, 0);
542*6a54128fSAndroid Build Coastguard Worker 	}
543*6a54128fSAndroid Build Coastguard Worker }
544*6a54128fSAndroid Build Coastguard Worker 
545*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
mtrace_print(char * mesg)546*6a54128fSAndroid Build Coastguard Worker void mtrace_print(char *mesg)
547*6a54128fSAndroid Build Coastguard Worker {
548*6a54128fSAndroid Build Coastguard Worker 	FILE	*malloc_get_mallstream();
549*6a54128fSAndroid Build Coastguard Worker 	FILE	*f = malloc_get_mallstream();
550*6a54128fSAndroid Build Coastguard Worker 
551*6a54128fSAndroid Build Coastguard Worker 	if (f)
552*6a54128fSAndroid Build Coastguard Worker 		fprintf(f, "============= %s\n", mesg);
553*6a54128fSAndroid Build Coastguard Worker }
554*6a54128fSAndroid Build Coastguard Worker #endif
555*6a54128fSAndroid Build Coastguard Worker 
get_backup_sb(e2fsck_t ctx,ext2_filsys fs,const char * name,io_manager manager)556*6a54128fSAndroid Build Coastguard Worker blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
557*6a54128fSAndroid Build Coastguard Worker 		      io_manager manager)
558*6a54128fSAndroid Build Coastguard Worker {
559*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb;
560*6a54128fSAndroid Build Coastguard Worker 	io_channel		io = NULL;
561*6a54128fSAndroid Build Coastguard Worker 	void			*buf = NULL;
562*6a54128fSAndroid Build Coastguard Worker 	int			blocksize;
563*6a54128fSAndroid Build Coastguard Worker 	blk64_t			superblock, ret_sb = 8193;
564*6a54128fSAndroid Build Coastguard Worker 
565*6a54128fSAndroid Build Coastguard Worker 	if (fs && fs->super) {
566*6a54128fSAndroid Build Coastguard Worker 		ret_sb = (fs->super->s_blocks_per_group +
567*6a54128fSAndroid Build Coastguard Worker 			  fs->super->s_first_data_block);
568*6a54128fSAndroid Build Coastguard Worker 		if (ctx) {
569*6a54128fSAndroid Build Coastguard Worker 			ctx->superblock = ret_sb;
570*6a54128fSAndroid Build Coastguard Worker 			ctx->blocksize = fs->blocksize;
571*6a54128fSAndroid Build Coastguard Worker 		}
572*6a54128fSAndroid Build Coastguard Worker 		return ret_sb;
573*6a54128fSAndroid Build Coastguard Worker 	}
574*6a54128fSAndroid Build Coastguard Worker 
575*6a54128fSAndroid Build Coastguard Worker 	if (ctx) {
576*6a54128fSAndroid Build Coastguard Worker 		if (ctx->blocksize) {
577*6a54128fSAndroid Build Coastguard Worker 			ret_sb = ctx->blocksize * 8;
578*6a54128fSAndroid Build Coastguard Worker 			if (ctx->blocksize == 1024)
579*6a54128fSAndroid Build Coastguard Worker 				ret_sb++;
580*6a54128fSAndroid Build Coastguard Worker 			ctx->superblock = ret_sb;
581*6a54128fSAndroid Build Coastguard Worker 			return ret_sb;
582*6a54128fSAndroid Build Coastguard Worker 		}
583*6a54128fSAndroid Build Coastguard Worker 		ctx->superblock = ret_sb;
584*6a54128fSAndroid Build Coastguard Worker 		ctx->blocksize = 1024;
585*6a54128fSAndroid Build Coastguard Worker 	}
586*6a54128fSAndroid Build Coastguard Worker 
587*6a54128fSAndroid Build Coastguard Worker 	if (!name || !manager)
588*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
589*6a54128fSAndroid Build Coastguard Worker 
590*6a54128fSAndroid Build Coastguard Worker 	if (manager->open(name, 0, &io) != 0)
591*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
592*6a54128fSAndroid Build Coastguard Worker 
593*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
594*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
595*6a54128fSAndroid Build Coastguard Worker 	sb = (struct ext2_super_block *) buf;
596*6a54128fSAndroid Build Coastguard Worker 
597*6a54128fSAndroid Build Coastguard Worker 	for (blocksize = EXT2_MIN_BLOCK_SIZE;
598*6a54128fSAndroid Build Coastguard Worker 	     blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
599*6a54128fSAndroid Build Coastguard Worker 		superblock = blocksize*8;
600*6a54128fSAndroid Build Coastguard Worker 		if (blocksize == 1024)
601*6a54128fSAndroid Build Coastguard Worker 			superblock++;
602*6a54128fSAndroid Build Coastguard Worker 		io_channel_set_blksize(io, blocksize);
603*6a54128fSAndroid Build Coastguard Worker 		if (io_channel_read_blk64(io, superblock,
604*6a54128fSAndroid Build Coastguard Worker 					-SUPERBLOCK_SIZE, buf))
605*6a54128fSAndroid Build Coastguard Worker 			continue;
606*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
607*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
608*6a54128fSAndroid Build Coastguard Worker 			ext2fs_swap_super(sb);
609*6a54128fSAndroid Build Coastguard Worker #endif
610*6a54128fSAndroid Build Coastguard Worker 		if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
611*6a54128fSAndroid Build Coastguard Worker 		    (EXT2_BLOCK_SIZE(sb) == blocksize)) {
612*6a54128fSAndroid Build Coastguard Worker 			ret_sb = superblock;
613*6a54128fSAndroid Build Coastguard Worker 			if (ctx) {
614*6a54128fSAndroid Build Coastguard Worker 				ctx->superblock = superblock;
615*6a54128fSAndroid Build Coastguard Worker 				ctx->blocksize = blocksize;
616*6a54128fSAndroid Build Coastguard Worker 			}
617*6a54128fSAndroid Build Coastguard Worker 			break;
618*6a54128fSAndroid Build Coastguard Worker 		}
619*6a54128fSAndroid Build Coastguard Worker 	}
620*6a54128fSAndroid Build Coastguard Worker 
621*6a54128fSAndroid Build Coastguard Worker cleanup:
622*6a54128fSAndroid Build Coastguard Worker 	if (io)
623*6a54128fSAndroid Build Coastguard Worker 		io_channel_close(io);
624*6a54128fSAndroid Build Coastguard Worker 	if (buf)
625*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&buf);
626*6a54128fSAndroid Build Coastguard Worker 	return (ret_sb);
627*6a54128fSAndroid Build Coastguard Worker }
628*6a54128fSAndroid Build Coastguard Worker 
629*6a54128fSAndroid Build Coastguard Worker /*
630*6a54128fSAndroid Build Coastguard Worker  * Given a mode, return the ext2 file type
631*6a54128fSAndroid Build Coastguard Worker  */
ext2_file_type(unsigned int mode)632*6a54128fSAndroid Build Coastguard Worker int ext2_file_type(unsigned int mode)
633*6a54128fSAndroid Build Coastguard Worker {
634*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISREG(mode))
635*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_REG_FILE;
636*6a54128fSAndroid Build Coastguard Worker 
637*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISDIR(mode))
638*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_DIR;
639*6a54128fSAndroid Build Coastguard Worker 
640*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISCHR(mode))
641*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_CHRDEV;
642*6a54128fSAndroid Build Coastguard Worker 
643*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISBLK(mode))
644*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_BLKDEV;
645*6a54128fSAndroid Build Coastguard Worker 
646*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISLNK(mode))
647*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_SYMLINK;
648*6a54128fSAndroid Build Coastguard Worker 
649*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISFIFO(mode))
650*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_FIFO;
651*6a54128fSAndroid Build Coastguard Worker 
652*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISSOCK(mode))
653*6a54128fSAndroid Build Coastguard Worker 		return EXT2_FT_SOCK;
654*6a54128fSAndroid Build Coastguard Worker 
655*6a54128fSAndroid Build Coastguard Worker 	return 0;
656*6a54128fSAndroid Build Coastguard Worker }
657*6a54128fSAndroid Build Coastguard Worker 
658*6a54128fSAndroid Build Coastguard Worker /*
659*6a54128fSAndroid Build Coastguard Worker  * Check to see if a filesystem is in /proc/filesystems.
660*6a54128fSAndroid Build Coastguard Worker  * Returns 1 if found, 0 if not
661*6a54128fSAndroid Build Coastguard Worker  */
fs_proc_check(const char * fs_name)662*6a54128fSAndroid Build Coastguard Worker int fs_proc_check(const char *fs_name)
663*6a54128fSAndroid Build Coastguard Worker {
664*6a54128fSAndroid Build Coastguard Worker 	FILE	*f;
665*6a54128fSAndroid Build Coastguard Worker 	char	buf[80], *cp, *t;
666*6a54128fSAndroid Build Coastguard Worker 
667*6a54128fSAndroid Build Coastguard Worker 	f = fopen("/proc/filesystems", "r");
668*6a54128fSAndroid Build Coastguard Worker 	if (!f)
669*6a54128fSAndroid Build Coastguard Worker 		return (0);
670*6a54128fSAndroid Build Coastguard Worker 	while (!feof(f)) {
671*6a54128fSAndroid Build Coastguard Worker 		if (!fgets(buf, sizeof(buf), f))
672*6a54128fSAndroid Build Coastguard Worker 			break;
673*6a54128fSAndroid Build Coastguard Worker 		cp = buf;
674*6a54128fSAndroid Build Coastguard Worker 		if (!isspace(*cp)) {
675*6a54128fSAndroid Build Coastguard Worker 			while (*cp && !isspace(*cp))
676*6a54128fSAndroid Build Coastguard Worker 				cp++;
677*6a54128fSAndroid Build Coastguard Worker 		}
678*6a54128fSAndroid Build Coastguard Worker 		while (*cp && isspace(*cp))
679*6a54128fSAndroid Build Coastguard Worker 			cp++;
680*6a54128fSAndroid Build Coastguard Worker 		if ((t = strchr(cp, '\n')) != NULL)
681*6a54128fSAndroid Build Coastguard Worker 			*t = 0;
682*6a54128fSAndroid Build Coastguard Worker 		if ((t = strchr(cp, '\t')) != NULL)
683*6a54128fSAndroid Build Coastguard Worker 			*t = 0;
684*6a54128fSAndroid Build Coastguard Worker 		if ((t = strchr(cp, ' ')) != NULL)
685*6a54128fSAndroid Build Coastguard Worker 			*t = 0;
686*6a54128fSAndroid Build Coastguard Worker 		if (!strcmp(fs_name, cp)) {
687*6a54128fSAndroid Build Coastguard Worker 			fclose(f);
688*6a54128fSAndroid Build Coastguard Worker 			return (1);
689*6a54128fSAndroid Build Coastguard Worker 		}
690*6a54128fSAndroid Build Coastguard Worker 	}
691*6a54128fSAndroid Build Coastguard Worker 	fclose(f);
692*6a54128fSAndroid Build Coastguard Worker 	return (0);
693*6a54128fSAndroid Build Coastguard Worker }
694*6a54128fSAndroid Build Coastguard Worker 
695*6a54128fSAndroid Build Coastguard Worker /*
696*6a54128fSAndroid Build Coastguard Worker  * Check to see if a filesystem is available as a module
697*6a54128fSAndroid Build Coastguard Worker  * Returns 1 if found, 0 if not
698*6a54128fSAndroid Build Coastguard Worker  */
check_for_modules(const char * fs_name)699*6a54128fSAndroid Build Coastguard Worker int check_for_modules(const char *fs_name)
700*6a54128fSAndroid Build Coastguard Worker {
701*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
702*6a54128fSAndroid Build Coastguard Worker 	struct utsname	uts;
703*6a54128fSAndroid Build Coastguard Worker 	FILE		*f;
704*6a54128fSAndroid Build Coastguard Worker 	char		buf[1024], *cp, *t;
705*6a54128fSAndroid Build Coastguard Worker 	int		i;
706*6a54128fSAndroid Build Coastguard Worker 
707*6a54128fSAndroid Build Coastguard Worker 	if (uname(&uts))
708*6a54128fSAndroid Build Coastguard Worker 		return (0);
709*6a54128fSAndroid Build Coastguard Worker 	snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
710*6a54128fSAndroid Build Coastguard Worker 
711*6a54128fSAndroid Build Coastguard Worker 	f = fopen(buf, "r");
712*6a54128fSAndroid Build Coastguard Worker 	if (!f)
713*6a54128fSAndroid Build Coastguard Worker 		return (0);
714*6a54128fSAndroid Build Coastguard Worker 	while (!feof(f)) {
715*6a54128fSAndroid Build Coastguard Worker 		if (!fgets(buf, sizeof(buf), f))
716*6a54128fSAndroid Build Coastguard Worker 			break;
717*6a54128fSAndroid Build Coastguard Worker 		if ((cp = strchr(buf, ':')) != NULL)
718*6a54128fSAndroid Build Coastguard Worker 			*cp = 0;
719*6a54128fSAndroid Build Coastguard Worker 		else
720*6a54128fSAndroid Build Coastguard Worker 			continue;
721*6a54128fSAndroid Build Coastguard Worker 		if ((cp = strrchr(buf, '/')) != NULL)
722*6a54128fSAndroid Build Coastguard Worker 			cp++;
723*6a54128fSAndroid Build Coastguard Worker 		else
724*6a54128fSAndroid Build Coastguard Worker 			cp = buf;
725*6a54128fSAndroid Build Coastguard Worker 		i = strlen(cp);
726*6a54128fSAndroid Build Coastguard Worker 		if (i > 3) {
727*6a54128fSAndroid Build Coastguard Worker 			t = cp + i - 3;
728*6a54128fSAndroid Build Coastguard Worker 			if (!strcmp(t, ".ko"))
729*6a54128fSAndroid Build Coastguard Worker 				*t = 0;
730*6a54128fSAndroid Build Coastguard Worker 		}
731*6a54128fSAndroid Build Coastguard Worker 		if (!strcmp(cp, fs_name)) {
732*6a54128fSAndroid Build Coastguard Worker 			fclose(f);
733*6a54128fSAndroid Build Coastguard Worker 			return (1);
734*6a54128fSAndroid Build Coastguard Worker 		}
735*6a54128fSAndroid Build Coastguard Worker 	}
736*6a54128fSAndroid Build Coastguard Worker 	fclose(f);
737*6a54128fSAndroid Build Coastguard Worker #endif /* __linux__ */
738*6a54128fSAndroid Build Coastguard Worker 	return (0);
739*6a54128fSAndroid Build Coastguard Worker }
740*6a54128fSAndroid Build Coastguard Worker 
741*6a54128fSAndroid Build Coastguard Worker /*
742*6a54128fSAndroid Build Coastguard Worker  * Helper function that does the right thing if write returns a
743*6a54128fSAndroid Build Coastguard Worker  * partial write, or an EAGAIN/EINTR error.
744*6a54128fSAndroid Build Coastguard Worker  */
write_all(int fd,char * buf,size_t count)745*6a54128fSAndroid Build Coastguard Worker int write_all(int fd, char *buf, size_t count)
746*6a54128fSAndroid Build Coastguard Worker {
747*6a54128fSAndroid Build Coastguard Worker 	ssize_t ret;
748*6a54128fSAndroid Build Coastguard Worker 	int c = 0;
749*6a54128fSAndroid Build Coastguard Worker 
750*6a54128fSAndroid Build Coastguard Worker 	while (count > 0) {
751*6a54128fSAndroid Build Coastguard Worker 		ret = write(fd, buf, count);
752*6a54128fSAndroid Build Coastguard Worker 		if (ret < 0) {
753*6a54128fSAndroid Build Coastguard Worker 			if ((errno == EAGAIN) || (errno == EINTR))
754*6a54128fSAndroid Build Coastguard Worker 				continue;
755*6a54128fSAndroid Build Coastguard Worker 			return -1;
756*6a54128fSAndroid Build Coastguard Worker 		}
757*6a54128fSAndroid Build Coastguard Worker 		count -= ret;
758*6a54128fSAndroid Build Coastguard Worker 		buf += ret;
759*6a54128fSAndroid Build Coastguard Worker 		c += ret;
760*6a54128fSAndroid Build Coastguard Worker 	}
761*6a54128fSAndroid Build Coastguard Worker 	return c;
762*6a54128fSAndroid Build Coastguard Worker }
763*6a54128fSAndroid Build Coastguard Worker 
dump_mmp_msg(struct mmp_struct * mmp,const char * fmt,...)764*6a54128fSAndroid Build Coastguard Worker void dump_mmp_msg(struct mmp_struct *mmp, const char *fmt, ...)
765*6a54128fSAndroid Build Coastguard Worker {
766*6a54128fSAndroid Build Coastguard Worker 	va_list pvar;
767*6a54128fSAndroid Build Coastguard Worker 
768*6a54128fSAndroid Build Coastguard Worker 	if (fmt) {
769*6a54128fSAndroid Build Coastguard Worker 		printf("MMP check failed: ");
770*6a54128fSAndroid Build Coastguard Worker 		va_start(pvar, fmt);
771*6a54128fSAndroid Build Coastguard Worker 		vprintf(fmt, pvar);
772*6a54128fSAndroid Build Coastguard Worker 		va_end(pvar);
773*6a54128fSAndroid Build Coastguard Worker 	}
774*6a54128fSAndroid Build Coastguard Worker 	if (mmp) {
775*6a54128fSAndroid Build Coastguard Worker 		time_t t = mmp->mmp_time;
776*6a54128fSAndroid Build Coastguard Worker 
777*6a54128fSAndroid Build Coastguard Worker 		printf("MMP_block:\n");
778*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_magic: 0x%x\n", mmp->mmp_magic);
779*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_check_interval: %d\n",
780*6a54128fSAndroid Build Coastguard Worker 		       mmp->mmp_check_interval);
781*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_sequence: %08x\n", mmp->mmp_seq);
782*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_update_date: %s", ctime(&t));
783*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_update_time: %lld\n",
784*6a54128fSAndroid Build Coastguard Worker 		       (long long) mmp->mmp_time);
785*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_node_name: %.*s\n",
786*6a54128fSAndroid Build Coastguard Worker 		       EXT2_LEN_STR(mmp->mmp_nodename));
787*6a54128fSAndroid Build Coastguard Worker 		printf("    mmp_device_name: %.*s\n",
788*6a54128fSAndroid Build Coastguard Worker 		       EXT2_LEN_STR(mmp->mmp_bdevname));
789*6a54128fSAndroid Build Coastguard Worker 	}
790*6a54128fSAndroid Build Coastguard Worker }
791*6a54128fSAndroid Build Coastguard Worker 
e2fsck_mmp_update(ext2_filsys fs)792*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_mmp_update(ext2_filsys fs)
793*6a54128fSAndroid Build Coastguard Worker {
794*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
795*6a54128fSAndroid Build Coastguard Worker 
796*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_mmp_update(fs);
797*6a54128fSAndroid Build Coastguard Worker 	if (retval == EXT2_ET_MMP_CHANGE_ABORT)
798*6a54128fSAndroid Build Coastguard Worker 		dump_mmp_msg(fs->mmp_cmp,
799*6a54128fSAndroid Build Coastguard Worker 			     _("UNEXPECTED INCONSISTENCY: the filesystem is "
800*6a54128fSAndroid Build Coastguard Worker 			       "being modified while fsck is running.\n"));
801*6a54128fSAndroid Build Coastguard Worker 
802*6a54128fSAndroid Build Coastguard Worker 	return retval;
803*6a54128fSAndroid Build Coastguard Worker }
804*6a54128fSAndroid Build Coastguard Worker 
e2fsck_set_bitmap_type(ext2_filsys fs,unsigned int default_type,const char * profile_name,unsigned int * old_type)805*6a54128fSAndroid Build Coastguard Worker void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
806*6a54128fSAndroid Build Coastguard Worker 			    const char *profile_name, unsigned int *old_type)
807*6a54128fSAndroid Build Coastguard Worker {
808*6a54128fSAndroid Build Coastguard Worker 	unsigned type;
809*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
810*6a54128fSAndroid Build Coastguard Worker 
811*6a54128fSAndroid Build Coastguard Worker 	if (old_type)
812*6a54128fSAndroid Build Coastguard Worker 		*old_type = fs->default_bitmap_type;
813*6a54128fSAndroid Build Coastguard Worker 	profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
814*6a54128fSAndroid Build Coastguard Worker 			 default_type, &type);
815*6a54128fSAndroid Build Coastguard Worker 	profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
816*6a54128fSAndroid Build Coastguard Worker 	fs->default_bitmap_type = type ? type : default_type;
817*6a54128fSAndroid Build Coastguard Worker }
818*6a54128fSAndroid Build Coastguard Worker 
e2fsck_allocate_inode_bitmap(ext2_filsys fs,const char * descr,int deftype,const char * name,ext2fs_inode_bitmap * ret)819*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
820*6a54128fSAndroid Build Coastguard Worker 				       int deftype,
821*6a54128fSAndroid Build Coastguard Worker 				       const char *name,
822*6a54128fSAndroid Build Coastguard Worker 				       ext2fs_inode_bitmap *ret)
823*6a54128fSAndroid Build Coastguard Worker {
824*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
825*6a54128fSAndroid Build Coastguard Worker 	unsigned int	save_type;
826*6a54128fSAndroid Build Coastguard Worker 
827*6a54128fSAndroid Build Coastguard Worker 	e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
828*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
829*6a54128fSAndroid Build Coastguard Worker 	fs->default_bitmap_type = save_type;
830*6a54128fSAndroid Build Coastguard Worker 	return retval;
831*6a54128fSAndroid Build Coastguard Worker }
832*6a54128fSAndroid Build Coastguard Worker 
e2fsck_allocate_block_bitmap(ext2_filsys fs,const char * descr,int deftype,const char * name,ext2fs_block_bitmap * ret)833*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
834*6a54128fSAndroid Build Coastguard Worker 				       int deftype,
835*6a54128fSAndroid Build Coastguard Worker 				       const char *name,
836*6a54128fSAndroid Build Coastguard Worker 				       ext2fs_block_bitmap *ret)
837*6a54128fSAndroid Build Coastguard Worker {
838*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
839*6a54128fSAndroid Build Coastguard Worker 	unsigned int	save_type;
840*6a54128fSAndroid Build Coastguard Worker 
841*6a54128fSAndroid Build Coastguard Worker 	e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
842*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
843*6a54128fSAndroid Build Coastguard Worker 	fs->default_bitmap_type = save_type;
844*6a54128fSAndroid Build Coastguard Worker 	return retval;
845*6a54128fSAndroid Build Coastguard Worker }
846*6a54128fSAndroid Build Coastguard Worker 
e2fsck_allocate_subcluster_bitmap(ext2_filsys fs,const char * descr,int deftype,const char * name,ext2fs_block_bitmap * ret)847*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
848*6a54128fSAndroid Build Coastguard Worker 					    int deftype,
849*6a54128fSAndroid Build Coastguard Worker 					    const char *name,
850*6a54128fSAndroid Build Coastguard Worker 					    ext2fs_block_bitmap *ret)
851*6a54128fSAndroid Build Coastguard Worker {
852*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
853*6a54128fSAndroid Build Coastguard Worker 	unsigned int	save_type;
854*6a54128fSAndroid Build Coastguard Worker 
855*6a54128fSAndroid Build Coastguard Worker 	e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
856*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
857*6a54128fSAndroid Build Coastguard Worker 	fs->default_bitmap_type = save_type;
858*6a54128fSAndroid Build Coastguard Worker 	return retval;
859*6a54128fSAndroid Build Coastguard Worker }
860*6a54128fSAndroid Build Coastguard Worker 
861*6a54128fSAndroid Build Coastguard Worker /* Return memory size in bytes */
get_memory_size(void)862*6a54128fSAndroid Build Coastguard Worker unsigned long long get_memory_size(void)
863*6a54128fSAndroid Build Coastguard Worker {
864*6a54128fSAndroid Build Coastguard Worker #if defined(_SC_PHYS_PAGES)
865*6a54128fSAndroid Build Coastguard Worker # if defined(_SC_PAGESIZE)
866*6a54128fSAndroid Build Coastguard Worker 	return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
867*6a54128fSAndroid Build Coastguard Worker 	       (unsigned long long)sysconf(_SC_PAGESIZE);
868*6a54128fSAndroid Build Coastguard Worker # elif defined(_SC_PAGE_SIZE)
869*6a54128fSAndroid Build Coastguard Worker 	return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
870*6a54128fSAndroid Build Coastguard Worker 	       (unsigned long long)sysconf(_SC_PAGE_SIZE);
871*6a54128fSAndroid Build Coastguard Worker # endif
872*6a54128fSAndroid Build Coastguard Worker #elif defined(CTL_HW)
873*6a54128fSAndroid Build Coastguard Worker # if (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
874*6a54128fSAndroid Build Coastguard Worker #  define CTL_HW_INT64
875*6a54128fSAndroid Build Coastguard Worker # elif (defined(HW_PHYSMEM) || defined(HW_REALMEM))
876*6a54128fSAndroid Build Coastguard Worker #  define CTL_HW_UINT
877*6a54128fSAndroid Build Coastguard Worker # endif
878*6a54128fSAndroid Build Coastguard Worker 	int mib[2];
879*6a54128fSAndroid Build Coastguard Worker 
880*6a54128fSAndroid Build Coastguard Worker 	mib[0] = CTL_HW;
881*6a54128fSAndroid Build Coastguard Worker # if defined(HW_MEMSIZE)
882*6a54128fSAndroid Build Coastguard Worker 	mib[1] = HW_MEMSIZE;
883*6a54128fSAndroid Build Coastguard Worker # elif defined(HW_PHYSMEM64)
884*6a54128fSAndroid Build Coastguard Worker 	mib[1] = HW_PHYSMEM64;
885*6a54128fSAndroid Build Coastguard Worker # elif defined(HW_REALMEM)
886*6a54128fSAndroid Build Coastguard Worker 	mib[1] = HW_REALMEM;
887*6a54128fSAndroid Build Coastguard Worker # elif defined(HW_PYSMEM)
888*6a54128fSAndroid Build Coastguard Worker 	mib[1] = HW_PHYSMEM;
889*6a54128fSAndroid Build Coastguard Worker # endif
890*6a54128fSAndroid Build Coastguard Worker # if defined(CTL_HW_INT64)
891*6a54128fSAndroid Build Coastguard Worker 	unsigned long long size = 0;
892*6a54128fSAndroid Build Coastguard Worker # elif defined(CTL_HW_UINT)
893*6a54128fSAndroid Build Coastguard Worker 	unsigned int size = 0;
894*6a54128fSAndroid Build Coastguard Worker # endif
895*6a54128fSAndroid Build Coastguard Worker 	return 0;
896*6a54128fSAndroid Build Coastguard Worker #else
897*6a54128fSAndroid Build Coastguard Worker # warning "Don't know how to detect memory on your platform?"
898*6a54128fSAndroid Build Coastguard Worker 	return 0;
899*6a54128fSAndroid Build Coastguard Worker #endif
900*6a54128fSAndroid Build Coastguard Worker }
901