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