e2fsck.c revision 342d847db355d81299218e07a1e58ece82080a04
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
25	retval = ext2fs_get_mem(sizeof(struct e2fsck_struct),
26				(void **) &context);
27	if (retval)
28		return retval;
29
30	memset(context, 0, sizeof(struct e2fsck_struct));
31
32	context->process_inode_size = 256;
33
34	*ret = context;
35	return 0;
36}
37
38/*
39 * This function resets an e2fsck context; it is called when e2fsck
40 * needs to be restarted.
41 */
42errcode_t e2fsck_reset_context(e2fsck_t ctx)
43{
44	ctx->flags = 0;
45	if (ctx->inode_used_map) {
46		ext2fs_free_inode_bitmap(ctx->inode_used_map);
47		ctx->inode_used_map = 0;
48	}
49	if (ctx->inode_dir_map) {
50		ext2fs_free_inode_bitmap(ctx->inode_dir_map);
51		ctx->inode_dir_map = 0;
52	}
53	if (ctx->inode_reg_map) {
54		ext2fs_free_inode_bitmap(ctx->inode_reg_map);
55		ctx->inode_reg_map = 0;
56	}
57	if (ctx->block_found_map) {
58		ext2fs_free_block_bitmap(ctx->block_found_map);
59		ctx->block_found_map = 0;
60	}
61	if (ctx->inode_link_info) {
62		ext2fs_free_icount(ctx->inode_link_info);
63		ctx->inode_link_info = 0;
64	}
65	if (ctx->fs && ctx->fs->dblist) {
66		ext2fs_free_dblist(ctx->fs->dblist);
67		ctx->fs->dblist = 0;
68	}
69	e2fsck_free_dir_info(ctx);
70	if (ctx->refcount) {
71		ea_refcount_free(ctx->refcount);
72		ctx->refcount = 0;
73	}
74	if (ctx->refcount_extra) {
75		ea_refcount_free(ctx->refcount_extra);
76		ctx->refcount_extra = 0;
77	}
78	if (ctx->block_dup_map) {
79		ext2fs_free_block_bitmap(ctx->block_dup_map);
80		ctx->block_dup_map = 0;
81	}
82	if (ctx->block_ea_map) {
83		ext2fs_free_block_bitmap(ctx->block_ea_map);
84		ctx->block_ea_map = 0;
85	}
86	if (ctx->inode_bb_map) {
87		ext2fs_free_inode_bitmap(ctx->inode_bb_map);
88		ctx->inode_bb_map = 0;
89	}
90	if (ctx->inode_bad_map) {
91		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
92		ctx->inode_bad_map = 0;
93	}
94	if (ctx->inode_imagic_map) {
95		ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
96		ctx->inode_imagic_map = 0;
97	}
98
99	/*
100	 * Clear the array of invalid meta-data flags
101	 */
102	if (ctx->invalid_inode_bitmap_flag) {
103		ext2fs_free_mem((void **) &ctx->invalid_inode_bitmap_flag);
104		ctx->invalid_inode_bitmap_flag = 0;
105	}
106	if (ctx->invalid_block_bitmap_flag) {
107		ext2fs_free_mem((void **) &ctx->invalid_block_bitmap_flag);
108		ctx->invalid_block_bitmap_flag = 0;
109	}
110	if (ctx->invalid_inode_table_flag) {
111		ext2fs_free_mem((void **) &ctx->invalid_inode_table_flag);
112		ctx->invalid_inode_table_flag = 0;
113	}
114
115	/* Clear statistic counters */
116	ctx->fs_directory_count = 0;
117	ctx->fs_regular_count = 0;
118	ctx->fs_blockdev_count = 0;
119	ctx->fs_chardev_count = 0;
120	ctx->fs_links_count = 0;
121	ctx->fs_symlinks_count = 0;
122	ctx->fs_fast_symlinks_count = 0;
123	ctx->fs_fifo_count = 0;
124	ctx->fs_total_count = 0;
125	ctx->fs_badblocks_count = 0;
126	ctx->fs_sockets_count = 0;
127	ctx->fs_ind_count = 0;
128	ctx->fs_dind_count = 0;
129	ctx->fs_tind_count = 0;
130	ctx->fs_fragmented = 0;
131	ctx->large_files = 0;
132
133	/* Reset the superblock to the user's requested value */
134	ctx->superblock = ctx->use_superblock;
135
136	return 0;
137}
138
139void e2fsck_free_context(e2fsck_t ctx)
140{
141	if (!ctx)
142		return;
143
144	e2fsck_reset_context(ctx);
145
146	ext2fs_free_mem((void **) &ctx);
147}
148
149/*
150 * This function runs through the e2fsck passes and calls them all,
151 * returning restart, abort, or cancel as necessary...
152 */
153typedef void (*pass_t)(e2fsck_t ctx);
154
155pass_t e2fsck_passes[] = {
156	e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
157	e2fsck_pass5, 0 };
158
159#define E2F_FLAG_RUN_RETURN	(E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
160
161int e2fsck_run(e2fsck_t ctx)
162{
163	int	i;
164	pass_t	e2fsck_pass;
165
166#ifdef HAVE_SETJMP_H
167	if (setjmp(ctx->abort_loc))
168		return (ctx->flags & E2F_FLAG_RUN_RETURN);
169	ctx->flags |= E2F_FLAG_SETJMP_OK;
170#endif
171
172	for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
173		if (ctx->flags & E2F_FLAG_RUN_RETURN)
174			break;
175		e2fsck_pass(ctx);
176		if (ctx->progress)
177			(void) (ctx->progress)(ctx, 0, 0, 0);
178	}
179	ctx->flags &= ~E2F_FLAG_SETJMP_OK;
180
181	if (ctx->flags & E2F_FLAG_RUN_RETURN)
182		return (ctx->flags & E2F_FLAG_RUN_RETURN);
183	return 0;
184}
185
186
187
188
189
190
191