blob: fba2d997b69b79f9811163f1fd61d0281f9c9a20 [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'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'o7b4e4531997-10-26 03:41:24 +000037 errcode_t retval;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000038
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000039 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_list),
40 (void **) &bb);
41 if (retval)
42 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000043 memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'of3db3561997-04-26 13:34:30 +000044 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
Theodore Ts'o3839e651997-04-26 13:21:57 +000045 bb->size = size ? size : 10;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000046 bb->num = num;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000047 retval = ext2fs_get_mem(bb->size * sizeof(blk_t), (void **) &bb->list);
Theodore Ts'o3839e651997-04-26 13:21:57 +000048 if (!bb->list) {
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000049 ext2fs_free_mem((void **) &bb);
50 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000051 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000052 if (list)
53 memcpy(bb->list, list, bb->size * sizeof(blk_t));
54 else
55 memset(bb->list, 0, bb->size * sizeof(blk_t));
Theodore Ts'o3839e651997-04-26 13:21:57 +000056 *ret = bb;
57 return 0;
58}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000059
60
61/*
62 * This procedure creates an empty badblocks list.
63 */
64errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
65{
66 return make_badblocks_list(size, 0, 0, ret);
67}
68
69/*
70 * This procedure copies a badblocks list
71 */
72errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
73 ext2_badblocks_list *dest)
74{
75 errcode_t retval;
76
77 retval = make_badblocks_list(src->size, src->num, src->list,
78 dest);
79 if (retval)
80 return retval;
81 (*dest)->badblocks_flags = src->badblocks_flags;
82 return 0;
83}
84
Theodore Ts'o3839e651997-04-26 13:21:57 +000085
86/*
87 * This procedure frees a badblocks list.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000088 *
89 * (note: moved to closefs.c)
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 */
Theodore Ts'of3db3561997-04-26 13:34:30 +000091
Theodore Ts'o3839e651997-04-26 13:21:57 +000092
93/*
94 * This procedure adds a block to a badblocks list.
95 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000096errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +000097{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000098 errcode_t retval;
99 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100
Theodore Ts'of3db3561997-04-26 13:34:30 +0000101 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
102
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 if (bb->num >= bb->size) {
104 bb->size += 10;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000105 retval = ext2fs_resize_mem(bb->size * sizeof(blk_t),
106 (void **) &bb->list);
107 if (retval)
108 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 }
110
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000111 j = bb->num;
112 for (i=0; i < bb->num; i++) {
113 if (bb->list[i] == blk)
114 return 0;
115 if (bb->list[i] > blk) {
116 j = i;
117 break;
118 }
119 }
120 for (i=bb->num; i > j; i--)
121 bb->list[i] = bb->list[i-1];
122 bb->list[j] = blk;
123 bb->num++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 return 0;
125}
126
127/*
128 * This procedure tests to see if a particular block is on a badblocks
129 * list.
130 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000131int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000133 int low, high, mid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000135 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
136 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000137
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000138 if (bb->num == 0)
139 return 0;
140
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000141 low = 0;
142 high = bb->num-1;
143 if (blk == bb->list[low])
144 return 1;
145 if (blk == bb->list[high])
146 return 1;
147
148 while (low < high) {
149 mid = (low+high)/2;
150 if (mid == low || mid == high)
151 break;
152 if (blk == bb->list[mid])
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000154 if (blk < bb->list[mid])
155 high = mid;
156 else
157 low = mid;
158 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 return 0;
160}
161
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000162errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
163 ext2_badblocks_iterate *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000165 ext2_badblocks_iterate iter;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000166 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167
Theodore Ts'of3db3561997-04-26 13:34:30 +0000168 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
169
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000170 retval = ext2fs_get_mem(sizeof(struct ext2_struct_badblocks_iterate),
171 (void **) &iter);
172 if (retval)
173 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174
Theodore Ts'of3db3561997-04-26 13:34:30 +0000175 iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176 iter->bb = bb;
177 iter->ptr = 0;
178 *ret = iter;
179 return 0;
180}
181
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000182int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000184 ext2_badblocks_list bb;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000185
186 if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
187 return 0;
188
189 bb = iter->bb;
190
191 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
192 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193
194 if (iter->ptr < bb->num) {
195 *blk = bb->list[iter->ptr++];
196 return 1;
197 }
198 *blk = 0;
199 return 0;
200}
201
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000202void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000204 if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
205 return;
206
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 iter->bb = 0;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000208 ext2fs_free_mem((void **) &iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209}
210
211
212
213
214