Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * badblocks.c --- routines to manipulate the bad block structure |
| 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 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% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <time.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 19 | #if HAVE_SYS_STAT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #if HAVE_SYS_TYPES_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 26 | #if EXT2_FLAT_INCLUDES |
| 27 | #include "ext2_fs.h" |
| 28 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 30 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 32 | #include "ext2fsP.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 35 | * Helper function for making a badblocks list |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | */ |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 37 | static errcode_t make_badblocks_list(int size, int num, blk_t *list, |
| 38 | ext2_badblocks_list *ret) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 39 | { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 40 | ext2_badblocks_list bb; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 41 | errcode_t retval; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 42 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 43 | retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list), |
| 44 | (void **) &bb); |
| 45 | if (retval) |
| 46 | return retval; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 47 | memset(bb, 0, sizeof(struct ext2_struct_badblocks_list)); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 48 | bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 49 | bb->size = size ? size : 10; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 50 | bb->num = num; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 51 | retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 52 | if (!bb->list) { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 53 | ext2fs_free_mem((void **) &bb); |
| 54 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 55 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 56 | if (list) |
| 57 | memcpy(bb->list, list, bb->size * sizeof(blk_t)); |
| 58 | else |
| 59 | memset(bb->list, 0, bb->size * sizeof(blk_t)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 60 | *ret = bb; |
| 61 | return 0; |
| 62 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | /* |
| 66 | * This procedure creates an empty badblocks list. |
| 67 | */ |
| 68 | errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size) |
| 69 | { |
| 70 | return make_badblocks_list(size, 0, 0, ret); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * This procedure copies a badblocks list |
| 75 | */ |
| 76 | errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src, |
| 77 | ext2_badblocks_list *dest) |
| 78 | { |
| 79 | errcode_t retval; |
| 80 | |
| 81 | retval = make_badblocks_list(src->size, src->num, src->list, |
| 82 | dest); |
| 83 | if (retval) |
| 84 | return retval; |
| 85 | (*dest)->badblocks_flags = src->badblocks_flags; |
| 86 | return 0; |
| 87 | } |
| 88 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * This procedure frees a badblocks list. |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 92 | * |
| 93 | * (note: moved to closefs.c) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 94 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 95 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * This procedure adds a block to a badblocks list. |
| 99 | */ |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 100 | errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 101 | { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 102 | errcode_t retval; |
| 103 | int i, j; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame^] | 104 | unsigned long old_size; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 106 | EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST); |
| 107 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 108 | if (bb->num >= bb->size) { |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame^] | 109 | old_size = bb->size * sizeof(blk_t); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 110 | bb->size += 10; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame^] | 111 | retval = ext2fs_resize_mem(old_size, bb->size * sizeof(blk_t), |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 112 | (void **) &bb->list); |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame^] | 113 | if (retval) { |
| 114 | bb->size -= 10; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 115 | return retval; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame^] | 116 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 119 | j = bb->num; |
| 120 | for (i=0; i < bb->num; i++) { |
| 121 | if (bb->list[i] == blk) |
| 122 | return 0; |
| 123 | if (bb->list[i] > blk) { |
| 124 | j = i; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | for (i=bb->num; i > j; i--) |
| 129 | bb->list[i] = bb->list[i-1]; |
| 130 | bb->list[j] = blk; |
| 131 | bb->num++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * This procedure tests to see if a particular block is on a badblocks |
| 137 | * list. |
| 138 | */ |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 139 | int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 140 | { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 141 | int low, high, mid; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 142 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 143 | if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST) |
| 144 | return 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 145 | |
Theodore Ts'o | f635d7f | 1997-05-09 02:50:16 +0000 | [diff] [blame] | 146 | if (bb->num == 0) |
| 147 | return 0; |
| 148 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 149 | low = 0; |
| 150 | high = bb->num-1; |
| 151 | if (blk == bb->list[low]) |
| 152 | return 1; |
| 153 | if (blk == bb->list[high]) |
| 154 | return 1; |
| 155 | |
| 156 | while (low < high) { |
| 157 | mid = (low+high)/2; |
| 158 | if (mid == low || mid == high) |
| 159 | break; |
| 160 | if (blk == bb->list[mid]) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 161 | return 1; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 162 | if (blk < bb->list[mid]) |
| 163 | high = mid; |
| 164 | else |
| 165 | low = mid; |
| 166 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 170 | errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb, |
| 171 | ext2_badblocks_iterate *ret) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 173 | ext2_badblocks_iterate iter; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 174 | errcode_t retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 175 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 176 | EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST); |
| 177 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 178 | retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate), |
| 179 | (void **) &iter); |
| 180 | if (retval) |
| 181 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 182 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 183 | iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 184 | iter->bb = bb; |
| 185 | iter->ptr = 0; |
| 186 | *ret = iter; |
| 187 | return 0; |
| 188 | } |
| 189 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 190 | int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 191 | { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 192 | ext2_badblocks_list bb; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 193 | |
| 194 | if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE) |
| 195 | return 0; |
| 196 | |
| 197 | bb = iter->bb; |
| 198 | |
| 199 | if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST) |
| 200 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 201 | |
| 202 | if (iter->ptr < bb->num) { |
| 203 | *blk = bb->list[iter->ptr++]; |
| 204 | return 1; |
| 205 | } |
| 206 | *blk = 0; |
| 207 | return 0; |
| 208 | } |
| 209 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 210 | void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 211 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 212 | if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)) |
| 213 | return; |
| 214 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 215 | iter->bb = 0; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 216 | ext2fs_free_mem((void **) &iter); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |