blob: 4312e1906ed9c35c1b3f1bbdd8b0195e6979876a [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * badblocks.c --- routines to manipulate the bad block structure
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00009 * %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 <fcntl.h>
18#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000027#include "ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000028
29/*
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000030 * Helper function for making a badblocks list
Theodore Ts'o3839e651997-04-26 13:21:57 +000031 */
Theodore Ts'ob7a00562002-07-20 00:28:07 -040032static errcode_t make_u32_list(int size, int num, __u32 *list,
33 ext2_u32_list *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000034{
Theodore Ts'ob7a00562002-07-20 00:28:07 -040035 ext2_u32_list bb;
36 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040037
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040038 retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_list), &bb);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000039 if (retval)
40 return retval;
Theodore Ts'ob7a00562002-07-20 00:28:07 -040041 memset(bb, 0, sizeof(struct ext2_struct_u32_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'oee010792007-11-09 19:01:06 -050045 retval = ext2fs_get_array(bb->size, sizeof(blk_t), &bb->list);
Theodore Ts'o2694f312006-11-14 00:34:34 -050046 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040047 ext2fs_free_mem(&bb);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000048 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 }
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'oefc6f622008-08-27 23:07:54 -040057
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000058
59/*
Theodore Ts'ob7a00562002-07-20 00:28:07 -040060 * This procedure creates an empty u32 list.
61 */
62errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size)
63{
64 return make_u32_list(size, 0, 0, ret);
65}
66
67/*
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000068 * This procedure creates an empty badblocks list.
69 */
70errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
71{
Theodore Ts'ob7a00562002-07-20 00:28:07 -040072 return make_u32_list(size, 0, 0, (ext2_badblocks_list *) ret);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000073}
74
Theodore Ts'ob7a00562002-07-20 00:28:07 -040075
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000076/*
77 * This procedure copies a badblocks list
78 */
Theodore Ts'ob7a00562002-07-20 00:28:07 -040079errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000080{
81 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040082
Theodore Ts'ob7a00562002-07-20 00:28:07 -040083 retval = make_u32_list(src->size, src->num, src->list, dest);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000084 if (retval)
85 return retval;
86 (*dest)->badblocks_flags = src->badblocks_flags;
87 return 0;
88}
89
Theodore Ts'ob7a00562002-07-20 00:28:07 -040090errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
91 ext2_badblocks_list *dest)
92{
93 return ext2fs_u32_copy((ext2_u32_list) src,
94 (ext2_u32_list *) dest);
95}
Theodore Ts'o3839e651997-04-26 13:21:57 +000096
97/*
98 * This procedure frees a badblocks list.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000099 *
100 * (note: moved to closefs.c)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000102
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103
104/*
105 * This procedure adds a block to a badblocks list.
106 */
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400107errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000109 errcode_t retval;
110 int i, j;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000111 unsigned long old_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112
Theodore Ts'of3db3561997-04-26 13:34:30 +0000113 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
114
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 if (bb->num >= bb->size) {
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400116 old_size = bb->size * sizeof(__u32);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000117 bb->size += 100;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400118 retval = ext2fs_resize_mem(old_size, bb->size * sizeof(__u32),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400119 &bb->list);
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000120 if (retval) {
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000121 bb->size -= 100;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000122 return retval;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000123 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 }
125
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +0000126 /*
127 * Add special case code for appending to the end of the list
128 */
129 i = bb->num-1;
130 if ((bb->num != 0) && (bb->list[i] == blk))
131 return 0;
132 if ((bb->num == 0) || (bb->list[i] < blk)) {
133 bb->list[bb->num++] = blk;
134 return 0;
135 }
136
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000137 j = bb->num;
138 for (i=0; i < bb->num; i++) {
139 if (bb->list[i] == blk)
140 return 0;
141 if (bb->list[i] > blk) {
142 j = i;
143 break;
144 }
145 }
146 for (i=bb->num; i > j; i--)
147 bb->list[i] = bb->list[i-1];
148 bb->list[j] = blk;
149 bb->num++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 return 0;
151}
152
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400153errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
154{
155 return ext2fs_u32_list_add((ext2_u32_list) bb, (__u32) blk);
156}
157
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400158/*
159 * This procedure finds a particular block is on a badblocks
160 * list.
161 */
162int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk)
163{
164 int low, high, mid;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400165
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400166 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
167 return -1;
168
169 if (bb->num == 0)
170 return -1;
171
172 low = 0;
173 high = bb->num-1;
174 if (blk == bb->list[low])
175 return low;
176 if (blk == bb->list[high])
177 return high;
178
179 while (low < high) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700180 mid = ((unsigned)low + (unsigned)high)/2;
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400181 if (mid == low || mid == high)
182 break;
183 if (blk == bb->list[mid])
184 return mid;
185 if (blk < bb->list[mid])
186 high = mid;
187 else
188 low = mid;
189 }
190 return -1;
191}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400192
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193/*
194 * This procedure tests to see if a particular block is on a badblocks
195 * list.
196 */
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400197int ext2fs_u32_list_test(ext2_u32_list bb, __u32 blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198{
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400199 if (ext2fs_u32_list_find(bb, blk) < 0)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000200 return 0;
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400201 else
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000202 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203}
204
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400205int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206{
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400207 return ext2fs_u32_list_test((ext2_u32_list) bb, (__u32) blk);
208}
209
210
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400211/*
212 * Remove a block from the badblock list
213 */
214int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk)
215{
216 int remloc, i;
217
218 if (bb->num == 0)
219 return -1;
220
221 remloc = ext2fs_u32_list_find(bb, blk);
222 if (remloc < 0)
223 return -1;
224
225 for (i = remloc ; i < bb->num-1; i++)
226 bb->list[i] = bb->list[i+1];
227 bb->num--;
228 return 0;
229}
230
231void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk)
232{
233 ext2fs_u32_list_del(bb, blk);
234}
235
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400236errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
237 ext2_u32_iterate *ret)
238{
239 ext2_u32_iterate iter;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000240 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241
Theodore Ts'of3db3561997-04-26 13:34:30 +0000242 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
243
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400244 retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_iterate), &iter);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000245 if (retval)
246 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247
Theodore Ts'of3db3561997-04-26 13:34:30 +0000248 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249 iter->bb = bb;
250 iter->ptr = 0;
251 *ret = iter;
252 return 0;
253}
254
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400255errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
256 ext2_badblocks_iterate *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000257{
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400258 return ext2fs_u32_list_iterate_begin((ext2_u32_list) bb,
259 (ext2_u32_iterate *) ret);
260}
261
262
263int ext2fs_u32_list_iterate(ext2_u32_iterate iter, __u32 *blk)
264{
265 ext2_u32_list bb;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000266
267 if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
268 return 0;
269
270 bb = iter->bb;
271
272 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
273 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400274
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275 if (iter->ptr < bb->num) {
276 *blk = bb->list[iter->ptr++];
277 return 1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400278 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 *blk = 0;
280 return 0;
281}
282
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400283int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
284{
285 return ext2fs_u32_list_iterate((ext2_u32_iterate) iter,
286 (__u32 *) blk);
287}
288
289
290void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000292 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
293 return;
294
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 iter->bb = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400296 ext2fs_free_mem(&iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297}
Theodore Ts'o57dca852000-07-04 19:20:25 +0000298
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400299void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
300{
301 ext2fs_u32_list_iterate_end((ext2_u32_iterate) iter);
302}
303
304
305int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2)
Theodore Ts'o57dca852000-07-04 19:20:25 +0000306{
307 EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
308 EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
309
310 if (bb1->num != bb2->num)
311 return 0;
312
313 if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0)
314 return 0;
315 return 1;
316}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400317
318int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
319{
320 return ext2fs_u32_list_equal((ext2_u32_list) bb1,
321 (ext2_u32_list) bb2);
322}
Theodore Ts'o220c0042003-03-14 00:59:42 -0500323
324int ext2fs_u32_list_count(ext2_u32_list bb)
325{
326 return bb->num;
327}