aboutsummaryrefslogtreecommitdiffstats
path: root/e2fsck/pass4.c
blob: 67a4e9e00aca8145895b094ddd70e56826ffd70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * pass4.c -- pass #4 of e2fsck: Check reference counts
 *
 * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
 * under the terms of the GNU Public License.
 * 
 */

#include "e2fsck.h"

void pass4(ext2_filsys fs)
{
	ino_t	i;
	struct ext2_inode	inode;
	struct resource_track	rtrack;
	
	init_resource_track(&rtrack);

#ifdef MTRACE
	mtrace_print("Pass 4");
#endif

	if (!preen)
		printf("Pass 4: Checking reference counts\n");
	for (i=1; i <= fs->super->s_inodes_count; i++) {
		if (i == EXT2_BAD_INO ||
		    (i > EXT2_ROOT_INO && i < EXT2_FIRST_INO))
			continue;
		if (!(ext2fs_test_inode_bitmap(inode_used_map, i)))
			continue;
		if (inode_count[i] == 0) {
			/*
			 * Inode isn't attached to the filesystem;
			 * prompt to reconnect.
			 */
			printf("Unattached inode %lu\n", i);
			preenhalt(fs);
			if (ask("Connect to /lost+found", 1)) {
				if (reconnect_file(fs, i))
					ext2fs_unmark_valid(fs);
			} else
				ext2fs_unmark_valid(fs);
		}
		if (inode_count[i] != inode_link_info[i]) {
			e2fsck_read_inode(fs, i, &inode, "pass4");
			if (inode_link_info[i] != inode.i_links_count) {
				printf("WARNING: PROGRAMMING BUG IN E2FSCK!\n");
				printf("inode_link_info[%ld] is %u, "
				       "inode.i_links_count is %d.  "
				       "They should be the same!\n",
				       i, inode_link_info[i],
				       inode.i_links_count);
			}
			printf("Inode %lu has ref count %d, expecting %d.\n",
			       i, inode.i_links_count, inode_count[i]);
			if (ask("Set i_nlinks to count", 1)) {
				inode.i_links_count = inode_count[i];
				e2fsck_write_inode(fs, i, &inode, "pass4");
			} else
				ext2fs_unmark_valid(fs);
		}
	}
	free(inode_link_info);	inode_link_info = 0;
	free(inode_count);	inode_count = 0;
	if (tflag > 1) {
		printf("Pass 4: ");
		print_resource_track(&rtrack);
	}
}