Whamcloud - gitweb
38e9f6e7ca6ef8f3a3d642ea686999ee80f018e0
[tools/e2fsprogs.git] / lib / ext2fs / badblocks.c
1 /*
2  * badblocks.c --- routines to manipulate the bad block structure
3  * 
4  * Copyright (C) 1994, 1995, 1996 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 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #if HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29
30 #include <linux/ext2_fs.h>
31
32 #include "ext2fsP.h"
33
34 /*
35  * Helper function for making a badblocks list
36  */
37 static errcode_t make_badblocks_list(int size, int num, blk_t *list,
38                                      ext2_badblocks_list *ret)
39 {
40         ext2_badblocks_list     bb;
41         
42         bb = malloc(sizeof(struct ext2_struct_badblocks_list));
43         if (!bb)
44                 return ENOMEM;
45         memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
46         bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
47         bb->size = size ? size : 10;
48         bb->num = num;
49         bb->list = malloc(bb->size * sizeof(blk_t));
50         if (!bb->list) {
51                 free(bb);
52                 return ENOMEM;
53         }
54         if (list)
55                 memcpy(bb->list, list, bb->size * sizeof(blk_t));
56         else
57                 memset(bb->list, 0, bb->size * sizeof(blk_t));
58         *ret = bb;
59         return 0;
60 }
61         
62
63 /*
64  * This procedure creates an empty badblocks list.
65  */
66 errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
67 {
68         return make_badblocks_list(size, 0, 0, ret);
69 }
70
71 /*
72  * This procedure copies a badblocks list
73  */
74 errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
75                                 ext2_badblocks_list *dest)
76 {
77         errcode_t       retval;
78         
79         retval = make_badblocks_list(src->size, src->num, src->list,
80                                      dest);
81         if (retval)
82                 return retval;
83         (*dest)->badblocks_flags = src->badblocks_flags;
84         return 0;
85 }
86
87
88 /*
89  * This procedure frees a badblocks list.
90  *
91  * (note: moved to closefs.c)
92  */
93
94
95 /*
96  * This procedure adds a block to a badblocks list.
97  */
98 errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
99 {
100         int     i, j;
101         blk_t   *new_list;
102
103         EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
104
105         if (bb->num >= bb->size) {
106                 bb->size += 10;
107                 new_list = realloc(bb->list, bb->size * sizeof(blk_t));
108                 if (!new_list)
109                         return ENOMEM;
110                 bb->list = new_list;
111         }
112
113         j = bb->num;
114         for (i=0; i < bb->num; i++) {
115                 if (bb->list[i] == blk)
116                         return 0;
117                 if (bb->list[i] > blk) {
118                         j = i;
119                         break;
120                 }
121         }
122         for (i=bb->num; i > j; i--)
123                 bb->list[i] = bb->list[i-1];
124         bb->list[j] = blk;
125         bb->num++;
126         return 0;
127 }
128
129 /*
130  * This procedure tests to see if a particular block is on a badblocks
131  * list.
132  */
133 int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
134 {
135         int     low, high, mid;
136
137         if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
138                 return 0;
139
140         if (bb->num == 0)
141                 return 0;
142
143         low = 0;
144         high = bb->num-1;
145         if (blk == bb->list[low])
146                 return 1;
147         if (blk == bb->list[high])
148                 return 1;
149
150         while (low < high) {
151                 mid = (low+high)/2;
152                 if (mid == low || mid == high)
153                         break;
154                 if (blk == bb->list[mid])
155                         return 1;
156                 if (blk < bb->list[mid])
157                         high = mid;
158                 else
159                         low = mid;
160         }
161         return 0;
162 }
163
164 errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
165                                               ext2_badblocks_iterate *ret)
166 {
167         ext2_badblocks_iterate iter;
168
169         EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
170
171         iter = malloc(sizeof(struct ext2_struct_badblocks_iterate));
172         if (!iter)
173                 return ENOMEM;
174
175         iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
176         iter->bb = bb;
177         iter->ptr = 0;
178         *ret = iter;
179         return 0;
180 }
181
182 int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
183 {
184         ext2_badblocks_list     bb;
185
186         if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
187                 return 0;
188
189         bb = iter->bb;
190
191         if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
192                 return 0;
193         
194         if (iter->ptr < bb->num) {
195                 *blk = bb->list[iter->ptr++];
196                 return 1;
197         } 
198         *blk = 0;
199         return 0;
200 }
201
202 void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
203 {
204         if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
205                 return;
206
207         iter->bb = 0;
208         free(iter);
209 }
210
211
212
213
214