2 * badblocks.c --- routines to manipulate the bad block structure
4 * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
30 * Helper function for making a badblocks list
32 static errcode_t make_u32_list(int size, int num, __u32 *list,
38 retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_list), &bb);
41 memset(bb, 0, sizeof(struct ext2_struct_u32_list));
42 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
43 bb->size = size ? size : 10;
45 retval = ext2fs_get_mem(bb->size * sizeof(blk_t), &bb->list);
51 memcpy(bb->list, list, bb->size * sizeof(blk_t));
53 memset(bb->list, 0, bb->size * sizeof(blk_t));
60 * This procedure creates an empty u32 list.
62 errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size)
64 return make_u32_list(size, 0, 0, ret);
68 * This procedure creates an empty badblocks list.
70 errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
72 return make_u32_list(size, 0, 0, (ext2_badblocks_list *) ret);
77 * This procedure copies a badblocks list
79 errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest)
83 retval = make_u32_list(src->size, src->num, src->list, dest);
86 (*dest)->badblocks_flags = src->badblocks_flags;
90 errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
91 ext2_badblocks_list *dest)
93 return ext2fs_u32_copy((ext2_u32_list) src,
94 (ext2_u32_list *) dest);
98 * This procedure frees a badblocks list.
100 * (note: moved to closefs.c)
105 * This procedure adds a block to a badblocks list.
107 errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk)
111 unsigned long old_size;
113 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
115 if (bb->num >= bb->size) {
116 old_size = bb->size * sizeof(__u32);
118 retval = ext2fs_resize_mem(old_size, bb->size * sizeof(__u32),
127 * Add special case code for appending to the end of the list
130 if ((bb->num != 0) && (bb->list[i] == blk))
132 if ((bb->num == 0) || (bb->list[i] < blk)) {
133 bb->list[bb->num++] = blk;
138 for (i=0; i < bb->num; i++) {
139 if (bb->list[i] == blk)
141 if (bb->list[i] > blk) {
146 for (i=bb->num; i > j; i--)
147 bb->list[i] = bb->list[i-1];
153 errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
155 return ext2fs_u32_list_add((ext2_u32_list) bb, (__u32) blk);
159 * This procedure finds a particular block is on a badblocks
162 int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk)
166 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
174 if (blk == bb->list[low])
176 if (blk == bb->list[high])
181 if (mid == low || mid == high)
183 if (blk == bb->list[mid])
185 if (blk < bb->list[mid])
194 * This procedure tests to see if a particular block is on a badblocks
197 int ext2fs_u32_list_test(ext2_u32_list bb, __u32 blk)
199 if (ext2fs_u32_list_find(bb, blk) < 0)
205 int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
207 return ext2fs_u32_list_test((ext2_u32_list) bb, (__u32) blk);
212 * Remove a block from the badblock list
214 int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk)
221 remloc = ext2fs_u32_list_find(bb, blk);
225 for (i = remloc ; i < bb->num-1; i++)
226 bb->list[i] = bb->list[i+1];
231 void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk)
233 ext2fs_u32_list_del(bb, blk);
236 errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
237 ext2_u32_iterate *ret)
239 ext2_u32_iterate iter;
242 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
244 retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_iterate), &iter);
248 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
255 errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
256 ext2_badblocks_iterate *ret)
258 return ext2fs_u32_list_iterate_begin((ext2_u32_list) bb,
259 (ext2_u32_iterate *) ret);
263 int ext2fs_u32_list_iterate(ext2_u32_iterate iter, __u32 *blk)
267 if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
272 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
275 if (iter->ptr < bb->num) {
276 *blk = bb->list[iter->ptr++];
283 int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
285 return ext2fs_u32_list_iterate((ext2_u32_iterate) iter,
290 void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter)
292 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
296 ext2fs_free_mem(&iter);
299 void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
301 ext2fs_u32_list_iterate_end((ext2_u32_iterate) iter);
305 int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2)
307 EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
308 EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
310 if (bb1->num != bb2->num)
313 if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0)
318 int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
320 return ext2fs_u32_list_equal((ext2_u32_list) bb1,
321 (ext2_u32_list) bb2);
324 int ext2fs_u32_list_count(ext2_u32_list bb)