xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/tst_iscan.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * tst_inode.c --- this function tests the inode scan function
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1996 by Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
14*6a54128fSAndroid Build Coastguard Worker #include <string.h>
15*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
16*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
17*6a54128fSAndroid Build Coastguard Worker #endif
18*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
19*6a54128fSAndroid Build Coastguard Worker #include <time.h>
20*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
21*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
22*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
23*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
24*6a54128fSAndroid Build Coastguard Worker #endif
25*6a54128fSAndroid Build Coastguard Worker 
26*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
27*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
28*6a54128fSAndroid Build Coastguard Worker 
29*6a54128fSAndroid Build Coastguard Worker blk64_t test_vec[] = { 8, 12, 24, 34, 43, 44, 100, 0 };
30*6a54128fSAndroid Build Coastguard Worker 
31*6a54128fSAndroid Build Coastguard Worker ext2_filsys	test_fs;
32*6a54128fSAndroid Build Coastguard Worker ext2fs_block_bitmap bad_block_map, touched_map;
33*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap bad_inode_map;
34*6a54128fSAndroid Build Coastguard Worker badblocks_list	test_badblocks;
35*6a54128fSAndroid Build Coastguard Worker 
36*6a54128fSAndroid Build Coastguard Worker int first_no_comma = 1;
37*6a54128fSAndroid Build Coastguard Worker int failed = 0;
38*6a54128fSAndroid Build Coastguard Worker 
iscan_test_read_blk64(unsigned long long block,int count,errcode_t err)39*6a54128fSAndroid Build Coastguard Worker static void iscan_test_read_blk64(unsigned long long block, int count, errcode_t err)
40*6a54128fSAndroid Build Coastguard Worker {
41*6a54128fSAndroid Build Coastguard Worker 	int	i;
42*6a54128fSAndroid Build Coastguard Worker 
43*6a54128fSAndroid Build Coastguard Worker 	if (first_no_comma)
44*6a54128fSAndroid Build Coastguard Worker 		first_no_comma = 0;
45*6a54128fSAndroid Build Coastguard Worker 	else
46*6a54128fSAndroid Build Coastguard Worker 		printf(", ");
47*6a54128fSAndroid Build Coastguard Worker 
48*6a54128fSAndroid Build Coastguard Worker 	if (count > 1)
49*6a54128fSAndroid Build Coastguard Worker 		printf("%llu-%llu", block, block+count-1);
50*6a54128fSAndroid Build Coastguard Worker 	else
51*6a54128fSAndroid Build Coastguard Worker 		printf("%llu", block);
52*6a54128fSAndroid Build Coastguard Worker 
53*6a54128fSAndroid Build Coastguard Worker 	for (i=0; i < count; i++, block++) {
54*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_block_bitmap2(touched_map, block)) {
55*6a54128fSAndroid Build Coastguard Worker 			printf("\nDuplicate block?!? --- %llu\n", block);
56*6a54128fSAndroid Build Coastguard Worker 			failed++;
57*6a54128fSAndroid Build Coastguard Worker 			first_no_comma = 1;
58*6a54128fSAndroid Build Coastguard Worker 		}
59*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(touched_map, block);
60*6a54128fSAndroid Build Coastguard Worker 	}
61*6a54128fSAndroid Build Coastguard Worker }
62*6a54128fSAndroid Build Coastguard Worker 
iscan_test_read_blk(unsigned long block,int count,errcode_t err)63*6a54128fSAndroid Build Coastguard Worker static void iscan_test_read_blk(unsigned long block, int count, errcode_t err)
64*6a54128fSAndroid Build Coastguard Worker {
65*6a54128fSAndroid Build Coastguard Worker 	iscan_test_read_blk64(block, count, err);
66*6a54128fSAndroid Build Coastguard Worker }
67*6a54128fSAndroid Build Coastguard Worker 
68*6a54128fSAndroid Build Coastguard Worker /*
69*6a54128fSAndroid Build Coastguard Worker  * Setup the variables for doing the inode scan test.
70*6a54128fSAndroid Build Coastguard Worker  */
setup(void)71*6a54128fSAndroid Build Coastguard Worker static void setup(void)
72*6a54128fSAndroid Build Coastguard Worker {
73*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
74*6a54128fSAndroid Build Coastguard Worker 	int		i;
75*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block param;
76*6a54128fSAndroid Build Coastguard Worker 
77*6a54128fSAndroid Build Coastguard Worker 	initialize_ext2_error_table();
78*6a54128fSAndroid Build Coastguard Worker 
79*6a54128fSAndroid Build Coastguard Worker 	memset(&param, 0, sizeof(param));
80*6a54128fSAndroid Build Coastguard Worker 	ext2fs_blocks_count_set(&param, 12000);
81*6a54128fSAndroid Build Coastguard Worker 
82*6a54128fSAndroid Build Coastguard Worker 
83*6a54128fSAndroid Build Coastguard Worker 	test_io_cb_read_blk = iscan_test_read_blk;
84*6a54128fSAndroid Build Coastguard Worker 	test_io_cb_read_blk64 = iscan_test_read_blk64;
85*6a54128fSAndroid Build Coastguard Worker 
86*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
87*6a54128fSAndroid Build Coastguard Worker 				   test_io_manager, &test_fs);
88*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
89*6a54128fSAndroid Build Coastguard Worker 		com_err("setup", retval,
90*6a54128fSAndroid Build Coastguard Worker 			"While initializing filesystem");
91*6a54128fSAndroid Build Coastguard Worker 		exit(1);
92*6a54128fSAndroid Build Coastguard Worker 	}
93*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_tables(test_fs);
94*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
95*6a54128fSAndroid Build Coastguard Worker 		com_err("setup", retval,
96*6a54128fSAndroid Build Coastguard Worker 			"While allocating tables for test filesystem");
97*6a54128fSAndroid Build Coastguard Worker 		exit(1);
98*6a54128fSAndroid Build Coastguard Worker 	}
99*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_block_bitmap(test_fs, "bad block map",
100*6a54128fSAndroid Build Coastguard Worker 					      &bad_block_map);
101*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
102*6a54128fSAndroid Build Coastguard Worker 		com_err("setup", retval,
103*6a54128fSAndroid Build Coastguard Worker 			"While allocating bad_block bitmap");
104*6a54128fSAndroid Build Coastguard Worker 		exit(1);
105*6a54128fSAndroid Build Coastguard Worker 	}
106*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_block_bitmap(test_fs, "touched map",
107*6a54128fSAndroid Build Coastguard Worker 					      &touched_map);
108*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
109*6a54128fSAndroid Build Coastguard Worker 		com_err("setup", retval,
110*6a54128fSAndroid Build Coastguard Worker 			"While allocating touched block bitmap");
111*6a54128fSAndroid Build Coastguard Worker 		exit(1);
112*6a54128fSAndroid Build Coastguard Worker 	}
113*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_inode_bitmap(test_fs, "bad inode map",
114*6a54128fSAndroid Build Coastguard Worker 					      &bad_inode_map);
115*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
116*6a54128fSAndroid Build Coastguard Worker 		com_err("setup", retval,
117*6a54128fSAndroid Build Coastguard Worker 			"While allocating bad inode bitmap");
118*6a54128fSAndroid Build Coastguard Worker 		exit(1);
119*6a54128fSAndroid Build Coastguard Worker 	}
120*6a54128fSAndroid Build Coastguard Worker 
121*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_badblocks_list_create(&test_badblocks, 5);
122*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
123*6a54128fSAndroid Build Coastguard Worker 		com_err("setup", retval, "while creating badblocks list");
124*6a54128fSAndroid Build Coastguard Worker 		exit(1);
125*6a54128fSAndroid Build Coastguard Worker 	}
126*6a54128fSAndroid Build Coastguard Worker 	for (i=0; test_vec[i]; i++) {
127*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_badblocks_list_add(test_badblocks, test_vec[i]);
128*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
129*6a54128fSAndroid Build Coastguard Worker 			com_err("setup", retval,
130*6a54128fSAndroid Build Coastguard Worker 				"while adding test vector %d", i);
131*6a54128fSAndroid Build Coastguard Worker 			exit(1);
132*6a54128fSAndroid Build Coastguard Worker 		}
133*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(bad_block_map, test_vec[i]);
134*6a54128fSAndroid Build Coastguard Worker 	}
135*6a54128fSAndroid Build Coastguard Worker 	test_fs->badblocks = test_badblocks;
136*6a54128fSAndroid Build Coastguard Worker }
137*6a54128fSAndroid Build Coastguard Worker 
138*6a54128fSAndroid Build Coastguard Worker /*
139*6a54128fSAndroid Build Coastguard Worker  * Iterate using inode_scan
140*6a54128fSAndroid Build Coastguard Worker  */
iterate(void)141*6a54128fSAndroid Build Coastguard Worker static void iterate(void)
142*6a54128fSAndroid Build Coastguard Worker {
143*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode inode;
144*6a54128fSAndroid Build Coastguard Worker 	ext2_inode_scan	scan;
145*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
146*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	ino;
147*6a54128fSAndroid Build Coastguard Worker 
148*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open_inode_scan(test_fs, 8, &scan);
149*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
150*6a54128fSAndroid Build Coastguard Worker 		com_err("iterate", retval, "While opening inode scan");
151*6a54128fSAndroid Build Coastguard Worker 		exit(1);
152*6a54128fSAndroid Build Coastguard Worker 	}
153*6a54128fSAndroid Build Coastguard Worker 	printf("Reading blocks: ");
154*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_next_inode(scan, &ino, &inode);
155*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
156*6a54128fSAndroid Build Coastguard Worker 		com_err("iterate", retval, "while reading first inode");
157*6a54128fSAndroid Build Coastguard Worker 		exit(1);
158*6a54128fSAndroid Build Coastguard Worker 	}
159*6a54128fSAndroid Build Coastguard Worker 	while (ino) {
160*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_next_inode(scan, &ino, &inode);
161*6a54128fSAndroid Build Coastguard Worker 		if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
162*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_inode_bitmap2(bad_inode_map, ino);
163*6a54128fSAndroid Build Coastguard Worker 			continue;
164*6a54128fSAndroid Build Coastguard Worker 		}
165*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
166*6a54128fSAndroid Build Coastguard Worker 			com_err("iterate", retval,
167*6a54128fSAndroid Build Coastguard Worker 				"while getting next inode");
168*6a54128fSAndroid Build Coastguard Worker 			exit(1);
169*6a54128fSAndroid Build Coastguard Worker 		}
170*6a54128fSAndroid Build Coastguard Worker 	}
171*6a54128fSAndroid Build Coastguard Worker 	printf("\n");
172*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close_inode_scan(scan);
173*6a54128fSAndroid Build Coastguard Worker }
174*6a54128fSAndroid Build Coastguard Worker 
175*6a54128fSAndroid Build Coastguard Worker /*
176*6a54128fSAndroid Build Coastguard Worker  * Verify the touched map
177*6a54128fSAndroid Build Coastguard Worker  */
check_map(void)178*6a54128fSAndroid Build Coastguard Worker static void check_map(void)
179*6a54128fSAndroid Build Coastguard Worker {
180*6a54128fSAndroid Build Coastguard Worker 	int	i, j, first=1;
181*6a54128fSAndroid Build Coastguard Worker 	blk64_t	blk;
182*6a54128fSAndroid Build Coastguard Worker 
183*6a54128fSAndroid Build Coastguard Worker 	for (i=0; test_vec[i]; i++) {
184*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_block_bitmap2(touched_map, test_vec[i])) {
185*6a54128fSAndroid Build Coastguard Worker 			printf("Bad block was touched --- %llu\n",
186*6a54128fSAndroid Build Coastguard Worker 			       (unsigned long long) test_vec[i]);
187*6a54128fSAndroid Build Coastguard Worker 			failed++;
188*6a54128fSAndroid Build Coastguard Worker 			first_no_comma = 1;
189*6a54128fSAndroid Build Coastguard Worker 		}
190*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(touched_map, test_vec[i]);
191*6a54128fSAndroid Build Coastguard Worker 	}
192*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < test_fs->group_desc_count; i++) {
193*6a54128fSAndroid Build Coastguard Worker 		for (j=0, blk = ext2fs_inode_table_loc(test_fs, i);
194*6a54128fSAndroid Build Coastguard Worker 		     j < test_fs->inode_blocks_per_group;
195*6a54128fSAndroid Build Coastguard Worker 		     j++, blk++) {
196*6a54128fSAndroid Build Coastguard Worker 			if (!ext2fs_test_block_bitmap2(touched_map, blk) &&
197*6a54128fSAndroid Build Coastguard Worker 			    !ext2fs_test_block_bitmap2(bad_block_map, blk)) {
198*6a54128fSAndroid Build Coastguard Worker 				printf("Missing block --- %llu\n",
199*6a54128fSAndroid Build Coastguard Worker 				       (unsigned long long) blk);
200*6a54128fSAndroid Build Coastguard Worker 				failed++;
201*6a54128fSAndroid Build Coastguard Worker 			}
202*6a54128fSAndroid Build Coastguard Worker 		}
203*6a54128fSAndroid Build Coastguard Worker 	}
204*6a54128fSAndroid Build Coastguard Worker 	printf("Bad inodes: ");
205*6a54128fSAndroid Build Coastguard Worker 	for (i=1; i <= test_fs->super->s_inodes_count; i++) {
206*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_inode_bitmap2(bad_inode_map, i)) {
207*6a54128fSAndroid Build Coastguard Worker 			if (first)
208*6a54128fSAndroid Build Coastguard Worker 				first = 0;
209*6a54128fSAndroid Build Coastguard Worker 			else
210*6a54128fSAndroid Build Coastguard Worker 				printf(", ");
211*6a54128fSAndroid Build Coastguard Worker 			printf("%u", i);
212*6a54128fSAndroid Build Coastguard Worker 		}
213*6a54128fSAndroid Build Coastguard Worker 	}
214*6a54128fSAndroid Build Coastguard Worker 	printf("\n");
215*6a54128fSAndroid Build Coastguard Worker }
216*6a54128fSAndroid Build Coastguard Worker 
217*6a54128fSAndroid Build Coastguard Worker 
main(int argc,char ** argv)218*6a54128fSAndroid Build Coastguard Worker int main(int argc, char **argv)
219*6a54128fSAndroid Build Coastguard Worker {
220*6a54128fSAndroid Build Coastguard Worker 	setup();
221*6a54128fSAndroid Build Coastguard Worker 	iterate();
222*6a54128fSAndroid Build Coastguard Worker 	check_map();
223*6a54128fSAndroid Build Coastguard Worker 	if (!failed)
224*6a54128fSAndroid Build Coastguard Worker 		printf("Inode scan tested OK!\n");
225*6a54128fSAndroid Build Coastguard Worker 	return failed;
226*6a54128fSAndroid Build Coastguard Worker }
227*6a54128fSAndroid Build Coastguard Worker 
228