e2fsprogs/lib/ext2fs/badblocks.c

211 lines
3.8 KiB
C
Raw Normal View History

1997-04-26 17:21:57 +04:00
/*
* badblocks.c --- routines to manipulate the bad block structure
*
1997-04-29 20:15:03 +04:00
* Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
1997-04-26 17:21:57 +04:00
*/
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
1997-04-26 17:21:57 +04:00
#include <unistd.h>
#endif
1997-04-26 17:21:57 +04:00
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
1997-04-26 17:58:21 +04:00
#if HAVE_ERRNO_H
#include <errno.h>
#endif
1997-04-26 17:21:57 +04:00
#include <linux/ext2_fs.h>
1997-04-29 20:15:03 +04:00
#include "ext2fsP.h"
1997-04-26 17:21:57 +04:00
/*
1997-04-30 01:26:48 +04:00
* Helper function for making a badblocks list
1997-04-26 17:21:57 +04:00
*/
1997-04-30 01:26:48 +04:00
static errcode_t make_badblocks_list(int size, int num, blk_t *list,
ext2_badblocks_list *ret)
1997-04-26 17:21:57 +04:00
{
1997-04-29 20:15:03 +04:00
ext2_badblocks_list bb;
1997-04-30 01:26:48 +04:00
1997-04-29 20:15:03 +04:00
bb = malloc(sizeof(struct ext2_struct_badblocks_list));
1997-04-26 17:21:57 +04:00
if (!bb)
return ENOMEM;
1997-04-29 20:15:03 +04:00
memset(bb, 0, sizeof(struct ext2_struct_badblocks_list));
1997-04-26 17:34:30 +04:00
bb->magic = EXT2_ET_MAGIC_BADBLOCKS_LIST;
1997-04-26 17:21:57 +04:00
bb->size = size ? size : 10;
1997-04-30 01:26:48 +04:00
bb->num = num;
1997-04-26 17:21:57 +04:00
bb->list = malloc(bb->size * sizeof(blk_t));
if (!bb->list) {
free(bb);
return ENOMEM;
}
1997-04-30 01:26:48 +04:00
if (list)
memcpy(bb->list, list, bb->size * sizeof(blk_t));
else
memset(bb->list, 0, bb->size * sizeof(blk_t));
1997-04-26 17:21:57 +04:00
*ret = bb;
return 0;
}
1997-04-30 01:26:48 +04:00
/*
* This procedure creates an empty badblocks list.
*/
errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret, int size)
{
return make_badblocks_list(size, 0, 0, ret);
}
/*
* This procedure copies a badblocks list
*/
errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
ext2_badblocks_list *dest)
{
errcode_t retval;
retval = make_badblocks_list(src->size, src->num, src->list,
dest);
if (retval)
return retval;
(*dest)->badblocks_flags = src->badblocks_flags;
return 0;
}
1997-04-26 17:21:57 +04:00
/*
* This procedure frees a badblocks list.
1997-04-29 20:15:03 +04:00
*
* (note: moved to closefs.c)
1997-04-26 17:21:57 +04:00
*/
1997-04-26 17:34:30 +04:00
1997-04-26 17:21:57 +04:00
/*
* This procedure adds a block to a badblocks list.
*/
1997-04-29 20:15:03 +04:00
errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
1997-04-26 17:21:57 +04:00
{
1997-04-29 20:15:03 +04:00
int i, j;
blk_t *new_list;
1997-04-26 17:21:57 +04:00
1997-04-26 17:34:30 +04:00
EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
1997-04-26 17:21:57 +04:00
if (bb->num >= bb->size) {
bb->size += 10;
1997-04-29 20:15:03 +04:00
new_list = realloc(bb->list, bb->size * sizeof(blk_t));
if (!new_list)
1997-04-26 17:21:57 +04:00
return ENOMEM;
1997-04-29 20:15:03 +04:00
bb->list = new_list;
1997-04-26 17:21:57 +04:00
}
1997-04-29 20:15:03 +04:00
j = bb->num;
for (i=0; i < bb->num; i++) {
if (bb->list[i] == blk)
return 0;
if (bb->list[i] > blk) {
j = i;
break;
}
}
for (i=bb->num; i > j; i--)
bb->list[i] = bb->list[i-1];
bb->list[j] = blk;
bb->num++;
1997-04-26 17:21:57 +04:00
return 0;
}
/*
* This procedure tests to see if a particular block is on a badblocks
* list.
*/
1997-04-29 20:15:03 +04:00
int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
1997-04-26 17:21:57 +04:00
{
1997-04-29 20:15:03 +04:00
int low, high, mid;
1997-04-26 17:21:57 +04:00
1997-04-29 20:15:03 +04:00
if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
return 0;
1997-04-26 17:34:30 +04:00
if (bb->num == 0)
return 0;
1997-04-29 20:15:03 +04:00
low = 0;
high = bb->num-1;
if (blk == bb->list[low])
return 1;
if (blk == bb->list[high])
return 1;
1997-04-26 17:21:57 +04:00
1997-04-29 20:15:03 +04:00
while (low < high) {
mid = (low+high)/2;
if (mid == low || mid == high)
break;
if (blk == bb->list[mid])
return 1;
if (blk < bb->list[mid])
high = mid;
else
low = mid;
}
1997-04-26 17:21:57 +04:00
return 0;
}
1997-04-29 20:15:03 +04:00
errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
ext2_badblocks_iterate *ret)
1997-04-26 17:21:57 +04:00
{
1997-04-29 20:15:03 +04:00
ext2_badblocks_iterate iter;
1997-04-26 17:21:57 +04:00
1997-04-26 17:34:30 +04:00
EXT2_CHECK_MAGIC(bb, EXT2_ET_MAGIC_BADBLOCKS_LIST);
1997-04-29 20:15:03 +04:00
iter = malloc(sizeof(struct ext2_struct_badblocks_iterate));
1997-04-26 17:21:57 +04:00
if (!iter)
return ENOMEM;
1997-04-26 17:34:30 +04:00
iter->magic = EXT2_ET_MAGIC_BADBLOCKS_ITERATE;
1997-04-26 17:21:57 +04:00
iter->bb = bb;
iter->ptr = 0;
*ret = iter;
return 0;
}
1997-04-29 20:15:03 +04:00
int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
1997-04-26 17:21:57 +04:00
{
1997-04-29 20:15:03 +04:00
ext2_badblocks_list bb;
1997-04-26 17:34:30 +04:00
if (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE)
return 0;
bb = iter->bb;
if (bb->magic != EXT2_ET_MAGIC_BADBLOCKS_LIST)
return 0;
1997-04-26 17:21:57 +04:00
if (iter->ptr < bb->num) {
*blk = bb->list[iter->ptr++];
return 1;
}
*blk = 0;
return 0;
}
1997-04-29 20:15:03 +04:00
void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
1997-04-26 17:21:57 +04:00
{
1997-04-26 17:34:30 +04:00
if (!iter || (iter->magic != EXT2_ET_MAGIC_BADBLOCKS_ITERATE))
return;
1997-04-26 17:21:57 +04:00
iter->bb = 0;
free(iter);
}