lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 | */ |
| 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 = strtoul(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 | */ |
| 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_link_info) { |
| 79 | ext2fs_free_icount(ctx->inode_link_info); |
| 80 | ctx->inode_link_info = 0; |
| 81 | } |
| 82 | if (ctx->journal_io) { |
| 83 | if (ctx->fs && ctx->fs->io != ctx->journal_io) |
| 84 | io_channel_close(ctx->journal_io); |
| 85 | ctx->journal_io = 0; |
| 86 | } |
| 87 | if (ctx->fs && ctx->fs->dblist) { |
| 88 | ext2fs_free_dblist(ctx->fs->dblist); |
| 89 | ctx->fs->dblist = 0; |
| 90 | } |
| 91 | e2fsck_free_dir_info(ctx); |
| 92 | #ifdef ENABLE_HTREE |
| 93 | e2fsck_free_dx_dir_info(ctx); |
| 94 | #endif |
| 95 | if (ctx->refcount) { |
| 96 | ea_refcount_free(ctx->refcount); |
| 97 | ctx->refcount = 0; |
| 98 | } |
| 99 | if (ctx->refcount_extra) { |
| 100 | ea_refcount_free(ctx->refcount_extra); |
| 101 | ctx->refcount_extra = 0; |
| 102 | } |
| 103 | if (ctx->block_dup_map) { |
| 104 | ext2fs_free_block_bitmap(ctx->block_dup_map); |
| 105 | ctx->block_dup_map = 0; |
| 106 | } |
| 107 | if (ctx->block_ea_map) { |
| 108 | ext2fs_free_block_bitmap(ctx->block_ea_map); |
| 109 | ctx->block_ea_map = 0; |
| 110 | } |
| 111 | if (ctx->inode_bb_map) { |
| 112 | ext2fs_free_inode_bitmap(ctx->inode_bb_map); |
| 113 | ctx->inode_bb_map = 0; |
| 114 | } |
| 115 | if (ctx->inode_bad_map) { |
| 116 | ext2fs_free_inode_bitmap(ctx->inode_bad_map); |
| 117 | ctx->inode_bad_map = 0; |
| 118 | } |
| 119 | if (ctx->inode_imagic_map) { |
| 120 | ext2fs_free_inode_bitmap(ctx->inode_imagic_map); |
| 121 | ctx->inode_imagic_map = 0; |
| 122 | } |
| 123 | if (ctx->dirs_to_hash) { |
| 124 | ext2fs_u32_list_free(ctx->dirs_to_hash); |
| 125 | ctx->dirs_to_hash = 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Clear the array of invalid meta-data flags |
| 130 | */ |
| 131 | if (ctx->invalid_inode_bitmap_flag) { |
| 132 | ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag); |
| 133 | ctx->invalid_inode_bitmap_flag = 0; |
| 134 | } |
| 135 | if (ctx->invalid_block_bitmap_flag) { |
| 136 | ext2fs_free_mem(&ctx->invalid_block_bitmap_flag); |
| 137 | ctx->invalid_block_bitmap_flag = 0; |
| 138 | } |
| 139 | if (ctx->invalid_inode_table_flag) { |
| 140 | ext2fs_free_mem(&ctx->invalid_inode_table_flag); |
| 141 | ctx->invalid_inode_table_flag = 0; |
| 142 | } |
| 143 | |
| 144 | /* Clear statistic counters */ |
| 145 | ctx->fs_directory_count = 0; |
| 146 | ctx->fs_regular_count = 0; |
| 147 | ctx->fs_blockdev_count = 0; |
| 148 | ctx->fs_chardev_count = 0; |
| 149 | ctx->fs_links_count = 0; |
| 150 | ctx->fs_symlinks_count = 0; |
| 151 | ctx->fs_fast_symlinks_count = 0; |
| 152 | ctx->fs_fifo_count = 0; |
| 153 | ctx->fs_total_count = 0; |
| 154 | ctx->fs_badblocks_count = 0; |
| 155 | ctx->fs_sockets_count = 0; |
| 156 | ctx->fs_ind_count = 0; |
| 157 | ctx->fs_dind_count = 0; |
| 158 | ctx->fs_tind_count = 0; |
| 159 | ctx->fs_fragmented = 0; |
| 160 | ctx->fs_fragmented_dir = 0; |
| 161 | ctx->large_files = 0; |
| 162 | |
| 163 | for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++) |
| 164 | ctx->extent_depth_count[i] = 0; |
| 165 | |
| 166 | /* Reset the superblock to the user's requested value */ |
| 167 | ctx->superblock = ctx->use_superblock; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | void e2fsck_free_context(e2fsck_t ctx) |
| 173 | { |
| 174 | if (!ctx) |
| 175 | return; |
| 176 | |
| 177 | e2fsck_reset_context(ctx); |
| 178 | if (ctx->blkid) |
| 179 | blkid_put_cache(ctx->blkid); |
| 180 | |
| 181 | if (ctx->profile) |
| 182 | profile_release(ctx->profile); |
| 183 | |
| 184 | if (ctx->filesystem_name) |
| 185 | ext2fs_free_mem(&ctx->filesystem_name); |
| 186 | |
| 187 | if (ctx->device_name) |
| 188 | ext2fs_free_mem(&ctx->device_name); |
| 189 | |
| 190 | if (ctx->log_fn) |
| 191 | free(ctx->log_fn); |
| 192 | |
| 193 | ext2fs_free_mem(&ctx); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * This function runs through the e2fsck passes and calls them all, |
| 198 | * returning restart, abort, or cancel as necessary... |
| 199 | */ |
| 200 | typedef void (*pass_t)(e2fsck_t ctx); |
| 201 | |
| 202 | static pass_t e2fsck_passes[] = { |
| 203 | e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4, |
| 204 | e2fsck_pass5, 0 }; |
| 205 | |
| 206 | #define E2F_FLAG_RUN_RETURN (E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART) |
| 207 | |
| 208 | int e2fsck_run(e2fsck_t ctx) |
| 209 | { |
| 210 | int i; |
| 211 | pass_t e2fsck_pass; |
| 212 | |
| 213 | #ifdef HAVE_SETJMP_H |
| 214 | if (setjmp(ctx->abort_loc)) { |
| 215 | ctx->flags &= ~E2F_FLAG_SETJMP_OK; |
| 216 | return (ctx->flags & E2F_FLAG_RUN_RETURN); |
| 217 | } |
| 218 | ctx->flags |= E2F_FLAG_SETJMP_OK; |
| 219 | #endif |
| 220 | |
| 221 | for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) { |
| 222 | if (ctx->flags & E2F_FLAG_RUN_RETURN) |
| 223 | break; |
| 224 | if (e2fsck_mmp_update(ctx->fs)) |
| 225 | fatal_error(ctx, 0); |
| 226 | e2fsck_pass(ctx); |
| 227 | if (ctx->progress) |
| 228 | (void) (ctx->progress)(ctx, 0, 0, 0); |
| 229 | } |
| 230 | ctx->flags &= ~E2F_FLAG_SETJMP_OK; |
| 231 | |
| 232 | if (ctx->flags & E2F_FLAG_RUN_RETURN) |
| 233 | return (ctx->flags & E2F_FLAG_RUN_RETURN); |
| 234 | return 0; |
| 235 | } |