1/*
2 * e2fsck.c - a consistency checker for the new extended file system.
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <errno.h>
13
14#include "e2fsck.h"
15#include "problem.h"
16
17/*
18 * This function allocates an e2fsck context
19 */
20errcode_t e2fsck_allocate_context(e2fsck_t *ret)
21{
22	e2fsck_t	context;
23	errcode_t	retval;
24	char		*time_env;
25
26	retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &context);
27	if (retval)
28		return retval;
29
30	memset(context, 0, sizeof(struct e2fsck_struct));
31
32	context->process_inode_size = 256;
33	context->ext_attr_ver = 2;
34	context->blocks_per_page = 1;
35
36	time_env = getenv("E2FSCK_TIME");
37	if (time_env)
38		context->now = strtoul(time_env, NULL, 0);
39	else
40		context->now = time(0);
41
42	*ret = context;
43	return 0;
44}
45
46/*
47 * This function resets an e2fsck context; it is called when e2fsck
48 * needs to be restarted.
49 */
50errcode_t e2fsck_reset_context(e2fsck_t ctx)
51{
52	ctx->flags = 0;
53	ctx->lost_and_found = 0;
54	ctx->bad_lost_and_found = 0;
55	if (ctx->inode_used_map) {
56		ext2fs_free_inode_bitmap(ctx->inode_used_map);
57		ctx->inode_used_map = 0;
58	}
59	if (ctx->inode_dir_map) {
60		ext2fs_free_inode_bitmap(ctx->inode_dir_map);
61		ctx->inode_dir_map = 0;
62	}
63	if (ctx->inode_reg_map) {
64		ext2fs_free_inode_bitmap(ctx->inode_reg_map);
65		ctx->inode_reg_map = 0;
66	}
67	if (ctx->block_found_map) {
68		ext2fs_free_block_bitmap(ctx->block_found_map);
69		ctx->block_found_map = 0;
70	}
71	if (ctx->inode_link_info) {
72		ext2fs_free_icount(ctx->inode_link_info);
73		ctx->inode_link_info = 0;
74	}
75	if (ctx->journal_io) {
76		if (ctx->fs && ctx->fs->io != ctx->journal_io)
77			io_channel_close(ctx->journal_io);
78		ctx->journal_io = 0;
79	}
80	if (ctx->fs && ctx->fs->dblist) {
81		ext2fs_free_dblist(ctx->fs->dblist);
82		ctx->fs->dblist = 0;
83	}
84	e2fsck_free_dir_info(ctx);
85#ifdef ENABLE_HTREE
86	e2fsck_free_dx_dir_info(ctx);
87#endif
88	if (ctx->refcount) {
89		ea_refcount_free(ctx->refcount);
90		ctx->refcount = 0;
91	}
92	if (ctx->refcount_extra) {
93		ea_refcount_free(ctx->refcount_extra);
94		ctx->refcount_extra = 0;
95	}
96	if (ctx->block_dup_map) {
97		ext2fs_free_block_bitmap(ctx->block_dup_map);
98		ctx->block_dup_map = 0;
99	}
100	if (ctx->block_ea_map) {
101		ext2fs_free_block_bitmap(ctx->block_ea_map);
102		ctx->block_ea_map = 0;
103	}
104	if (ctx->inode_bb_map) {
105		ext2fs_free_inode_bitmap(ctx->inode_bb_map);
106		ctx->inode_bb_map = 0;
107	}
108	if (ctx->inode_bad_map) {
109		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
110		ctx->inode_bad_map = 0;
111	}
112	if (ctx->inode_imagic_map) {
113		ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
114		ctx->inode_imagic_map = 0;
115	}
116	if (ctx->dirs_to_hash) {
117		ext2fs_u32_list_free(ctx->dirs_to_hash);
118		ctx->dirs_to_hash = 0;
119	}
120
121	/*
122	 * Clear the array of invalid meta-data flags
123	 */
124	if (ctx->invalid_inode_bitmap_flag) {
125		ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag);
126		ctx->invalid_inode_bitmap_flag = 0;
127	}
128	if (ctx->invalid_block_bitmap_flag) {
129		ext2fs_free_mem(&ctx->invalid_block_bitmap_flag);
130		ctx->invalid_block_bitmap_flag = 0;
131	}
132	if (ctx->invalid_inode_table_flag) {
133		ext2fs_free_mem(&ctx->invalid_inode_table_flag);
134		ctx->invalid_inode_table_flag = 0;
135	}
136
137	/* Clear statistic counters */
138	ctx->fs_directory_count = 0;
139	ctx->fs_regular_count = 0;
140	ctx->fs_blockdev_count = 0;
141	ctx->fs_chardev_count = 0;
142	ctx->fs_links_count = 0;
143	ctx->fs_symlinks_count = 0;
144	ctx->fs_fast_symlinks_count = 0;
145	ctx->fs_fifo_count = 0;
146	ctx->fs_total_count = 0;
147	ctx->fs_badblocks_count = 0;
148	ctx->fs_sockets_count = 0;
149	ctx->fs_ind_count = 0;
150	ctx->fs_dind_count = 0;
151	ctx->fs_tind_count = 0;
152	ctx->fs_fragmented = 0;
153	ctx->large_files = 0;
154
155	/* Reset the superblock to the user's requested value */
156	ctx->superblock = ctx->use_superblock;
157
158	return 0;
159}
160
161void e2fsck_free_context(e2fsck_t ctx)
162{
163	if (!ctx)
164		return;
165
166	e2fsck_reset_context(ctx);
167	if (ctx->blkid)
168		blkid_put_cache(ctx->blkid);
169
170	if (ctx->profile)
171		profile_release(ctx->profile);
172
173	ext2fs_free_mem(&ctx);
174}
175
176/*
177 * This function runs through the e2fsck passes and calls them all,
178 * returning restart, abort, or cancel as necessary...
179 */
180typedef void (*pass_t)(e2fsck_t ctx);
181
182pass_t e2fsck_passes[] = {
183	e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
184	e2fsck_pass5, 0 };
185
186#define E2F_FLAG_RUN_RETURN	(E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
187
188int e2fsck_run(e2fsck_t ctx)
189{
190	int	i;
191	pass_t	e2fsck_pass;
192
193#ifdef HAVE_SETJMP_H
194	if (setjmp(ctx->abort_loc)) {
195		ctx->flags &= ~E2F_FLAG_SETJMP_OK;
196		return (ctx->flags & E2F_FLAG_RUN_RETURN);
197	}
198	ctx->flags |= E2F_FLAG_SETJMP_OK;
199#endif
200
201	for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
202		if (ctx->flags & E2F_FLAG_RUN_RETURN)
203			break;
204		e2fsck_pass(ctx);
205		if (ctx->progress)
206			(void) (ctx->progress)(ctx, 0, 0, 0);
207	}
208	ctx->flags &= ~E2F_FLAG_SETJMP_OK;
209
210	if (ctx->flags & E2F_FLAG_RUN_RETURN)
211		return (ctx->flags & E2F_FLAG_RUN_RETURN);
212	return 0;
213}
214