blob: 195c8be8686db86d388619ca34a487cfc00be80a [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 <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#if EXT2_FLAT_INCLUDES
27#include "ext2_fs.h"
28#else
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000030#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000031
Theodore Ts'o21c84b71997-04-29 16:15:03 +000032#include "ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000033
34/*
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000035 * Helper function for making a badblocks list
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000037static errcode_t make_badblocks_list(int size, int num, blk_t *list,
38 ext2_badblocks_list *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000039{
Theodore Ts'o21c84b71997-04-29 16:15:03 +000040 ext2_badblocks_list bb;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000041 errcode_t retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000042
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000043 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list),
44 (void **) &bb);
45 if (retval)
46 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000047 memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'of3db3561997-04-26 13:34:30 +000048 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 bb->size = size ? size : 10;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000050 bb->num = num;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000051 retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list);
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 if (!bb->list) {
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000053 ext2fs_free_mem((void **) &bb);
54 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000056 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'o3839e651997-04-26 13:21:57 +000060 *ret = bb;
61 return 0;
62}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000063
64
65/*
66 * This procedure creates an empty badblocks list.
67 */
68errcode_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 */
76errcode_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'o3839e651997-04-26 13:21:57 +000089
90/*
91 * This procedure frees a badblocks list.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000092 *
93 * (note: moved to closefs.c)
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 */
Theodore Ts'of3db3561997-04-26 13:34:30 +000095
Theodore Ts'o3839e651997-04-26 13:21:57 +000096
97/*
98 * This procedure adds a block to a badblocks list.
99 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000100errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000102 errcode_t retval;
103 int i, j;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000104 unsigned long old_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105
Theodore Ts'of3db3561997-04-26 13:34:30 +0000106 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
107
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108 if (bb->num >= bb->size) {
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000109 old_size = bb->size * sizeof(blk_t);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000110 bb->size += 100;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000111 retval = ext2fs_resize_mem(old_size, bb->size * sizeof(blk_t),
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000112 (void **) &bb->list);
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000113 if (retval) {
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000114 bb->size -= 100;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000115 return retval;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000116 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117 }
118
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +0000119 /*
120 * Add special case code for appending to the end of the list
121 */
122 i = bb->num-1;
123 if ((bb->num != 0) && (bb->list[i] == blk))
124 return 0;
125 if ((bb->num == 0) || (bb->list[i] < blk)) {
126 bb->list[bb->num++] = blk;
127 return 0;
128 }
129
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000130 j = bb->num;
131 for (i=0; i < bb->num; i++) {
132 if (bb->list[i] == blk)
133 return 0;
134 if (bb->list[i] > blk) {
135 j = i;
136 break;
137 }
138 }
139 for (i=bb->num; i > j; i--)
140 bb->list[i] = bb->list[i-1];
141 bb->list[j] = blk;
142 bb->num++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 return 0;
144}
145
146/*
147 * This procedure tests to see if a particular block is on a badblocks
148 * list.
149 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000150int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000152 int low, high, mid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000154 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
155 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000156
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000157 if (bb->num == 0)
158 return 0;
159
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000160 low = 0;
161 high = bb->num-1;
162 if (blk == bb->list[low])
163 return 1;
164 if (blk == bb->list[high])
165 return 1;
166
167 while (low < high) {
168 mid = (low+high)/2;
169 if (mid == low || mid == high)
170 break;
171 if (blk == bb->list[mid])
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000173 if (blk < bb->list[mid])
174 high = mid;
175 else
176 low = mid;
177 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 return 0;
179}
180
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000181errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
182 ext2_badblocks_iterate *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000184 ext2_badblocks_iterate iter;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000185 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186
Theodore Ts'of3db3561997-04-26 13:34:30 +0000187 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
188
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000189 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate),
190 (void **) &iter);
191 if (retval)
192 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193
Theodore Ts'of3db3561997-04-26 13:34:30 +0000194 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 iter->bb = bb;
196 iter->ptr = 0;
197 *ret = iter;
198 return 0;
199}
200
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000201int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000203 ext2_badblocks_list bb;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000204
205 if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
206 return 0;
207
208 bb = iter->bb;
209
210 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
211 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000212
213 if (iter->ptr < bb->num) {
214 *blk = bb->list[iter->ptr++];
215 return 1;
216 }
217 *blk = 0;
218 return 0;
219}
220
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000221void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000223 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
224 return;
225
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 iter->bb = 0;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000227 ext2fs_free_mem((void **) &iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228}
Theodore Ts'o57dca852000-07-04 19:20:25 +0000229
230int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
231{
232 EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
233 EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
234
235 if (bb1->num != bb2->num)
236 return 0;
237
238 if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0)
239 return 0;
240 return 1;
241}