1997-04-29 20:17:09 +04:00
|
|
|
/*
|
|
|
|
* This testing program makes sure the badblocks implementation works.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1996 by Theodore Ts'o.
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
1997-04-29 20:17:09 +04:00
|
|
|
* %Begin-Header%
|
2010-05-18 05:31:56 +04:00
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
1997-04-29 20:17:09 +04:00
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1997-08-11 03:07:40 +04:00
|
|
|
#if HAVE_UNISTD_H
|
1997-04-29 20:17:09 +04:00
|
|
|
#include <unistd.h>
|
1997-08-11 03:07:40 +04:00
|
|
|
#endif
|
1997-04-29 20:17:09 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
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:17:09 +04:00
|
|
|
#include "ext2fs.h"
|
|
|
|
|
2003-06-25 01:34:02 +04:00
|
|
|
#define ADD_BLK 0x0001
|
|
|
|
#define DEL_BLK 0x0002
|
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
|
2000-07-04 23:20:25 +04:00
|
|
|
blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
|
1999-11-09 01:05:04 +03:00
|
|
|
blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
|
1997-04-29 20:17:09 +04:00
|
|
|
blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
|
|
|
|
blk_t test4a[] = {
|
|
|
|
20, 1,
|
|
|
|
50, 1,
|
|
|
|
3, 0,
|
|
|
|
17, 1,
|
|
|
|
18, 0,
|
|
|
|
16, 0,
|
|
|
|
11, 0,
|
|
|
|
12, 1,
|
|
|
|
13, 1,
|
2008-08-28 07:07:54 +04:00
|
|
|
14, 0,
|
1997-04-29 20:17:09 +04:00
|
|
|
80, 0,
|
|
|
|
45, 0,
|
|
|
|
66, 1,
|
|
|
|
0 };
|
2003-06-25 01:34:02 +04:00
|
|
|
blk_t test5[] = { 31, 20, 17, 51, 23, 1, 56, 57, 0 };
|
|
|
|
blk_t test5a[] = {
|
|
|
|
50, ADD_BLK,
|
|
|
|
51, DEL_BLK,
|
|
|
|
57, DEL_BLK,
|
|
|
|
66, ADD_BLK,
|
|
|
|
31, DEL_BLK,
|
|
|
|
12, ADD_BLK,
|
|
|
|
2, ADD_BLK,
|
|
|
|
13, ADD_BLK,
|
|
|
|
1, DEL_BLK,
|
|
|
|
0
|
|
|
|
};
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
|
|
|
|
static int test_fail = 0;
|
2005-12-10 22:37:10 +03:00
|
|
|
static int test_expected_fail = 0;
|
1997-04-29 20:17:09 +04:00
|
|
|
|
|
|
|
static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
badblocks_list bb;
|
|
|
|
int i;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1999-11-09 01:05:04 +03:00
|
|
|
retval = ext2fs_badblocks_list_create(&bb, 5);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (retval) {
|
|
|
|
com_err("create_test_list", retval, "while creating list");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
for (i=0; vec[i]; i++) {
|
1999-11-09 01:05:04 +03:00
|
|
|
retval = ext2fs_badblocks_list_add(bb, vec[i]);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (retval) {
|
|
|
|
com_err("create_test_list", retval,
|
|
|
|
"while adding test vector %d", i);
|
1999-11-09 01:05:04 +03:00
|
|
|
ext2fs_badblocks_list_free(bb);
|
1997-04-29 20:17:09 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ret = bb;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_list(badblocks_list bb, int verify)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
badblocks_iterate iter;
|
|
|
|
blk_t blk;
|
|
|
|
int i, ok;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1999-11-09 01:05:04 +03:00
|
|
|
retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (retval) {
|
|
|
|
com_err("print_list", retval, "while setting up iterator");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ok = i = 1;
|
1999-11-09 01:05:04 +03:00
|
|
|
while (ext2fs_badblocks_list_iterate(iter, &blk)) {
|
2006-09-12 22:56:15 +04:00
|
|
|
printf("%u ", blk);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (i++ != blk)
|
|
|
|
ok = 0;
|
|
|
|
}
|
1999-11-09 01:05:04 +03:00
|
|
|
ext2fs_badblocks_list_iterate_end(iter);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (verify) {
|
|
|
|
if (ok)
|
|
|
|
printf("--- OK");
|
|
|
|
else {
|
|
|
|
printf("--- NOT OK");
|
|
|
|
test_fail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void validate_test_seq(badblocks_list bb, blk_t *vec)
|
|
|
|
{
|
|
|
|
int i, match, ok;
|
|
|
|
|
|
|
|
for (i = 0; vec[i]; i += 2) {
|
1999-11-09 01:05:04 +03:00
|
|
|
match = ext2fs_badblocks_list_test(bb, vec[i]);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (match == vec[i+1])
|
|
|
|
ok = 1;
|
|
|
|
else {
|
|
|
|
ok = 0;
|
|
|
|
test_fail++;
|
|
|
|
}
|
2006-09-12 22:56:15 +04:00
|
|
|
printf("\tblock %u is %s --- %s\n", vec[i],
|
1997-04-29 20:17:09 +04:00
|
|
|
match ? "present" : "absent",
|
|
|
|
ok ? "OK" : "NOT OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-25 01:34:02 +04:00
|
|
|
static void do_test_seq(badblocks_list bb, blk_t *vec)
|
|
|
|
{
|
|
|
|
int i, match;
|
|
|
|
|
|
|
|
for (i = 0; vec[i]; i += 2) {
|
|
|
|
switch (vec[i+1]) {
|
|
|
|
case ADD_BLK:
|
|
|
|
ext2fs_badblocks_list_add(bb, vec[i]);
|
|
|
|
match = ext2fs_badblocks_list_test(bb, vec[i]);
|
2006-09-12 22:56:15 +04:00
|
|
|
printf("Adding block %u --- now %s\n", vec[i],
|
2003-06-25 01:34:02 +04:00
|
|
|
match ? "present" : "absent");
|
|
|
|
if (!match) {
|
|
|
|
printf("FAILURE!\n");
|
|
|
|
test_fail++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DEL_BLK:
|
|
|
|
ext2fs_badblocks_list_del(bb, vec[i]);
|
|
|
|
match = ext2fs_badblocks_list_test(bb, vec[i]);
|
2006-09-12 22:56:15 +04:00
|
|
|
printf("Removing block %u --- now %s\n", vec[i],
|
2008-08-28 07:07:54 +04:00
|
|
|
ext2fs_badblocks_list_test(bb, vec[i]) ?
|
2003-06-25 01:34:02 +04:00
|
|
|
"present" : "absent");
|
|
|
|
if (match) {
|
|
|
|
printf("FAILURE!\n");
|
|
|
|
test_fail++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-04 23:20:25 +04:00
|
|
|
int file_test(badblocks_list bb)
|
|
|
|
{
|
|
|
|
badblocks_list new_bb = 0;
|
|
|
|
errcode_t retval;
|
|
|
|
FILE *f;
|
|
|
|
|
2003-05-04 00:39:50 +04:00
|
|
|
f = tmpfile();
|
2000-07-04 23:20:25 +04:00
|
|
|
if (!f) {
|
2003-05-04 00:39:50 +04:00
|
|
|
fprintf(stderr, "Error opening temp file: %s\n",
|
|
|
|
error_message(errno));
|
2000-07-04 23:20:25 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
retval = ext2fs_write_bb_FILE(bb, 0, f);
|
|
|
|
if (retval) {
|
|
|
|
com_err("file_test", retval, "while writing bad blocks");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-05-04 00:39:50 +04:00
|
|
|
rewind(f);
|
2000-07-04 23:20:25 +04:00
|
|
|
retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
|
|
|
|
if (retval) {
|
|
|
|
com_err("file_test", retval, "while reading bad blocks");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (ext2fs_badblocks_equal(bb, new_bb)) {
|
|
|
|
printf("Block bitmap matched after reading and writing.\n");
|
|
|
|
} else {
|
|
|
|
printf("Block bitmap NOT matched.\n");
|
|
|
|
test_fail++;
|
|
|
|
}
|
2002-03-08 07:52:56 +03:00
|
|
|
return 0;
|
2000-07-04 23:20:25 +04:00
|
|
|
}
|
|
|
|
|
2005-12-10 22:37:10 +03:00
|
|
|
static void invalid_proc(ext2_filsys fs, blk_t blk)
|
|
|
|
{
|
|
|
|
if (blk == 34500) {
|
|
|
|
printf("Expected invalid block\n");
|
|
|
|
test_expected_fail++;
|
|
|
|
} else {
|
2006-09-12 22:56:15 +04:00
|
|
|
printf("Invalid block #: %u\n", blk);
|
2005-12-10 22:37:10 +03:00
|
|
|
test_fail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_test_invalid(badblocks_list bb)
|
|
|
|
{
|
|
|
|
badblocks_list new_bb = 0;
|
|
|
|
errcode_t retval;
|
|
|
|
ext2_filsys fs;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
fs = malloc(sizeof(struct struct_ext2_filsys));
|
|
|
|
memset(fs, 0, sizeof(struct struct_ext2_filsys));
|
|
|
|
fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
|
|
|
|
fs->super = malloc(SUPERBLOCK_SIZE);
|
|
|
|
memset(fs->super, 0, SUPERBLOCK_SIZE);
|
|
|
|
fs->super->s_first_data_block = 1;
|
|
|
|
fs->super->s_blocks_count = 100;
|
|
|
|
|
|
|
|
f = tmpfile();
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "Error opening temp file: %s\n",
|
|
|
|
error_message(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
retval = ext2fs_write_bb_FILE(bb, 0, f);
|
|
|
|
if (retval) {
|
|
|
|
com_err("file_test", retval, "while writing bad blocks");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(f, "34500\n");
|
|
|
|
|
|
|
|
rewind(f);
|
|
|
|
test_expected_fail = 0;
|
|
|
|
retval = ext2fs_read_bb_FILE(fs, f, &new_bb, invalid_proc);
|
|
|
|
if (retval) {
|
|
|
|
com_err("file_test", retval, "while reading bad blocks");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
if (!test_expected_fail) {
|
|
|
|
printf("Expected test failure didn't happen!\n");
|
|
|
|
test_fail++;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2005-12-10 22:37:10 +03:00
|
|
|
|
|
|
|
if (ext2fs_badblocks_equal(bb, new_bb)) {
|
|
|
|
printf("Block bitmap matched after reading and writing.\n");
|
|
|
|
} else {
|
|
|
|
printf("Block bitmap NOT matched.\n");
|
|
|
|
test_fail++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2000-07-04 23:20:25 +04:00
|
|
|
|
2002-03-08 07:52:56 +03:00
|
|
|
int main(int argc, char **argv)
|
1997-04-29 20:17:09 +04:00
|
|
|
{
|
2003-06-25 01:34:02 +04:00
|
|
|
badblocks_list bb1, bb2, bb3, bb4, bb5;
|
2000-07-04 23:20:25 +04:00
|
|
|
int equal;
|
1997-04-29 20:17:09 +04:00
|
|
|
errcode_t retval;
|
|
|
|
|
2009-09-09 05:33:03 +04:00
|
|
|
add_error_table(&et_ext2_error_table);
|
|
|
|
|
2003-06-25 01:34:02 +04:00
|
|
|
bb1 = bb2 = bb3 = bb4 = bb5 = 0;
|
2000-07-04 23:20:25 +04:00
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("test1: ");
|
2000-07-04 23:20:25 +04:00
|
|
|
retval = create_test_list(test1, &bb1);
|
|
|
|
if (retval == 0)
|
|
|
|
print_list(bb1, 1);
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("\n");
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("test2: ");
|
2000-07-04 23:20:25 +04:00
|
|
|
retval = create_test_list(test2, &bb2);
|
|
|
|
if (retval == 0)
|
|
|
|
print_list(bb2, 1);
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("test3: ");
|
2000-07-04 23:20:25 +04:00
|
|
|
retval = create_test_list(test3, &bb3);
|
|
|
|
if (retval == 0)
|
|
|
|
print_list(bb3, 1);
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("\n");
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("test4: ");
|
2000-07-04 23:20:25 +04:00
|
|
|
retval = create_test_list(test4, &bb4);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (retval == 0) {
|
2000-07-04 23:20:25 +04:00
|
|
|
print_list(bb4, 0);
|
1997-04-29 20:17:09 +04:00
|
|
|
printf("\n");
|
2000-07-04 23:20:25 +04:00
|
|
|
validate_test_seq(bb4, test4a);
|
1997-04-29 20:17:09 +04:00
|
|
|
}
|
|
|
|
printf("\n");
|
2000-07-04 23:20:25 +04:00
|
|
|
|
2003-06-25 01:34:02 +04:00
|
|
|
printf("test5: ");
|
|
|
|
retval = create_test_list(test5, &bb5);
|
|
|
|
if (retval == 0) {
|
|
|
|
print_list(bb5, 0);
|
|
|
|
printf("\n");
|
|
|
|
do_test_seq(bb5, test5a);
|
|
|
|
printf("After test5 sequence: ");
|
|
|
|
print_list(bb5, 0);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (bb1 && bb2 && bb3 && bb4 && bb5) {
|
2000-07-04 23:20:25 +04:00
|
|
|
printf("Comparison tests:\n");
|
|
|
|
equal = ext2fs_badblocks_equal(bb1, bb2);
|
2008-08-28 07:07:54 +04:00
|
|
|
printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
|
2000-07-04 23:20:25 +04:00
|
|
|
if (equal)
|
|
|
|
test_fail++;
|
|
|
|
|
|
|
|
equal = ext2fs_badblocks_equal(bb1, bb3);
|
2008-08-28 07:07:54 +04:00
|
|
|
printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
|
2000-07-04 23:20:25 +04:00
|
|
|
if (!equal)
|
|
|
|
test_fail++;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-07-04 23:20:25 +04:00
|
|
|
equal = ext2fs_badblocks_equal(bb1, bb4);
|
2008-08-28 07:07:54 +04:00
|
|
|
printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
|
2000-07-04 23:20:25 +04:00
|
|
|
if (equal)
|
|
|
|
test_fail++;
|
2003-06-25 01:34:02 +04:00
|
|
|
|
|
|
|
equal = ext2fs_badblocks_equal(bb4, bb5);
|
2008-08-28 07:07:54 +04:00
|
|
|
printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
|
2003-06-25 01:34:02 +04:00
|
|
|
if (!equal)
|
|
|
|
test_fail++;
|
2000-07-04 23:20:25 +04:00
|
|
|
printf("\n");
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2003-06-25 01:34:02 +04:00
|
|
|
file_test(bb4);
|
2005-12-10 22:37:10 +03:00
|
|
|
|
|
|
|
file_test_invalid(bb4);
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
if (test_fail == 0)
|
|
|
|
printf("ext2fs library badblocks tests checks out OK!\n");
|
2000-07-04 23:20:25 +04:00
|
|
|
|
|
|
|
if (bb1)
|
|
|
|
ext2fs_badblocks_list_free(bb1);
|
|
|
|
if (bb2)
|
|
|
|
ext2fs_badblocks_list_free(bb2);
|
|
|
|
if (bb3)
|
|
|
|
ext2fs_badblocks_list_free(bb3);
|
|
|
|
if (bb4)
|
|
|
|
ext2fs_badblocks_list_free(bb4);
|
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
return test_fail;
|
2000-07-04 23:20:25 +04:00
|
|
|
|
1997-04-29 20:17:09 +04:00
|
|
|
}
|