blob: ab1404dd3555b79c897227490c85df810df7610c [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>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <linux/ext2_fs.h>
28
Theodore Ts'o21c84b71997-04-29 16:15:03 +000029#include "ext2fsP.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000030
31/*
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000032 * Helper function for making a badblocks list
Theodore Ts'o3839e651997-04-26 13:21:57 +000033 */
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000034static errcode_t make_badblocks_list(int size, int num, blk_t *list,
35 ext2_badblocks_list *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000036{
Theodore Ts'o21c84b71997-04-29 16:15:03 +000037 ext2_badblocks_list bb;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000038 errcode_t retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000039
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000040 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list),
41 (void **) &bb);
42 if (retval)
43 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000044 memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'of3db3561997-04-26 13:34:30 +000045 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
Theodore Ts'o3839e651997-04-26 13:21:57 +000046 bb->size = size ? size : 10;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000047 bb->num = num;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000048 retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list);
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 if (!bb->list) {
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000050 ext2fs_free_mem((void **) &bb);
51 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000053 if (list)
54 memcpy(bb->list, list, bb->size * sizeof(blk_t));
55 else
56 memset(bb->list, 0, bb->size * sizeof(blk_t));
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 *ret = bb;
58 return 0;
59}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000060
61
62/*
63 * This procedure creates an empty badblocks list.
64 */
65errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
66{
67 return make_badblocks_list(size, 0, 0, ret);
68}
69
70/*
71 * This procedure copies a badblocks list
72 */
73errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
74 ext2_badblocks_list *dest)
75{
76 errcode_t retval;
77
78 retval = make_badblocks_list(src->size, src->num, src->list,
79 dest);
80 if (retval)
81 return retval;
82 (*dest)->badblocks_flags = src->badblocks_flags;
83 return 0;
84}
85
Theodore Ts'o3839e651997-04-26 13:21:57 +000086
87/*
88 * This procedure frees a badblocks list.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000089 *
90 * (note: moved to closefs.c)
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 */
Theodore Ts'of3db3561997-04-26 13:34:30 +000092
Theodore Ts'o3839e651997-04-26 13:21:57 +000093
94/*
95 * This procedure adds a block to a badblocks list.
96 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000097errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +000098{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000099 errcode_t retval;
100 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101
Theodore Ts'of3db3561997-04-26 13:34:30 +0000102 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
103
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104 if (bb->num >= bb->size) {
105 bb->size += 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000106 retval = ext2fs_resize_mem(bb->size * sizeof(blk_t),
107 (void **) &bb->list);
108 if (retval)
109 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 }
111
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000112 j = bb->num;
113 for (i=0; i < bb->num; i++) {
114 if (bb->list[i] == blk)
115 return 0;
116 if (bb->list[i] > blk) {
117 j = i;
118 break;
119 }
120 }
121 for (i=bb->num; i > j; i--)
122 bb->list[i] = bb->list[i-1];
123 bb->list[j] = blk;
124 bb->num++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 return 0;
126}
127
128/*
129 * This procedure tests to see if a particular block is on a badblocks
130 * list.
131 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000132int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000134 int low, high, mid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000136 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
137 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000138
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000139 if (bb->num == 0)
140 return 0;
141
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000142 low = 0;
143 high = bb->num-1;
144 if (blk == bb->list[low])
145 return 1;
146 if (blk == bb->list[high])
147 return 1;
148
149 while (low < high) {
150 mid = (low+high)/2;
151 if (mid == low || mid == high)
152 break;
153 if (blk == bb->list[mid])
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000155 if (blk < bb->list[mid])
156 high = mid;
157 else
158 low = mid;
159 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 return 0;
161}
162
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000163errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
164 ext2_badblocks_iterate *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000166 ext2_badblocks_iterate iter;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000167 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168
Theodore Ts'of3db3561997-04-26 13:34:30 +0000169 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
170
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000171 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate),
172 (void **) &iter);
173 if (retval)
174 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175
Theodore Ts'of3db3561997-04-26 13:34:30 +0000176 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 iter->bb = bb;
178 iter->ptr = 0;
179 *ret = iter;
180 return 0;
181}
182
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000183int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000185 ext2_badblocks_list bb;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000186
187 if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
188 return 0;
189
190 bb = iter->bb;
191
192 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
193 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194
195 if (iter->ptr < bb->num) {
196 *blk = bb->list[iter->ptr++];
197 return 1;
198 }
199 *blk = 0;
200 return 0;
201}
202
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000203void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000205 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
206 return;
207
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 iter->bb = 0;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000209 ext2fs_free_mem((void **) &iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210}
211
212
213
214
215