blob: 38e9f6e7ca6ef8f3a3d642ea686999ee80f018e0 [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'o50e1e101997-04-26 13:58:21 +000026#if HAVE_ERRNO_H
27#include <errno.h>
28#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000029
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <linux/ext2_fs.h>
31
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'oa29f4d31997-04-29 21:26:48 +000041
Theodore Ts'o21c84b71997-04-29 16:15:03 +000042 bb = malloc(sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 if (!bb)
44 return ENOMEM;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000045 memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
Theodore Ts'of3db3561997-04-26 13:34:30 +000046 bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
Theodore Ts'o3839e651997-04-26 13:21:57 +000047 bb->size = size ? size : 10;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000048 bb->num = num;
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 bb->list = malloc(bb->size * sizeof(blk_t));
50 if (!bb->list) {
51 free(bb);
52 return ENOMEM;
53 }
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000054 if (list)
55 memcpy(bb->list, list, bb->size * sizeof(blk_t));
56 else
57 memset(bb->list, 0, bb->size * sizeof(blk_t));
Theodore Ts'o3839e651997-04-26 13:21:57 +000058 *ret = bb;
59 return 0;
60}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000061
62
63/*
64 * This procedure creates an empty badblocks list.
65 */
66errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
67{
68 return make_badblocks_list(size, 0, 0, ret);
69}
70
71/*
72 * This procedure copies a badblocks list
73 */
74errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
75 ext2_badblocks_list *dest)
76{
77 errcode_t retval;
78
79 retval = make_badblocks_list(src->size, src->num, src->list,
80 dest);
81 if (retval)
82 return retval;
83 (*dest)->badblocks_flags = src->badblocks_flags;
84 return 0;
85}
86
Theodore Ts'o3839e651997-04-26 13:21:57 +000087
88/*
89 * This procedure frees a badblocks list.
Theodore Ts'o21c84b71997-04-29 16:15:03 +000090 *
91 * (note: moved to closefs.c)
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 */
Theodore Ts'of3db3561997-04-26 13:34:30 +000093
Theodore Ts'o3839e651997-04-26 13:21:57 +000094
95/*
96 * This procedure adds a block to a badblocks list.
97 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000098errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +000099{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000100 int i, j;
101 blk_t *new_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102
Theodore Ts'of3db3561997-04-26 13:34:30 +0000103 EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
104
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105 if (bb->num >= bb->size) {
106 bb->size += 10;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000107 new_list = realloc(bb->list, bb->size * sizeof(blk_t));
108 if (!new_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 return ENOMEM;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000110 bb->list = new_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 }
112
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000113 j = bb->num;
114 for (i=0; i < bb->num; i++) {
115 if (bb->list[i] == blk)
116 return 0;
117 if (bb->list[i] > blk) {
118 j = i;
119 break;
120 }
121 }
122 for (i=bb->num; i > j; i--)
123 bb->list[i] = bb->list[i-1];
124 bb->list[j] = blk;
125 bb->num++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 return 0;
127}
128
129/*
130 * This procedure tests to see if a particular block is on a badblocks
131 * list.
132 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000133int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000135 int low, high, mid;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000137 if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
138 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000139
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000140 if (bb->num == 0)
141 return 0;
142
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000143 low = 0;
144 high = bb->num-1;
145 if (blk == bb->list[low])
146 return 1;
147 if (blk == bb->list[high])
148 return 1;
149
150 while (low < high) {
151 mid = (low+high)/2;
152 if (mid == low || mid == high)
153 break;
154 if (blk == bb->list[mid])
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000156 if (blk < bb->list[mid])
157 high = mid;
158 else
159 low = mid;
160 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 return 0;
162}
163
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000164errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
165 ext2_badblocks_iterate *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000167 ext2_badblocks_iterate iter;
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'o21c84b71997-04-29 16:15:03 +0000171 iter = malloc(sizeof(struct ext2_struct_badblocks_iterate));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 if (!iter)
173 return ENOMEM;
174
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;
208 free(iter);
209}
210
211
212
213
214