blob: 66e4a75ee28a1f6d7b274334d21e0bc7cc35b3c1 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * e2fsck.c - a consistency checker for the new extended file system.
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
Theodore Ts'o50e1e101997-04-26 13:58:21 +000012#include <errno.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000013
Theodore Ts'o3839e651997-04-26 13:21:57 +000014#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000015#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000016
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000017/*
18 * This function allocates an e2fsck context
19 */
20errcode_t e2fsck_allocate_context(e2fsck_t *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000021{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000022 e2fsck_t context;
Theodore Ts'o08b21301997-11-03 19:42:40 +000023 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000024
Theodore Ts'o08b21301997-11-03 19:42:40 +000025 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct),
26 (void **) &context);
27 if (retval)
28 return retval;
29
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000030 memset(context, 0, sizeof(struct e2fsck_struct));
Theodore Ts'o7f88b041997-04-26 14:48:50 +000031
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000032 context->process_inode_size = 256;
Theodore Ts'o3839e651997-04-26 13:21:57 +000033
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000034 *ret = context;
35 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000036}
37
38/*
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000039 * This function resets an e2fsck context; it is called when e2fsck
40 * needs to be restarted.
Theodore Ts'o3839e651997-04-26 13:21:57 +000041 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000042errcode_t e2fsck_reset_context(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000043{
Theodore Ts'o08b21301997-11-03 19:42:40 +000044 ctx->flags = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000045 if (ctx->inode_used_map) {
46 ext2fs_free_inode_bitmap(ctx->inode_used_map);
47 ctx->inode_used_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000048 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000049 if (ctx->inode_dir_map) {
50 ext2fs_free_inode_bitmap(ctx->inode_dir_map);
51 ctx->inode_dir_map = 0;
52 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000053 if (ctx->inode_reg_map) {
54 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
Theodore Ts'o7142db01999-11-08 18:46:54 +000055 ctx->inode_reg_map = 0;
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000056 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000057 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 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +000065 if (ctx->fs && ctx->fs->dblist) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 ext2fs_free_dblist(ctx->fs->dblist);
67 ctx->fs->dblist = 0;
68 }
Theodore Ts'o08b21301997-11-03 19:42:40 +000069 e2fsck_free_dir_info(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000070 if (ctx->block_dup_map) {
71 ext2fs_free_block_bitmap(ctx->block_dup_map);
72 ctx->block_dup_map = 0;
73 }
74 if (ctx->inode_bb_map) {
75 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
76 ctx->inode_bb_map = 0;
77 }
78 if (ctx->inode_bad_map) {
79 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
80 ctx->inode_bad_map = 0;
81 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000082 if (ctx->inode_imagic_map) {
83 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
84 ctx->inode_imagic_map = 0;
85 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000087 /*
88 * Clear the array of invalid meta-data flags
89 */
90 if (ctx->invalid_inode_bitmap_flag) {
Theodore Ts'o08b21301997-11-03 19:42:40 +000091 ext2fs_free_mem((void **) &ctx->invalid_inode_bitmap_flag);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000092 ctx->invalid_inode_bitmap_flag = 0;
93 }
94 if (ctx->invalid_block_bitmap_flag) {
Theodore Ts'o08b21301997-11-03 19:42:40 +000095 ext2fs_free_mem((void **) &ctx->invalid_block_bitmap_flag);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000096 ctx->invalid_block_bitmap_flag = 0;
97 }
98 if (ctx->invalid_inode_table_flag) {
Theodore Ts'o08b21301997-11-03 19:42:40 +000099 ext2fs_free_mem((void **) &ctx->invalid_inode_table_flag);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000100 ctx->invalid_inode_table_flag = 0;
101 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000102
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000103 /* Clear statistic counters */
104 ctx->fs_directory_count = 0;
105 ctx->fs_regular_count = 0;
106 ctx->fs_blockdev_count = 0;
107 ctx->fs_chardev_count = 0;
108 ctx->fs_links_count = 0;
109 ctx->fs_symlinks_count = 0;
110 ctx->fs_fast_symlinks_count = 0;
111 ctx->fs_fifo_count = 0;
112 ctx->fs_total_count = 0;
113 ctx->fs_badblocks_count = 0;
114 ctx->fs_sockets_count = 0;
115 ctx->fs_ind_count = 0;
116 ctx->fs_dind_count = 0;
117 ctx->fs_tind_count = 0;
118 ctx->fs_fragmented = 0;
Theodore Ts'o246501c1998-03-24 16:22:38 +0000119 ctx->large_files = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 /* Reset the superblock to the user's requested value */
122 ctx->superblock = ctx->use_superblock;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000124 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125}
Theodore Ts'o521e3681997-04-29 17:48:10 +0000126
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000127void e2fsck_free_context(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000129 if (!ctx)
130 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000132 e2fsck_reset_context(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133
Theodore Ts'o08b21301997-11-03 19:42:40 +0000134 ext2fs_free_mem((void **) &ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135}
Theodore Ts'o08b21301997-11-03 19:42:40 +0000136
137/*
138 * This function runs through the e2fsck passes and calls them all,
139 * returning restart, abort, or cancel as necessary...
140 */
141typedef void (*pass_t)(e2fsck_t ctx);
142
143pass_t e2fsck_passes[] = {
144 e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
145 e2fsck_pass5, 0 };
146
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000147#define E2F_FLAG_RUN_RETURN (E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
148
Theodore Ts'o08b21301997-11-03 19:42:40 +0000149int e2fsck_run(e2fsck_t ctx)
150{
151 int i;
152 pass_t e2fsck_pass;
153
154#ifdef HAVE_SETJMP_H
155 if (setjmp(ctx->abort_loc))
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000156 return (ctx->flags & E2F_FLAG_RUN_RETURN);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000157 ctx->flags |= E2F_FLAG_SETJMP_OK;
158#endif
159
160 for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000161 if (ctx->flags & E2F_FLAG_RUN_RETURN)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000162 break;
163 e2fsck_pass(ctx);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000164 if (ctx->progress)
165 (void) (ctx->progress)(ctx, 0, 0, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000166 }
167 ctx->flags &= ~E2F_FLAG_SETJMP_OK;
168
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000169 if (ctx->flags & E2F_FLAG_RUN_RETURN)
170 return (ctx->flags & E2F_FLAG_RUN_RETURN);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000171 return 0;
172}
173
174
175
176
177
178