blob: 75f6df0adb5e64ea13823c55a9bcf0b6cfacc884 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * badblocks.c --- routines to manipulate the bad block structure
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * 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'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <stdlib.h>
18#include <fcntl.h>
19#include <time.h>
20#include <sys/stat.h>
21#include <sys/types.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#if HAVE_ERRNO_H
23#include <errno.h>
24#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <linux/ext2_fs.h>
27
Theodore Ts'o21c84b71997-04-29 16:15:03 +000028#include "ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000029
30/*
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000031 * Helper function for making a badblocks list
Theodore Ts'o3839e651997-04-26 13:21:57 +000032 */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000033static errcode_t make_badblocks_list(int size, int num, blk_t *list,
34 ext2_badblocks_list *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000035{
Theodore Ts'o21c84b71997-04-29 16:15:03 +000036 ext2_badblocks_list bb;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000037
Theodore Ts'o21c84b71997-04-29 16:15:03 +000038 bb = malloc(sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'o3839e651997-04-26 13:21:57 +000039 if (!bb)
40 return ENOMEM;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000041 memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'of3db3561997-04-26 13:34:30 +000042 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 bb->size = size ? size : 10;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000044 bb->num = num;
Theodore Ts'o3839e651997-04-26 13:21:57 +000045 bb->list = malloc(bb->size * sizeof(blk_t));
46 if (!bb->list) {
47 free(bb);
48 return ENOMEM;
49 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000050 if (list)
51 memcpy(bb->list, list, bb->size * sizeof(blk_t));
52 else
53 memset(bb->list, 0, bb->size * sizeof(blk_t));
Theodore Ts'o3839e651997-04-26 13:21:57 +000054 *ret = bb;
55 return 0;
56}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000057
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
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
84/*
85 * This procedure frees a badblocks list.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000086 *
87 * (note: moved to closefs.c)
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 */
Theodore Ts'of3db3561997-04-26 13:34:30 +000089
Theodore Ts'o3839e651997-04-26 13:21:57 +000090
91/*
92 * This procedure adds a block to a badblocks list.
93 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000094errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +000095{
Theodore Ts'o21c84b71997-04-29 16:15:03 +000096 int i, j;
97 blk_t *new_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +000098
Theodore Ts'of3db3561997-04-26 13:34:30 +000099 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
100
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 if (bb->num >= bb->size) {
102 bb->size += 10;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000103 new_list = realloc(bb->list, bb->size * sizeof(blk_t));
104 if (!new_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 return ENOMEM;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000106 bb->list = new_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 }
108
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000109 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++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 return 0;
123}
124
125/*
126 * This procedure tests to see if a particular block is on a badblocks
127 * list.
128 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000129int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000131 int low, high, mid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000133 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
134 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000135
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000136 if (bb->num == 0)
137 return 0;
138
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000139 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])
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000152 if (blk < bb->list[mid])
153 high = mid;
154 else
155 low = mid;
156 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 return 0;
158}
159
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000160errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
161 ext2_badblocks_iterate *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000163 ext2_badblocks_iterate iter;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164
Theodore Ts'of3db3561997-04-26 13:34:30 +0000165 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
166
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000167 iter = malloc(sizeof(struct ext2_struct_badblocks_iterate));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168 if (!iter)
169 return ENOMEM;
170
Theodore Ts'of3db3561997-04-26 13:34:30 +0000171 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 iter->bb = bb;
173 iter->ptr = 0;
174 *ret = iter;
175 return 0;
176}
177
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000178int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000180 ext2_badblocks_list bb;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000181
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;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189
190 if (iter->ptr < bb->num) {
191 *blk = bb->list[iter->ptr++];
192 return 1;
193 }
194 *blk = 0;
195 return 0;
196}
197
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000198void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000200 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
201 return;
202
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 iter->bb = 0;
204 free(iter);
205}
206
207
208
209
210