xref: /aosp_15_r20/external/e2fsprogs/e2fsck/e2fsck.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * e2fsck.c - a consistency checker for the new extended file system.
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 <errno.h>
14*6a54128fSAndroid Build Coastguard Worker 
15*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
16*6a54128fSAndroid Build Coastguard Worker #include "problem.h"
17*6a54128fSAndroid Build Coastguard Worker 
18*6a54128fSAndroid Build Coastguard Worker /*
19*6a54128fSAndroid Build Coastguard Worker  * This function allocates an e2fsck context
20*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_allocate_context(e2fsck_t * ret)21*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_allocate_context(e2fsck_t *ret)
22*6a54128fSAndroid Build Coastguard Worker {
23*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t	context;
24*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
25*6a54128fSAndroid Build Coastguard Worker 	char		*time_env;
26*6a54128fSAndroid Build Coastguard Worker 
27*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &context);
28*6a54128fSAndroid Build Coastguard Worker 	if (retval)
29*6a54128fSAndroid Build Coastguard Worker 		return retval;
30*6a54128fSAndroid Build Coastguard Worker 
31*6a54128fSAndroid Build Coastguard Worker 	memset(context, 0, sizeof(struct e2fsck_struct));
32*6a54128fSAndroid Build Coastguard Worker 
33*6a54128fSAndroid Build Coastguard Worker 	context->process_inode_size = 256;
34*6a54128fSAndroid Build Coastguard Worker 	context->ext_attr_ver = 2;
35*6a54128fSAndroid Build Coastguard Worker 	context->blocks_per_page = 1;
36*6a54128fSAndroid Build Coastguard Worker 	context->htree_slack_percentage = 255;
37*6a54128fSAndroid Build Coastguard Worker 
38*6a54128fSAndroid Build Coastguard Worker 	time_env = getenv("E2FSCK_TIME");
39*6a54128fSAndroid Build Coastguard Worker 	if (time_env)
40*6a54128fSAndroid Build Coastguard Worker 		context->now = (time_t) strtoull(time_env, NULL, 0);
41*6a54128fSAndroid Build Coastguard Worker 	else {
42*6a54128fSAndroid Build Coastguard Worker 		context->now = time(0);
43*6a54128fSAndroid Build Coastguard Worker 		if (context->now < 1262322000) /* January 1 2010 */
44*6a54128fSAndroid Build Coastguard Worker 			context->flags |= E2F_FLAG_TIME_INSANE;
45*6a54128fSAndroid Build Coastguard Worker 	}
46*6a54128fSAndroid Build Coastguard Worker 
47*6a54128fSAndroid Build Coastguard Worker 	*ret = context;
48*6a54128fSAndroid Build Coastguard Worker 	return 0;
49*6a54128fSAndroid Build Coastguard Worker }
50*6a54128fSAndroid Build Coastguard Worker 
51*6a54128fSAndroid Build Coastguard Worker /*
52*6a54128fSAndroid Build Coastguard Worker  * This function resets an e2fsck context; it is called when e2fsck
53*6a54128fSAndroid Build Coastguard Worker  * needs to be restarted.
54*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_reset_context(e2fsck_t ctx)55*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_reset_context(e2fsck_t ctx)
56*6a54128fSAndroid Build Coastguard Worker {
57*6a54128fSAndroid Build Coastguard Worker 	int	i;
58*6a54128fSAndroid Build Coastguard Worker 
59*6a54128fSAndroid Build Coastguard Worker 	ctx->flags &= E2F_RESET_FLAGS;
60*6a54128fSAndroid Build Coastguard Worker 	ctx->lost_and_found = 0;
61*6a54128fSAndroid Build Coastguard Worker 	ctx->bad_lost_and_found = 0;
62*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_used_map) {
63*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_used_map);
64*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_used_map = 0;
65*6a54128fSAndroid Build Coastguard Worker 	}
66*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_dir_map) {
67*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_dir_map);
68*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_dir_map = 0;
69*6a54128fSAndroid Build Coastguard Worker 	}
70*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_reg_map) {
71*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_reg_map);
72*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_reg_map = 0;
73*6a54128fSAndroid Build Coastguard Worker 	}
74*6a54128fSAndroid Build Coastguard Worker 	if (ctx->block_found_map) {
75*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_block_bitmap(ctx->block_found_map);
76*6a54128fSAndroid Build Coastguard Worker 		ctx->block_found_map = 0;
77*6a54128fSAndroid Build Coastguard Worker 	}
78*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_casefold_map) {
79*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_block_bitmap(ctx->inode_casefold_map);
80*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_casefold_map = 0;
81*6a54128fSAndroid Build Coastguard Worker 	}
82*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_link_info) {
83*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_icount(ctx->inode_link_info);
84*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_link_info = 0;
85*6a54128fSAndroid Build Coastguard Worker 	}
86*6a54128fSAndroid Build Coastguard Worker 	if (ctx->journal_io) {
87*6a54128fSAndroid Build Coastguard Worker 		if (ctx->fs && ctx->fs->io != ctx->journal_io)
88*6a54128fSAndroid Build Coastguard Worker 			io_channel_close(ctx->journal_io);
89*6a54128fSAndroid Build Coastguard Worker 		ctx->journal_io = 0;
90*6a54128fSAndroid Build Coastguard Worker 	}
91*6a54128fSAndroid Build Coastguard Worker 	if (ctx->fs && ctx->fs->dblist) {
92*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_dblist(ctx->fs->dblist);
93*6a54128fSAndroid Build Coastguard Worker 		ctx->fs->dblist = 0;
94*6a54128fSAndroid Build Coastguard Worker 	}
95*6a54128fSAndroid Build Coastguard Worker 	e2fsck_free_dir_info(ctx);
96*6a54128fSAndroid Build Coastguard Worker 	e2fsck_free_dx_dir_info(ctx);
97*6a54128fSAndroid Build Coastguard Worker 	if (ctx->refcount) {
98*6a54128fSAndroid Build Coastguard Worker 		ea_refcount_free(ctx->refcount);
99*6a54128fSAndroid Build Coastguard Worker 		ctx->refcount = 0;
100*6a54128fSAndroid Build Coastguard Worker 	}
101*6a54128fSAndroid Build Coastguard Worker 	if (ctx->refcount_extra) {
102*6a54128fSAndroid Build Coastguard Worker 		ea_refcount_free(ctx->refcount_extra);
103*6a54128fSAndroid Build Coastguard Worker 		ctx->refcount_extra = 0;
104*6a54128fSAndroid Build Coastguard Worker 	}
105*6a54128fSAndroid Build Coastguard Worker 	if (ctx->ea_block_quota_blocks) {
106*6a54128fSAndroid Build Coastguard Worker 		ea_refcount_free(ctx->ea_block_quota_blocks);
107*6a54128fSAndroid Build Coastguard Worker 		ctx->ea_block_quota_blocks = 0;
108*6a54128fSAndroid Build Coastguard Worker 	}
109*6a54128fSAndroid Build Coastguard Worker 	if (ctx->ea_block_quota_inodes) {
110*6a54128fSAndroid Build Coastguard Worker 		ea_refcount_free(ctx->ea_block_quota_inodes);
111*6a54128fSAndroid Build Coastguard Worker 		ctx->ea_block_quota_inodes = 0;
112*6a54128fSAndroid Build Coastguard Worker 	}
113*6a54128fSAndroid Build Coastguard Worker 	if (ctx->ea_inode_refs) {
114*6a54128fSAndroid Build Coastguard Worker 		ea_refcount_free(ctx->ea_inode_refs);
115*6a54128fSAndroid Build Coastguard Worker 		ctx->ea_inode_refs = 0;
116*6a54128fSAndroid Build Coastguard Worker 	}
117*6a54128fSAndroid Build Coastguard Worker 	if (ctx->block_dup_map) {
118*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_block_bitmap(ctx->block_dup_map);
119*6a54128fSAndroid Build Coastguard Worker 		ctx->block_dup_map = 0;
120*6a54128fSAndroid Build Coastguard Worker 	}
121*6a54128fSAndroid Build Coastguard Worker 	if (ctx->block_ea_map) {
122*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_block_bitmap(ctx->block_ea_map);
123*6a54128fSAndroid Build Coastguard Worker 		ctx->block_ea_map = 0;
124*6a54128fSAndroid Build Coastguard Worker 	}
125*6a54128fSAndroid Build Coastguard Worker 	if (ctx->block_metadata_map) {
126*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_block_bitmap(ctx->block_metadata_map);
127*6a54128fSAndroid Build Coastguard Worker 		ctx->block_metadata_map = 0;
128*6a54128fSAndroid Build Coastguard Worker 	}
129*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_bb_map) {
130*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_bb_map);
131*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_bb_map = 0;
132*6a54128fSAndroid Build Coastguard Worker 	}
133*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_bad_map) {
134*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
135*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_bad_map = 0;
136*6a54128fSAndroid Build Coastguard Worker 	}
137*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_imagic_map) {
138*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
139*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_imagic_map = 0;
140*6a54128fSAndroid Build Coastguard Worker 	}
141*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dirs_to_hash) {
142*6a54128fSAndroid Build Coastguard Worker 		ext2fs_u32_list_free(ctx->dirs_to_hash);
143*6a54128fSAndroid Build Coastguard Worker 		ctx->dirs_to_hash = 0;
144*6a54128fSAndroid Build Coastguard Worker 	}
145*6a54128fSAndroid Build Coastguard Worker 	destroy_encrypted_file_info(ctx);
146*6a54128fSAndroid Build Coastguard Worker 
147*6a54128fSAndroid Build Coastguard Worker 	/*
148*6a54128fSAndroid Build Coastguard Worker 	 * Clear the array of invalid meta-data flags
149*6a54128fSAndroid Build Coastguard Worker 	 */
150*6a54128fSAndroid Build Coastguard Worker 	if (ctx->invalid_inode_bitmap_flag) {
151*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag);
152*6a54128fSAndroid Build Coastguard Worker 		ctx->invalid_inode_bitmap_flag = 0;
153*6a54128fSAndroid Build Coastguard Worker 	}
154*6a54128fSAndroid Build Coastguard Worker 	if (ctx->invalid_block_bitmap_flag) {
155*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ctx->invalid_block_bitmap_flag);
156*6a54128fSAndroid Build Coastguard Worker 		ctx->invalid_block_bitmap_flag = 0;
157*6a54128fSAndroid Build Coastguard Worker 	}
158*6a54128fSAndroid Build Coastguard Worker 	if (ctx->invalid_inode_table_flag) {
159*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ctx->invalid_inode_table_flag);
160*6a54128fSAndroid Build Coastguard Worker 		ctx->invalid_inode_table_flag = 0;
161*6a54128fSAndroid Build Coastguard Worker 	}
162*6a54128fSAndroid Build Coastguard Worker 	if (ctx->casefolded_dirs) {
163*6a54128fSAndroid Build Coastguard Worker 		ext2fs_u32_list_free(ctx->casefolded_dirs);
164*6a54128fSAndroid Build Coastguard Worker 		ctx->casefolded_dirs = 0;
165*6a54128fSAndroid Build Coastguard Worker 	}
166*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_count) {
167*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_icount(ctx->inode_count);
168*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_count = 0;
169*6a54128fSAndroid Build Coastguard Worker 	}
170*6a54128fSAndroid Build Coastguard Worker 
171*6a54128fSAndroid Build Coastguard Worker 	/* Clear statistic counters */
172*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_directory_count = 0;
173*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_regular_count = 0;
174*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_blockdev_count = 0;
175*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_chardev_count = 0;
176*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_links_count = 0;
177*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_symlinks_count = 0;
178*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_fast_symlinks_count = 0;
179*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_fifo_count = 0;
180*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_total_count = 0;
181*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_badblocks_count = 0;
182*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_sockets_count = 0;
183*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_ind_count = 0;
184*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_dind_count = 0;
185*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_tind_count = 0;
186*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_fragmented = 0;
187*6a54128fSAndroid Build Coastguard Worker 	ctx->fs_fragmented_dir = 0;
188*6a54128fSAndroid Build Coastguard Worker 	ctx->large_files = 0;
189*6a54128fSAndroid Build Coastguard Worker 	ctx->large_dirs = 0;
190*6a54128fSAndroid Build Coastguard Worker 
191*6a54128fSAndroid Build Coastguard Worker 	for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
192*6a54128fSAndroid Build Coastguard Worker 		ctx->extent_depth_count[i] = 0;
193*6a54128fSAndroid Build Coastguard Worker 
194*6a54128fSAndroid Build Coastguard Worker 	/* Reset the superblock to the user's requested value */
195*6a54128fSAndroid Build Coastguard Worker 	ctx->superblock = ctx->use_superblock;
196*6a54128fSAndroid Build Coastguard Worker 
197*6a54128fSAndroid Build Coastguard Worker 	return 0;
198*6a54128fSAndroid Build Coastguard Worker }
199*6a54128fSAndroid Build Coastguard Worker 
e2fsck_free_context(e2fsck_t ctx)200*6a54128fSAndroid Build Coastguard Worker void e2fsck_free_context(e2fsck_t ctx)
201*6a54128fSAndroid Build Coastguard Worker {
202*6a54128fSAndroid Build Coastguard Worker 	if (!ctx)
203*6a54128fSAndroid Build Coastguard Worker 		return;
204*6a54128fSAndroid Build Coastguard Worker 
205*6a54128fSAndroid Build Coastguard Worker 	e2fsck_reset_context(ctx);
206*6a54128fSAndroid Build Coastguard Worker 	if (ctx->blkid)
207*6a54128fSAndroid Build Coastguard Worker 		blkid_put_cache(ctx->blkid);
208*6a54128fSAndroid Build Coastguard Worker 
209*6a54128fSAndroid Build Coastguard Worker 	if (ctx->profile)
210*6a54128fSAndroid Build Coastguard Worker 		profile_release(ctx->profile);
211*6a54128fSAndroid Build Coastguard Worker 
212*6a54128fSAndroid Build Coastguard Worker 	if (ctx->filesystem_name)
213*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ctx->filesystem_name);
214*6a54128fSAndroid Build Coastguard Worker 
215*6a54128fSAndroid Build Coastguard Worker 	if (ctx->device_name)
216*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ctx->device_name);
217*6a54128fSAndroid Build Coastguard Worker 
218*6a54128fSAndroid Build Coastguard Worker 	if (ctx->log_fn)
219*6a54128fSAndroid Build Coastguard Worker 		free(ctx->log_fn);
220*6a54128fSAndroid Build Coastguard Worker 
221*6a54128fSAndroid Build Coastguard Worker 	if (ctx->logf)
222*6a54128fSAndroid Build Coastguard Worker 		fclose(ctx->logf);
223*6a54128fSAndroid Build Coastguard Worker 
224*6a54128fSAndroid Build Coastguard Worker 	if (ctx->problem_log_fn)
225*6a54128fSAndroid Build Coastguard Worker 		free(ctx->problem_log_fn);
226*6a54128fSAndroid Build Coastguard Worker 
227*6a54128fSAndroid Build Coastguard Worker 	if (ctx->problem_logf) {
228*6a54128fSAndroid Build Coastguard Worker 		fputs("</problem_log>\n", ctx->problem_logf);
229*6a54128fSAndroid Build Coastguard Worker 		fclose(ctx->problem_logf);
230*6a54128fSAndroid Build Coastguard Worker 	}
231*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&ctx);
232*6a54128fSAndroid Build Coastguard Worker }
233*6a54128fSAndroid Build Coastguard Worker 
234*6a54128fSAndroid Build Coastguard Worker /*
235*6a54128fSAndroid Build Coastguard Worker  * This function runs through the e2fsck passes and calls them all,
236*6a54128fSAndroid Build Coastguard Worker  * returning restart, abort, or cancel as necessary...
237*6a54128fSAndroid Build Coastguard Worker  */
238*6a54128fSAndroid Build Coastguard Worker typedef void (*pass_t)(e2fsck_t ctx);
239*6a54128fSAndroid Build Coastguard Worker 
240*6a54128fSAndroid Build Coastguard Worker static pass_t e2fsck_passes[] = {
241*6a54128fSAndroid Build Coastguard Worker 	e2fsck_pass1, e2fsck_pass1e, e2fsck_pass2, e2fsck_pass3,
242*6a54128fSAndroid Build Coastguard Worker 	e2fsck_pass4, e2fsck_pass5, 0 };
243*6a54128fSAndroid Build Coastguard Worker 
e2fsck_run(e2fsck_t ctx)244*6a54128fSAndroid Build Coastguard Worker int e2fsck_run(e2fsck_t ctx)
245*6a54128fSAndroid Build Coastguard Worker {
246*6a54128fSAndroid Build Coastguard Worker 	int	i;
247*6a54128fSAndroid Build Coastguard Worker 	pass_t	e2fsck_pass;
248*6a54128fSAndroid Build Coastguard Worker 
249*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SETJMP_H
250*6a54128fSAndroid Build Coastguard Worker 	if (setjmp(ctx->abort_loc)) {
251*6a54128fSAndroid Build Coastguard Worker 		ctx->flags &= ~E2F_FLAG_SETJMP_OK;
252*6a54128fSAndroid Build Coastguard Worker 		return (ctx->flags & E2F_FLAG_RUN_RETURN);
253*6a54128fSAndroid Build Coastguard Worker 	}
254*6a54128fSAndroid Build Coastguard Worker 	ctx->flags |= E2F_FLAG_SETJMP_OK;
255*6a54128fSAndroid Build Coastguard Worker #endif
256*6a54128fSAndroid Build Coastguard Worker 
257*6a54128fSAndroid Build Coastguard Worker 	for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
258*6a54128fSAndroid Build Coastguard Worker 		if (ctx->flags & E2F_FLAG_RUN_RETURN)
259*6a54128fSAndroid Build Coastguard Worker 			break;
260*6a54128fSAndroid Build Coastguard Worker 		if (e2fsck_mmp_update(ctx->fs))
261*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
262*6a54128fSAndroid Build Coastguard Worker 		e2fsck_pass(ctx);
263*6a54128fSAndroid Build Coastguard Worker 		if (ctx->progress)
264*6a54128fSAndroid Build Coastguard Worker 			(void) (ctx->progress)(ctx, 0, 0, 0);
265*6a54128fSAndroid Build Coastguard Worker 	}
266*6a54128fSAndroid Build Coastguard Worker 	ctx->flags &= ~E2F_FLAG_SETJMP_OK;
267*6a54128fSAndroid Build Coastguard Worker 
268*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_RUN_RETURN)
269*6a54128fSAndroid Build Coastguard Worker 		return (ctx->flags & E2F_FLAG_RUN_RETURN);
270*6a54128fSAndroid Build Coastguard Worker 	return 0;
271*6a54128fSAndroid Build Coastguard Worker }
272