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>
26 #if EXT2_FLAT_INCLUDES
29 #include <linux/ext2_fs.h>
35 * Helper function for making a badblocks list
37 static errcode_t make_badblocks_list(int size, int num, blk_t *list,
38 ext2_badblocks_list *ret)
40 ext2_badblocks_list bb;
43 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list),
47 memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
48 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
49 bb->size = size ? size : 10;
51 retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list);
53 ext2fs_free_mem((void **) &bb);
57 memcpy(bb->list, list, bb->size * sizeof(blk_t));
59 memset(bb->list, 0, bb->size * sizeof(blk_t));
66 * This procedure creates an empty badblocks list.
68 errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
70 return make_badblocks_list(size, 0, 0, ret);
74 * This procedure copies a badblocks list
76 errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
77 ext2_badblocks_list *dest)
81 retval = make_badblocks_list(src->size, src->num, src->list,
85 (*dest)->badblocks_flags = src->badblocks_flags;
91 * This procedure frees a badblocks list.
93 * (note: moved to closefs.c)
98 * This procedure adds a block to a badblocks list.
100 errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
104 unsigned long old_size;
106 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
108 if (bb->num >= bb->size) {
109 old_size = bb->size * sizeof(blk_t);
111 retval = ext2fs_resize_mem(old_size, bb->size * sizeof(blk_t),
112 (void **) &bb->list);
120 for (i=0; i < bb->num; i++) {
121 if (bb->list[i] == blk)
123 if (bb->list[i] > blk) {
128 for (i=bb->num; i > j; i--)
129 bb->list[i] = bb->list[i-1];
136 * This procedure tests to see if a particular block is on a badblocks
139 int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
143 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
151 if (blk == bb->list[low])
153 if (blk == bb->list[high])
158 if (mid == low || mid == high)
160 if (blk == bb->list[mid])
162 if (blk < bb->list[mid])
170 errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
171 ext2_badblocks_iterate *ret)
173 ext2_badblocks_iterate iter;
176 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
178 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate),
183 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
190 int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
192 ext2_badblocks_list bb;
194 if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
199 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
202 if (iter->ptr < bb->num) {
203 *blk = bb->list[iter->ptr++];
210 void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
212 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
216 ext2fs_free_mem((void **) &iter);