e2fsprogs/lib/ext2fs/badblocks.c

329 lines
6.4 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 Library
* General Public License, version 2.
1997-04-29 20:15:03 +04:00
* %End-Header%
1997-04-26 17:21:57 +04:00
*/
#include "config.h"
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 <fcntl.h>
#include <time.h>
#if HAVE_SYS_STAT_H
1997-04-26 17:21:57 +04:00
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
1997-04-26 17:21:57 +04:00
#include <sys/types.h>
#endif
1997-04-26 17:21:57 +04:00
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
#include "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
*/
static errcode_t make_u32_list(int size, int num, __u32 *list,
ext2_u32_list *ret)
1997-04-26 17:21:57 +04:00
{
ext2_u32_list bb;
errcode_t retval;
retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_list), &bb);
if (retval)
return retval;
memset(bb, 0, sizeof(struct ext2_struct_u32_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;
retval = ext2fs_get_array(bb->size, sizeof(blk_t), &bb->list);
if (retval) {
ext2fs_free_mem(&bb);
return retval;
1997-04-26 17:21:57 +04:00
}
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 u32 list.
*/
errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size)
{
return make_u32_list(size, 0, 0, ret);
}
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_u32_list(size, 0, 0, (ext2_badblocks_list *) ret);
1997-04-30 01:26:48 +04:00
}
1997-04-30 01:26:48 +04:00
/*
* This procedure copies a badblocks list
*/
errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest)
1997-04-30 01:26:48 +04:00
{
errcode_t retval;
retval = make_u32_list(src->size, src->num, src->list, dest);
1997-04-30 01:26:48 +04:00
if (retval)
return retval;
(*dest)->badblocks_flags = src->badblocks_flags;
return 0;
}
errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
ext2_badblocks_list *dest)
{
return ext2fs_u32_copy((ext2_u32_list) src,
(ext2_u32_list *) dest);
}
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.
*/
errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk)
1997-04-26 17:21:57 +04:00
{
errcode_t retval;
int i, j;
unsigned long old_size;
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) {
old_size = bb->size * sizeof(__u32);
bb->size += 100;
retval = ext2fs_resize_mem(old_size, bb->size * sizeof(__u32),
&bb->list);
if (retval) {
bb->size -= 100;
return retval;
}
1997-04-26 17:21:57 +04:00
}
/*
* Add special case code for appending to the end of the list
*/
i = bb->num-1;
if ((bb->num != 0) && (bb->list[i] == blk))
return 0;
if ((bb->num == 0) || (bb->list[i] < blk)) {
bb->list[bb->num++] = blk;
return 0;
}
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;
}
errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb, blk_t blk)
{
return ext2fs_u32_list_add((ext2_u32_list) bb, (__u32) blk);
}
1997-04-26 17:21:57 +04:00
/*
* This procedure finds a particular block is on a badblocks
1997-04-26 17:21:57 +04:00
* list.
*/
int ext2fs_u32_list_find(ext2_u32_list bb, __u32 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 -1;
1997-04-26 17:34:30 +04:00
if (bb->num == 0)
return -1;
1997-04-29 20:15:03 +04:00
low = 0;
high = bb->num-1;
if (blk == bb->list[low])
return low;
1997-04-29 20:15:03 +04:00
if (blk == bb->list[high])
return high;
1997-04-26 17:21:57 +04:00
1997-04-29 20:15:03 +04:00
while (low < high) {
mid = ((unsigned)low + (unsigned)high)/2;
1997-04-29 20:15:03 +04:00
if (mid == low || mid == high)
break;
if (blk == bb->list[mid])
return mid;
1997-04-29 20:15:03 +04:00
if (blk < bb->list[mid])
high = mid;
else
low = mid;
}
return -1;
}
/*
* This procedure tests to see if a particular block is on a badblocks
* list.
*/
int ext2fs_u32_list_test(ext2_u32_list bb, __u32 blk)
{
if (ext2fs_u32_list_find(bb, blk) < 0)
return 0;
else
return 1;
1997-04-26 17:21:57 +04:00
}
int ext2fs_badblocks_list_test(ext2_badblocks_list bb, blk_t blk)
{
return ext2fs_u32_list_test((ext2_u32_list) bb, (__u32) blk);
}
/*
* Remove a block from the badblock list
*/
int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk)
{
int remloc, i;
if (bb->num == 0)
return -1;
remloc = ext2fs_u32_list_find(bb, blk);
if (remloc < 0)
return -1;
for (i = remloc ; i < bb->num-1; i++)
bb->list[i] = bb->list[i+1];
bb->num--;
return 0;
}
void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk)
{
ext2fs_u32_list_del(bb, blk);
}
errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
ext2_u32_iterate *ret)
1997-04-26 17:21:57 +04:00
{
ext2_u32_iterate iter;
errcode_t retval;
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);
retval = ext2fs_get_mem(sizeof(struct ext2_struct_u32_iterate), &iter);
if (retval)
return retval;
1997-04-26 17:21:57 +04:00
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;
}
errcode_t ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
ext2_badblocks_iterate *ret)
1997-04-26 17:21:57 +04:00
{
return ext2fs_u32_list_iterate_begin((ext2_u32_list) bb,
(ext2_u32_iterate *) ret);
}
int ext2fs_u32_list_iterate(ext2_u32_iterate iter, __u32 *blk)
{
ext2_u32_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;
}
1997-04-26 17:21:57 +04:00
*blk = 0;
return 0;
}
int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter, blk_t *blk)
{
return ext2fs_u32_list_iterate((ext2_u32_iterate) iter,
(__u32 *) blk);
}
void ext2fs_u32_list_iterate_end(ext2_u32_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;
ext2fs_free_mem(&iter);
1997-04-26 17:21:57 +04:00
}
void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter)
{
ext2fs_u32_list_iterate_end((ext2_u32_iterate) iter);
}
int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2)
{
EXT2_CHECK_MAGIC(bb1, EXT2_ET_MAGIC_BADBLOCKS_LIST);
EXT2_CHECK_MAGIC(bb2, EXT2_ET_MAGIC_BADBLOCKS_LIST);
if (bb1->num != bb2->num)
return 0;
if (memcmp(bb1->list, bb2->list, bb1->num * sizeof(blk_t)) != 0)
return 0;
return 1;
}
int ext2fs_badblocks_equal(ext2_badblocks_list bb1, ext2_badblocks_list bb2)
{
return ext2fs_u32_list_equal((ext2_u32_list) bb1,
(ext2_u32_list) bb2);
}
int ext2fs_u32_list_count(ext2_u32_list bb)
{
return bb->num;
}