Coverage Report

Created: 2026-06-10 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/e2fsprogs/lib/ext2fs/badblocks.c
Line
Count
Source
1
/*
2
 * badblocks.c --- routines to manipulate the bad block structure
3
 *
4
 * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
5
 *
6
 * %Begin-Header%
7
 * This file may be redistributed under the terms of the GNU Library
8
 * General Public License, version 2.
9
 * %End-Header%
10
 */
11
12
#include "config.h"
13
#include <stdio.h>
14
#include <string.h>
15
#if HAVE_UNISTD_H
16
#include <unistd.h>
17
#endif
18
#include <fcntl.h>
19
#include <time.h>
20
#if HAVE_SYS_STAT_H
21
#include <sys/stat.h>
22
#endif
23
#if HAVE_SYS_TYPES_H
24
#include <sys/types.h>
25
#endif
26
27
#include "ext2_fs.h"
28
#include "ext2fsP.h"
29
30
/*
31
 * Helper function for making a badblocks list
32
 */
33
static errcode_t make_u32_list(int size, int num, __u32 *list,
34
             ext2_u32_list *ret)
35
0
{
36
0
  ext2_u32_list bb;
37
0
  errcode_t retval;
38
39
0
  retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_list), &bb);
40
0
  if (retval)
41
0
    return retval;
42
0
  memset(bb, 0, sizeof(struct ext2_struct_u32_list));
43
0
  bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
44
0
  bb->size = size ? size : 10;
45
0
  bb->num = num;
46
0
  retval = ext2fs_get_array(bb->size, sizeof(blk_t), &bb->list);
47
0
  if (retval) {
48
0
    ext2fs_free_mem(&bb);
49
0
    return retval;
50
0
  }
51
0
  if (list)
52
0
    memcpy(bb->list, list, bb->size * sizeof(blk_t));
53
0
  else
54
0
    memset(bb->list, 0, bb->size * sizeof(blk_t));
55
0
  *ret = bb;
56
0
  return 0;
57
0
}
58
59
60
/*
61
 * This procedure creates an empty u32 list.
62
 */
63
errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size)
64
0
{
65
0
  return make_u32_list(size, 0, 0, ret);
66
0
}
67
68
/*
69
 * This procedure creates an empty badblocks list.
70
 */
71
errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
72
0
{
73
0
  return make_u32_list(size, 0, 0, (ext2_badblocks_list *) ret);
74
0
}
75
76
77
/*
78
 * This procedure copies a badblocks list
79
 */
80
errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest)
81
0
{
82
0
  errcode_t retval;
83
84
0
  retval = make_u32_list(src->size, src->num, src->list, dest);
85
0
  if (retval)
86
0
    return retval;
87
0
  (*dest)->badblocks_flags = src->badblocks_flags;
88
0
  return 0;
89
0
}
90
91
errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
92
        ext2_badblocks_list *dest)
93
0
{
94
0
  return ext2fs_u32_copy((ext2_u32_list) src,
95
0
             (ext2_u32_list *) dest);
96
0
}
97
98
/*
99
 * This procedure frees a badblocks list.
100
 *
101
 * (note: moved to closefs.c)
102
 */
103
104
105
/*
106
 * This procedure adds an item to a tracking list (e.g. badblocks or casefold).
107
 */
108
errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk)
109
0
{
110
0
  errcode_t retval;
111
0
  int   i, j;
112
0
  unsigned long old_size;
113
114
0
  EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
115
116
0
  if (bb->num >= bb->size) {
117
0
    old_size = bb->size * sizeof(__u32);
118
0
    bb->size += 100;
119
0
    retval = ext2fs_resize_mem(old_size, bb->size * sizeof(__u32),
120
0
             &bb->list);
121
0
    if (retval) {
122
0
      bb->size -= 100;
123
0
      return retval;
124
0
    }
125
0
  }
126
127
  /*
128
   * Add special case code for appending to the end of the list
129
   */
130
0
  i = bb->num-1;
131
0
  if ((bb->num != 0) && (bb->list[i] == blk))
132
0
    return 0;
133
0
  if ((bb->num == 0) || (bb->list[i] < blk)) {
134
0
    bb->list[bb->num++] = blk;
135
0
    return 0;
136
0
  }
137
138
0
  j = bb->num;
139
0
  for (i=0; i < bb->num; i++) {
140
0
    if (bb->list[i] == blk)
141
0
      return 0;
142
0
    if (bb->list[i] > blk) {
143
0
      j = i;
144
0
      break;
145
0
    }
146
0
  }
147
0
  for (i=bb->num; i > j; i--)
148
0
    bb->list[i] = bb->list[i-1];
149
0
  bb->list[j] = blk;
150
0
  bb->num++;
151
0
  return 0;
152
0
}
153
154
errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
155
0
{
156
0
  return ext2fs_u32_list_add((ext2_u32_list) bb, (__u32) blk);
157
0
}
158
159
/*
160
 * This procedure finds a particular block is on a badblocks
161
 * list.
162
 */
163
int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk)
164
0
{
165
0
  int low, high, mid;
166
167
0
  if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
168
0
    return -1;
169
170
0
  if (bb->num == 0)
171
0
    return -1;
172
173
0
  low = 0;
174
0
  high = bb->num-1;
175
0
  if (blk == bb->list[low])
176
0
    return low;
177
0
  if (blk == bb->list[high])
178
0
    return high;
179
180
0
  while (low < high) {
181
0
    mid = ((unsigned)low + (unsigned)high)/2;
182
0
    if (mid == low || mid == high)
183
0
      break;
184
0
    if (blk == bb->list[mid])
185
0
      return mid;
186
0
    if (blk < bb->list[mid])
187
0
      high = mid;
188
0
    else
189
0
      low = mid;
190
0
  }
191
0
  return -1;
192
0
}
193
194
/*
195
 * This procedure tests to see if a particular block is on a badblocks
196
 * list.
197
 */
198
int ext2fs_u32_list_test(ext2_u32_list bb, __u32 blk)
199
0
{
200
0
  if (ext2fs_u32_list_find(bb, blk) < 0)
201
0
    return 0;
202
0
  else
203
0
    return 1;
204
0
}
205
206
int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
207
0
{
208
0
  return ext2fs_u32_list_test((ext2_u32_list) bb, (__u32) blk);
209
0
}
210
211
212
/*
213
 * Remove a block from the badblock list
214
 */
215
int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk)
216
0
{
217
0
  int remloc, i;
218
219
0
  if (bb->num == 0)
220
0
    return -1;
221
222
0
  remloc = ext2fs_u32_list_find(bb, blk);
223
0
  if (remloc < 0)
224
0
    return -1;
225
226
0
  for (i = remloc ; i < bb->num-1; i++)
227
0
    bb->list[i] = bb->list[i+1];
228
0
  bb->num--;
229
0
  return 0;
230
0
}
231
232
void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk)
233
0
{
234
0
  ext2fs_u32_list_del(bb, blk);
235
0
}
236
237
errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
238
          ext2_u32_iterate *ret)
239
0
{
240
0
  ext2_u32_iterate iter;
241
0
  errcode_t   retval;
242
243
0
  EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
244
245
0
  retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_iterate), &iter);
246
0
  if (retval)
247
0
    return retval;
248
249
0
  iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
250
0
  iter->bb = bb;
251
0
  iter->ptr = 0;
252
0
  *ret = iter;
253
0
  return 0;
254
0
}
255
256
errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
257
                ext2_badblocks_iterate *ret)
258
0
{
259
0
  return ext2fs_u32_list_iterate_begin((ext2_u32_list) bb,
260
0
                (ext2_u32_iterate *) ret);
261
0
}
262
263
264
int ext2fs_u32_list_iterate(ext2_u32_iterate iter, __u32 *blk)
265
0
{
266
0
  ext2_u32_list bb;
267
268
0
  if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
269
0
    return 0;
270
271
0
  bb = iter->bb;
272
273
0
  if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
274
0
    return 0;
275
276
0
  if (iter->ptr < bb->num) {
277
0
    *blk = bb->list[iter->ptr++];
278
0
    return 1;
279
0
  }
280
0
  *blk = 0;
281
0
  return 0;
282
0
}
283
284
int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
285
0
{
286
0
  return ext2fs_u32_list_iterate((ext2_u32_iterate) iter,
287
0
               (__u32 *) blk);
288
0
}
289
290
291
void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter)
292
0
{
293
0
  if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
294
0
    return;
295
296
0
  iter->bb = 0;
297
0
  ext2fs_free_mem(&iter);
298
0
}
299
300
void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
301
0
{
302
0
  ext2fs_u32_list_iterate_end((ext2_u32_iterate) iter);
303
0
}
304
305
306
int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2)
307
0
{
308
0
  EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
309
0
  EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
310
311
0
  if (bb1->num != bb2->num)
312
0
    return 0;
313
314
0
  if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0)
315
0
    return 0;
316
0
  return 1;
317
0
}
318
319
int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
320
0
{
321
0
  return ext2fs_u32_list_equal((ext2_u32_list) bb1,
322
0
             (ext2_u32_list) bb2);
323
0
}
324
325
int ext2fs_u32_list_count(ext2_u32_list bb)
326
0
{
327
0
  return bb->num;
328
0
}