blob: 1be6b5e19cdb4035a1cf31515780df9caac63073 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * e2fsck.c - a consistency checker for the new extended file system.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
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/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -040018 * This function allocates an e2fsck context
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000019 */
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'o1f3ad142005-04-14 14:07:53 -040024 char *time_env;
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040026 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &context);
Theodore Ts'o08b21301997-11-03 19:42:40 +000027 if (retval)
28 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040029
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'o0684a4f2002-08-17 10:19:44 -040033 context->ext_attr_ver = 2;
Theodore Ts'o9f0288d2007-08-03 20:43:37 -040034 context->blocks_per_page = 1;
Theodore Ts'o7dca4c82008-12-23 19:10:43 -050035 context->htree_slack_percentage = 255;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040036
Theodore Ts'o1f3ad142005-04-14 14:07:53 -040037 time_env = getenv("E2FSCK_TIME");
38 if (time_env)
39 context->now = strtoul(time_env, NULL, 0);
Theodore Ts'o177839e2010-05-13 17:36:36 -040040 else {
Theodore Ts'o1f3ad142005-04-14 14:07:53 -040041 context->now = time(0);
Theodore Ts'o177839e2010-05-13 17:36:36 -040042 if (context->now < 1262322000) /* January 1 2010 */
43 context->flags |= E2F_FLAG_TIME_INSANE;
44 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000045
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000046 *ret = context;
47 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000048}
49
50/*
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000051 * This function resets an e2fsck context; it is called when e2fsck
52 * needs to be restarted.
Theodore Ts'o3839e651997-04-26 13:21:57 +000053 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054errcode_t e2fsck_reset_context(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000055{
Theodore Ts'o8da6d1a2008-08-14 09:48:07 -040056 int i;
57
Theodore Ts'o177839e2010-05-13 17:36:36 -040058 ctx->flags &= E2F_RESET_FLAGS;
Theodore Ts'o850d05e2002-07-25 00:00:08 -040059 ctx->lost_and_found = 0;
60 ctx->bad_lost_and_found = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000061 if (ctx->inode_used_map) {
62 ext2fs_free_inode_bitmap(ctx->inode_used_map);
63 ctx->inode_used_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000064 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000065 if (ctx->inode_dir_map) {
66 ext2fs_free_inode_bitmap(ctx->inode_dir_map);
67 ctx->inode_dir_map = 0;
68 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000069 if (ctx->inode_reg_map) {
70 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
Theodore Ts'o7142db01999-11-08 18:46:54 +000071 ctx->inode_reg_map = 0;
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000072 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000073 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 }
Theodore Ts'oadee8d72001-07-23 00:17:49 -040081 if (ctx->journal_io) {
Theodore Ts'o1e165262001-07-29 11:48:10 -040082 if (ctx->fs && ctx->fs->io != ctx->journal_io)
Theodore Ts'oadee8d72001-07-23 00:17:49 -040083 io_channel_close(ctx->journal_io);
84 ctx->journal_io = 0;
85 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +000086 if (ctx->fs && ctx->fs->dblist) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000087 ext2fs_free_dblist(ctx->fs->dblist);
88 ctx->fs->dblist = 0;
89 }
Theodore Ts'o08b21301997-11-03 19:42:40 +000090 e2fsck_free_dir_info(ctx);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040091#ifdef ENABLE_HTREE
92 e2fsck_free_dx_dir_info(ctx);
93#endif
Theodore Ts'o342d8472001-07-02 11:54:09 -040094 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 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000102 if (ctx->block_dup_map) {
103 ext2fs_free_block_bitmap(ctx->block_dup_map);
104 ctx->block_dup_map = 0;
105 }
Theodore Ts'o342d8472001-07-02 11:54:09 -0400106 if (ctx->block_ea_map) {
107 ext2fs_free_block_bitmap(ctx->block_ea_map);
108 ctx->block_ea_map = 0;
109 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000110 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 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000118 if (ctx->inode_imagic_map) {
119 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
120 ctx->inode_imagic_map = 0;
121 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400122 if (ctx->dirs_to_hash) {
123 ext2fs_u32_list_free(ctx->dirs_to_hash);
124 ctx->dirs_to_hash = 0;
125 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000127 /*
128 * Clear the array of invalid meta-data flags
129 */
130 if (ctx->invalid_inode_bitmap_flag) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400131 ext2fs_free_mem(&ctx->invalid_inode_bitmap_flag);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000132 ctx->invalid_inode_bitmap_flag = 0;
133 }
134 if (ctx->invalid_block_bitmap_flag) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400135 ext2fs_free_mem(&ctx->invalid_block_bitmap_flag);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000136 ctx->invalid_block_bitmap_flag = 0;
137 }
138 if (ctx->invalid_inode_table_flag) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400139 ext2fs_free_mem(&ctx->invalid_inode_table_flag);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000140 ctx->invalid_inode_table_flag = 0;
141 }
Theodore Ts'o638c15d2015-10-25 22:19:04 -0400142 if (ctx->encrypted_dirs) {
143 ext2fs_u32_list_free(ctx->encrypted_dirs);
144 ctx->encrypted_dirs = 0;
145 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000146
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000147 /* 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;
Theodore Ts'oce44d8c2008-12-08 21:33:11 -0500163 ctx->fs_fragmented_dir = 0;
Theodore Ts'o246501c1998-03-24 16:22:38 +0000164 ctx->large_files = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165
Theodore Ts'o8da6d1a2008-08-14 09:48:07 -0400166 for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
167 ctx->extent_depth_count[i] = 0;
168
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000169 /* Reset the superblock to the user's requested value */
170 ctx->superblock = ctx->use_superblock;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400171
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000172 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173}
Theodore Ts'o521e3681997-04-29 17:48:10 +0000174
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000175void e2fsck_free_context(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000177 if (!ctx)
178 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400179
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000180 e2fsck_reset_context(ctx);
Theodore Ts'of3640932003-03-01 19:47:44 -0500181 if (ctx->blkid)
182 blkid_put_cache(ctx->blkid);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500183
184 if (ctx->profile)
185 profile_release(ctx->profile);
Theodore Ts'o1dc506c2008-03-26 09:09:09 -0400186
187 if (ctx->filesystem_name)
188 ext2fs_free_mem(&ctx->filesystem_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400189
Theodore Ts'o1dc506c2008-03-26 09:09:09 -0400190 if (ctx->device_name)
191 ext2fs_free_mem(&ctx->device_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400192
JP Abgralle0ed7402014-03-19 19:08:39 -0700193 if (ctx->log_fn)
194 free(ctx->log_fn);
195
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400196 ext2fs_free_mem(&ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197}
Theodore Ts'o08b21301997-11-03 19:42:40 +0000198
199/*
200 * This function runs through the e2fsck passes and calls them all,
201 * returning restart, abort, or cancel as necessary...
202 */
203typedef void (*pass_t)(e2fsck_t ctx);
204
JP Abgralle0ed7402014-03-19 19:08:39 -0700205static pass_t e2fsck_passes[] = {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000206 e2fsck_pass1, e2fsck_pass2, e2fsck_pass3, e2fsck_pass4,
207 e2fsck_pass5, 0 };
208
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000209#define E2F_FLAG_RUN_RETURN (E2F_FLAG_SIGNAL_MASK|E2F_FLAG_RESTART)
210
Theodore Ts'o08b21301997-11-03 19:42:40 +0000211int e2fsck_run(e2fsck_t ctx)
212{
213 int i;
214 pass_t e2fsck_pass;
215
216#ifdef HAVE_SETJMP_H
Theodore Ts'obbbc92f2004-02-21 20:53:03 -0500217 if (setjmp(ctx->abort_loc)) {
218 ctx->flags &= ~E2F_FLAG_SETJMP_OK;
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000219 return (ctx->flags & E2F_FLAG_RUN_RETURN);
Theodore Ts'obbbc92f2004-02-21 20:53:03 -0500220 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000221 ctx->flags |= E2F_FLAG_SETJMP_OK;
222#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400223
Theodore Ts'o08b21301997-11-03 19:42:40 +0000224 for (i=0; (e2fsck_pass = e2fsck_passes[i]); i++) {
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000225 if (ctx->flags & E2F_FLAG_RUN_RETURN)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000226 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700227 if (e2fsck_mmp_update(ctx->fs))
228 fatal_error(ctx, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000229 e2fsck_pass(ctx);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000230 if (ctx->progress)
231 (void) (ctx->progress)(ctx, 0, 0, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000232 }
233 ctx->flags &= ~E2F_FLAG_SETJMP_OK;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400234
Theodore Ts'o2df1f6a1998-02-27 05:03:48 +0000235 if (ctx->flags & E2F_FLAG_RUN_RETURN)
236 return (ctx->flags & E2F_FLAG_RUN_RETURN);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000237 return 0;
238}