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