Home | History | Annotate | Download | only in e2fsck
      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  */
     20 errcode_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 	context->htree_slack_percentage = 255;
     36 
     37 	time_env = getenv("E2FSCK_TIME");
     38 	if (time_env)
     39 		context->now = strtoul(time_env, NULL, 0);
     40 	else {
     41 		context->now = time(0);
     42 		if (context->now < 1262322000) /* January 1 2010 */
     43 			context->flags |= E2F_FLAG_TIME_INSANE;
     44 	}
     45 
     46 	*ret = context;
     47 	return 0;
     48 }
     49 
     50 /*
     51  * This function resets an e2fsck context; it is called when e2fsck
     52  * needs to be restarted.
     53  */
     54 errcode_t e2fsck_reset_context(e2fsck_t ctx)
     55 {
     56 	int	i;
     57 
     58 	ctx->flags &= E2F_RESET_FLAGS;
     59 	ctx->lost_and_found = 0;
     60 	ctx->bad_lost_and_found = 0;
     61 	if (ctx->inode_used_map) {
     62 		ext2fs_free_inode_bitmap(ctx->inode_used_map);
     63 		ctx->inode_used_map = 0;
     64 	}
     65 	if (ctx->inode_dir_map) {
     66 		ext2fs_free_inode_bitmap(ctx->inode_dir_map);
     67 		ctx->inode_dir_map = 0;
     68 	}
     69 	if (ctx->inode_reg_map) {
     70 		ext2fs_free_inode_bitmap(ctx->inode_reg_map);
     71 		ctx->inode_reg_map = 0;
     72 	}
     73 	if (ctx->block_found_map) {
     74 		ext2fs_free_block_bitmap(ctx->block_found_map);
     75 		ctx->block_found_map = 0;
     76 	}
     77 	if (ctx->inode_link_info) {
     78 		ext2fs_free_icount(ctx->inode_link_info);
     79 		ctx->inode_link_info = 0;
     80 	}
     81 	if (ctx->journal_io) {
     82 		if (ctx->fs && ctx->fs->io != ctx->journal_io)
     83 			io_channel_close(ctx->journal_io);
     84 		ctx->journal_io = 0;
     85 	}
     86 	if (ctx->fs && ctx->fs->dblist) {
     87 		ext2fs_free_dblist(ctx->fs->dblist);
     88 		ctx->fs->dblist = 0;
     89 	}
     90 	e2fsck_free_dir_info(ctx);
     91 #ifdef ENABLE_HTREE
     92 	e2fsck_free_dx_dir_info(ctx);
     93 #endif
     94 	if (ctx->refcount) {
     95 		ea_refcount_free(ctx->refcount);
     96 		ctx->refcount = 0;
     97 	}
     98 	if (ctx->refcount_extra) {
     99 		ea_refcount_free(ctx->refcount_extra);
    100 		ctx->refcount_extra = 0;
    101 	}
    102 	if (ctx->block_dup_map) {
    103 		ext2fs_free_block_bitmap(ctx->block_dup_map);
    104 		ctx->block_dup_map = 0;
    105 	}
    106 	if (ctx->block_ea_map) {
    107 		ext2fs_free_block_bitmap(ctx->block_ea_map);
    108 		ctx->block_ea_map = 0;
    109 	}
    110 	if (ctx->inode_bb_map) {
    111 		ext2fs_free_inode_bitmap(ctx->inode_bb_map);
    112 		ctx->inode_bb_map = 0;
    113 	}
    114 	if (ctx->inode_bad_map) {
    115 		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
    116 		ctx->inode_bad_map = 0;
    117 	}
    118 	if (ctx->inode_imagic_map) {
    119 		ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
    120 		ctx->inode_imagic_map = 0;
    121 	}
    122 	if (ctx->dirs_to_hash) {
    123 		ext2fs_u32_list_free(ctx->dirs_to_hash);
    124 		ctx->dirs_to_hash = 0;
    125 	}
    126 
    127 	/*
    128 	 * Clear the array of invalid meta-data flags
    129 	 */
    130 	if (ctx->invalid_inode_bitmap_flag) {
    131 		ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag);
    132 		ctx->invalid_inode_bitmap_flag = 0;
    133 	}
    134 	if (ctx->invalid_block_bitmap_flag) {
    135 		ext2fs_free_mem(&ctx->invalid_block_bitmap_flag);
    136 		ctx->invalid_block_bitmap_flag = 0;
    137 	}
    138 	if (ctx->invalid_inode_table_flag) {
    139 		ext2fs_free_mem(&ctx->invalid_inode_table_flag);
    140 		ctx->invalid_inode_table_flag = 0;
    141 	}
    142 	if (ctx->encrypted_dirs) {
    143 		ext2fs_u32_list_free(ctx->encrypted_dirs);
    144 		ctx->encrypted_dirs = 0;
    145 	}
    146 
    147 	/* Clear statistic counters */
    148 	ctx->fs_directory_count = 0;
    149 	ctx->fs_regular_count = 0;
    150 	ctx->fs_blockdev_count = 0;
    151 	ctx->fs_chardev_count = 0;
    152 	ctx->fs_links_count = 0;
    153 	ctx->fs_symlinks_count = 0;
    154 	ctx->fs_fast_symlinks_count = 0;
    155 	ctx->fs_fifo_count = 0;
    156 	ctx->fs_total_count = 0;
    157 	ctx->fs_badblocks_count = 0;
    158 	ctx->fs_sockets_count = 0;
    159 	ctx->fs_ind_count = 0;
    160 	ctx->fs_dind_count = 0;
    161 	ctx->fs_tind_count = 0;
    162 	ctx->fs_fragmented = 0;
    163 	ctx->fs_fragmented_dir = 0;
    164 	ctx->large_files = 0;
    165 
    166 	for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
    167 		ctx->extent_depth_count[i] = 0;
    168 
    169 	/* Reset the superblock to the user's requested value */
    170 	ctx->superblock = ctx->use_superblock;
    171 
    172 	return 0;
    173 }
    174 
    175 void e2fsck_free_context(e2fsck_t ctx)
    176 {
    177 	if (!ctx)
    178 		return;
    179 
    180 	e2fsck_reset_context(ctx);
    181 	if (ctx->blkid)
    182 		blkid_put_cache(ctx->blkid);
    183 
    184 	if (ctx->profile)
    185 		profile_release(ctx->profile);
    186 
    187 	if (ctx->filesystem_name)
    188 		ext2fs_free_mem(&ctx->filesystem_name);
    189 
    190 	if (ctx->device_name)
    191 		ext2fs_free_mem(&ctx->device_name);
    192 
    193 	if (ctx->log_fn)
    194 		free(ctx->log_fn);
    195 
    196 	ext2fs_free_mem(&ctx);
    197 }
    198 
    199 /*
    200  * This function runs through the e2fsck passes and calls them all,
    201  * returning restart, abort, or cancel as necessary...
    202  */
    203 typedef void (*pass_t)(e2fsck_t ctx);
    204 
    205 static pass_t e2fsck_passes[] = {
    206 	e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
    207 	e2fsck_pass5, 0 };
    208 
    209 #define E2F_FLAG_RUN_RETURN	(E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
    210 
    211 int e2fsck_run(e2fsck_t ctx)
    212 {
    213 	int	i;
    214 	pass_t	e2fsck_pass;
    215 
    216 #ifdef HAVE_SETJMP_H
    217 	if (setjmp(ctx->abort_loc)) {
    218 		ctx->flags &= ~E2F_FLAG_SETJMP_OK;
    219 		return (ctx->flags & E2F_FLAG_RUN_RETURN);
    220 	}
    221 	ctx->flags |= E2F_FLAG_SETJMP_OK;
    222 #endif
    223 
    224 	for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
    225 		if (ctx->flags & E2F_FLAG_RUN_RETURN)
    226 			break;
    227 		if (e2fsck_mmp_update(ctx->fs))
    228 			fatal_error(ctx, 0);
    229 		e2fsck_pass(ctx);
    230 		if (ctx->progress)
    231 			(void) (ctx->progress)(ctx, 0, 0, 0);
    232 	}
    233 	ctx->flags &= ~E2F_FLAG_SETJMP_OK;
    234 
    235 	if (ctx->flags & E2F_FLAG_RUN_RETURN)
    236 		return (ctx->flags & E2F_FLAG_RUN_RETURN);
    237 	return 0;
    238 }
    239