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