Whamcloud - gitweb
Overhaul extended attribute handling. Should now be correct with
[tools/e2fsprogs.git] / e2fsck / e2fsck.c
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
25         retval = ext2fs_get_mem(sizeof(struct e2fsck_struct),
26                                 (void **) &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
35         *ret = context;
36         return 0;
37 }
38
39 /*
40  * This function resets an e2fsck context; it is called when e2fsck
41  * needs to be restarted.
42  */
43 errcode_t e2fsck_reset_context(e2fsck_t ctx)
44 {
45         ctx->flags = 0;
46         ctx->lost_and_found = 0;
47         ctx->bad_lost_and_found = 0;
48         if (ctx->inode_used_map) {
49                 ext2fs_free_inode_bitmap(ctx->inode_used_map);
50                 ctx->inode_used_map = 0;
51         }
52         if (ctx->inode_dir_map) {
53                 ext2fs_free_inode_bitmap(ctx->inode_dir_map);
54                 ctx->inode_dir_map = 0;
55         }
56         if (ctx->inode_reg_map) {
57                 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
58                 ctx->inode_reg_map = 0;
59         }
60         if (ctx->block_found_map) {
61                 ext2fs_free_block_bitmap(ctx->block_found_map);
62                 ctx->block_found_map = 0;
63         }
64         if (ctx->inode_link_info) {
65                 ext2fs_free_icount(ctx->inode_link_info);
66                 ctx->inode_link_info = 0;
67         }
68         if (ctx->journal_io) {
69                 if (ctx->fs && ctx->fs->io != ctx->journal_io)
70                         io_channel_close(ctx->journal_io);
71                 ctx->journal_io = 0;
72         }
73         if (ctx->fs && ctx->fs->dblist) {
74                 ext2fs_free_dblist(ctx->fs->dblist);
75                 ctx->fs->dblist = 0;
76         }
77         e2fsck_free_dir_info(ctx);
78 #ifdef ENABLE_HTREE
79         e2fsck_free_dx_dir_info(ctx);
80 #endif
81         if (ctx->refcount) {
82                 ea_refcount_free(ctx->refcount);
83                 ctx->refcount = 0;
84         }
85         if (ctx->refcount_extra) {
86                 ea_refcount_free(ctx->refcount_extra);
87                 ctx->refcount_extra = 0;
88         }
89         if (ctx->block_dup_map) {
90                 ext2fs_free_block_bitmap(ctx->block_dup_map);
91                 ctx->block_dup_map = 0;
92         }
93         if (ctx->block_ea_map) {
94                 ext2fs_free_block_bitmap(ctx->block_ea_map);
95                 ctx->block_ea_map = 0;
96         }
97         if (ctx->inode_bb_map) {
98                 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
99                 ctx->inode_bb_map = 0;
100         }
101         if (ctx->inode_bad_map) {
102                 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
103                 ctx->inode_bad_map = 0;
104         }
105         if (ctx->inode_imagic_map) {
106                 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
107                 ctx->inode_imagic_map = 0;
108         }
109         if (ctx->dirs_to_hash) {
110                 ext2fs_u32_list_free(ctx->dirs_to_hash);
111                 ctx->dirs_to_hash = 0;
112         }
113
114         /*
115          * Clear the array of invalid meta-data flags
116          */
117         if (ctx->invalid_inode_bitmap_flag) {
118                 ext2fs_free_mem((void **) &ctx->invalid_inode_bitmap_flag);
119                 ctx->invalid_inode_bitmap_flag = 0;
120         }
121         if (ctx->invalid_block_bitmap_flag) {
122                 ext2fs_free_mem((void **) &ctx->invalid_block_bitmap_flag);
123                 ctx->invalid_block_bitmap_flag = 0;
124         }
125         if (ctx->invalid_inode_table_flag) {
126                 ext2fs_free_mem((void **) &ctx->invalid_inode_table_flag);
127                 ctx->invalid_inode_table_flag = 0;
128         }
129
130         /* Clear statistic counters */
131         ctx->fs_directory_count = 0;
132         ctx->fs_regular_count = 0;
133         ctx->fs_blockdev_count = 0;
134         ctx->fs_chardev_count = 0;
135         ctx->fs_links_count = 0;
136         ctx->fs_symlinks_count = 0;
137         ctx->fs_fast_symlinks_count = 0;
138         ctx->fs_fifo_count = 0;
139         ctx->fs_total_count = 0;
140         ctx->fs_badblocks_count = 0;
141         ctx->fs_sockets_count = 0;
142         ctx->fs_ind_count = 0;
143         ctx->fs_dind_count = 0;
144         ctx->fs_tind_count = 0;
145         ctx->fs_fragmented = 0;
146         ctx->large_files = 0;
147
148         /* Reset the superblock to the user's requested value */
149         ctx->superblock = ctx->use_superblock;
150         
151         return 0;
152 }
153
154 void e2fsck_free_context(e2fsck_t ctx)
155 {
156         if (!ctx)
157                 return;
158         
159         e2fsck_reset_context(ctx);
160         
161         ext2fs_free_mem((void **) &ctx);
162 }
163
164 /*
165  * This function runs through the e2fsck passes and calls them all,
166  * returning restart, abort, or cancel as necessary...
167  */
168 typedef void (*pass_t)(e2fsck_t ctx);
169
170 pass_t e2fsck_passes[] = {
171         e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
172         e2fsck_pass5, 0 };
173
174 #define E2F_FLAG_RUN_RETURN     (E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
175
176 int e2fsck_run(e2fsck_t ctx)
177 {
178         int     i;
179         pass_t  e2fsck_pass;
180
181 #ifdef HAVE_SETJMP_H
182         if (setjmp(ctx->abort_loc))
183                 return (ctx->flags & E2F_FLAG_RUN_RETURN);
184         ctx->flags |= E2F_FLAG_SETJMP_OK;
185 #endif
186                 
187         for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
188                 if (ctx->flags & E2F_FLAG_RUN_RETURN)
189                         break;
190                 e2fsck_pass(ctx);
191                 if (ctx->progress)
192                         (void) (ctx->progress)(ctx, 0, 0, 0);
193         }
194         ctx->flags &= ~E2F_FLAG_SETJMP_OK;
195         
196         if (ctx->flags & E2F_FLAG_RUN_RETURN)
197                 return (ctx->flags & E2F_FLAG_RUN_RETURN);
198         return 0;
199 }
200
201
202         
203
204         
205