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