e2fsprogs/e2fsck/pass4.c

146 lines
3.6 KiB
C
Raw Normal View History

1997-04-26 17:21:57 +04:00
/*
* pass4.c -- pass #4 of e2fsck: Check reference counts
*
1997-04-29 20:15:03 +04:00
* Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*
* Pass 4 frees the following data structures:
* - A bitmap of which inodes are in bad blocks. (inode_bb_map)
1997-04-26 17:21:57 +04:00
*/
#include "e2fsck.h"
1997-04-29 20:15:03 +04:00
#include "problem.h"
1997-04-26 17:21:57 +04:00
1997-04-29 19:29:49 +04:00
/*
* This routine is called when an inode is not connected to the
* directory tree.
*
* This subroutine returns 1 then the caller shouldn't bother with the
* rest of the pass 4 tests.
*/
static int disconnect_inode(e2fsck_t ctx, ino_t i)
1997-04-29 19:29:49 +04:00
{
ext2_filsys fs = ctx->fs;
1997-04-29 19:29:49 +04:00
struct ext2_inode inode;
1997-04-29 20:15:03 +04:00
struct problem_context pctx;
1997-04-29 19:29:49 +04:00
e2fsck_read_inode(fs, i, &inode, "pass4: disconnect_inode");
1997-04-29 20:15:03 +04:00
clear_problem_context(&pctx);
pctx.ino = i;
pctx.inode = &inode;
1997-04-29 19:29:49 +04:00
if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
LINUX_S_ISDIR(inode.i_mode))) {
/*
* This is a zero-length file; prompt to delete it...
*/
if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
ext2fs_icount_store(ctx->inode_link_info, i, 0);
1997-04-29 19:29:49 +04:00
inode.i_links_count = 0;
inode.i_dtime = time(0);
e2fsck_write_inode(fs, i, &inode,
"disconnect_inode");
/*
* Fix up the bitmaps...
*/
read_bitmaps(ctx);
ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
1997-04-29 19:29:49 +04:00
ext2fs_unmark_inode_bitmap(fs->inode_map, i);
ext2fs_mark_ib_dirty(fs);
return 0;
}
}
/*
* Prompt to reconnect.
*/
if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
if (reconnect_file(ctx, i))
1997-04-29 19:29:49 +04:00
ext2fs_unmark_valid(fs);
} else {
/*
* If we don't attach the inode, then skip the
* i_links_test since there's no point in trying to
* force i_links_count to zero.
*/
ext2fs_unmark_valid(fs);
return 1;
}
return 0;
}
void pass4(e2fsck_t ctx)
1997-04-26 17:21:57 +04:00
{
ext2_filsys fs = ctx->fs;
1997-04-26 17:34:30 +04:00
ino_t i;
1997-04-26 17:21:57 +04:00
struct ext2_inode inode;
#ifdef RESOURCE_TRACK
1997-04-26 17:21:57 +04:00
struct resource_track rtrack;
#endif
1997-04-29 20:15:03 +04:00
struct problem_context pctx;
__u16 link_count, link_counted;
1997-04-26 17:21:57 +04:00
#ifdef RESOURCE_TRACK
1997-04-26 17:21:57 +04:00
init_resource_track(&rtrack);
#endif
1997-04-26 17:21:57 +04:00
#ifdef MTRACE
mtrace_print("Pass 4");
#endif
1997-04-29 20:15:03 +04:00
clear_problem_context(&pctx);
if (!(ctx->options & E2F_OPT_PREEN))
fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
1997-04-26 17:21:57 +04:00
for (i=1; i <= fs->super->s_inodes_count; i++) {
if (i == EXT2_BAD_INO ||
1997-04-26 18:48:50 +04:00
(i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
1997-04-26 17:21:57 +04:00
continue;
if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
(ctx->inode_bb_map &&
ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
1997-04-26 17:21:57 +04:00
continue;
ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
1997-04-29 20:15:03 +04:00
if (link_counted == 0) {
if (disconnect_inode(ctx, i))
1997-04-29 18:53:37 +04:00
continue;
ext2fs_icount_fetch(ctx->inode_link_info, i,
&link_count);
ext2fs_icount_fetch(ctx->inode_count, i,
&link_counted);
1997-04-26 17:21:57 +04:00
}
1997-04-29 20:15:03 +04:00
if (link_counted != link_count) {
1997-04-26 17:34:30 +04:00
e2fsck_read_inode(fs, i, &inode, "pass4");
1997-04-29 20:15:03 +04:00
pctx.ino = i;
pctx.inode = &inode;
if (link_count != inode.i_links_count) {
pctx.num = link_count;
fix_problem(ctx,
PR_4_INCONSISTENT_COUNT, &pctx);
1997-04-26 17:21:57 +04:00
}
1997-04-29 20:15:03 +04:00
pctx.num = link_counted;
if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
1997-04-29 20:15:03 +04:00
inode.i_links_count = link_counted;
1997-04-26 17:34:30 +04:00
e2fsck_write_inode(fs, i, &inode, "pass4");
1997-04-29 20:15:03 +04:00
}
1997-04-26 17:21:57 +04:00
}
}
ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
ext2fs_free_inode_bitmap(ctx->inode_bb_map);
ctx->inode_bb_map = 0;
#ifdef RESOURCE_TRACK
if (ctx->options & E2F_OPT_TIME2)
print_resource_track("Pass 4", &rtrack);
#endif
1997-04-26 17:21:57 +04:00
}