2 * e2fsck.c - a consistency checker for the new extended file system.
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
18 * This function allocates an e2fsck context
20 errcode_t e2fsck_allocate_context(e2fsck_t *ret)
26 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &context);
30 memset(context, 0, sizeof(struct e2fsck_struct));
32 context->process_inode_size = 256;
33 context->ext_attr_ver = 2;
34 context->blocks_per_page = 1;
36 time_env = getenv("E2FSCK_TIME");
38 context->now = strtoul(time_env, NULL, 0);
40 context->now = time(0);
47 * This function resets an e2fsck context; it is called when e2fsck
48 * needs to be restarted.
50 errcode_t e2fsck_reset_context(e2fsck_t ctx)
55 ctx->lost_and_found = 0;
56 ctx->bad_lost_and_found = 0;
57 if (ctx->inode_used_map) {
58 ext2fs_free_inode_bitmap(ctx->inode_used_map);
59 ctx->inode_used_map = 0;
61 if (ctx->inode_dir_map) {
62 ext2fs_free_inode_bitmap(ctx->inode_dir_map);
63 ctx->inode_dir_map = 0;
65 if (ctx->inode_reg_map) {
66 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
67 ctx->inode_reg_map = 0;
69 if (ctx->block_found_map) {
70 ext2fs_free_block_bitmap(ctx->block_found_map);
71 ctx->block_found_map = 0;
73 if (ctx->inode_link_info) {
74 ext2fs_free_icount(ctx->inode_link_info);
75 ctx->inode_link_info = 0;
77 if (ctx->journal_io) {
78 if (ctx->fs && ctx->fs->io != ctx->journal_io)
79 io_channel_close(ctx->journal_io);
82 if (ctx->fs && ctx->fs->dblist) {
83 ext2fs_free_dblist(ctx->fs->dblist);
86 e2fsck_free_dir_info(ctx);
88 e2fsck_free_dx_dir_info(ctx);
91 ea_refcount_free(ctx->refcount);
94 if (ctx->refcount_extra) {
95 ea_refcount_free(ctx->refcount_extra);
96 ctx->refcount_extra = 0;
98 if (ctx->block_dup_map) {
99 ext2fs_free_block_bitmap(ctx->block_dup_map);
100 ctx->block_dup_map = 0;
102 if (ctx->block_ea_map) {
103 ext2fs_free_block_bitmap(ctx->block_ea_map);
104 ctx->block_ea_map = 0;
106 if (ctx->inode_bb_map) {
107 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
108 ctx->inode_bb_map = 0;
110 if (ctx->inode_bad_map) {
111 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
112 ctx->inode_bad_map = 0;
114 if (ctx->inode_imagic_map) {
115 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
116 ctx->inode_imagic_map = 0;
118 if (ctx->dirs_to_hash) {
119 ext2fs_u32_list_free(ctx->dirs_to_hash);
120 ctx->dirs_to_hash = 0;
124 * Clear the array of invalid meta-data flags
126 if (ctx->invalid_inode_bitmap_flag) {
127 ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag);
128 ctx->invalid_inode_bitmap_flag = 0;
130 if (ctx->invalid_block_bitmap_flag) {
131 ext2fs_free_mem(&ctx->invalid_block_bitmap_flag);
132 ctx->invalid_block_bitmap_flag = 0;
134 if (ctx->invalid_inode_table_flag) {
135 ext2fs_free_mem(&ctx->invalid_inode_table_flag);
136 ctx->invalid_inode_table_flag = 0;
139 /* Clear statistic counters */
140 ctx->fs_directory_count = 0;
141 ctx->fs_regular_count = 0;
142 ctx->fs_blockdev_count = 0;
143 ctx->fs_chardev_count = 0;
144 ctx->fs_links_count = 0;
145 ctx->fs_symlinks_count = 0;
146 ctx->fs_fast_symlinks_count = 0;
147 ctx->fs_fifo_count = 0;
148 ctx->fs_total_count = 0;
149 ctx->fs_badblocks_count = 0;
150 ctx->fs_sockets_count = 0;
151 ctx->fs_ind_count = 0;
152 ctx->fs_dind_count = 0;
153 ctx->fs_tind_count = 0;
154 ctx->fs_fragmented = 0;
155 ctx->large_files = 0;
157 for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
158 ctx->extent_depth_count[i] = 0;
160 /* Reset the superblock to the user's requested value */
161 ctx->superblock = ctx->use_superblock;
166 void e2fsck_free_context(e2fsck_t ctx)
171 e2fsck_reset_context(ctx);
173 blkid_put_cache(ctx->blkid);
176 profile_release(ctx->profile);
178 if (ctx->filesystem_name)
179 ext2fs_free_mem(&ctx->filesystem_name);
181 if (ctx->device_name)
182 ext2fs_free_mem(&ctx->device_name);
184 ext2fs_free_mem(&ctx);
188 * This function runs through the e2fsck passes and calls them all,
189 * returning restart, abort, or cancel as necessary...
191 typedef void (*pass_t)(e2fsck_t ctx);
193 pass_t e2fsck_passes[] = {
194 e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
197 #define E2F_FLAG_RUN_RETURN (E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
199 int e2fsck_run(e2fsck_t ctx)
205 if (setjmp(ctx->abort_loc)) {
206 ctx->flags &= ~E2F_FLAG_SETJMP_OK;
207 return (ctx->flags & E2F_FLAG_RUN_RETURN);
209 ctx->flags |= E2F_FLAG_SETJMP_OK;
212 for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
213 if (ctx->flags & E2F_FLAG_RUN_RETURN)
217 (void) (ctx->progress)(ctx, 0, 0, 0);
219 ctx->flags &= ~E2F_FLAG_SETJMP_OK;
221 if (ctx->flags & E2F_FLAG_RUN_RETURN)
222 return (ctx->flags & E2F_FLAG_RUN_RETURN);