e2fsprogs/e2fsck/badblocks.c

138 lines
3.1 KiB
C
Raw Normal View History

1997-04-26 17:21:57 +04:00
/*
* badblocks.c --- replace/append bad blocks to the bad block inode
*
* Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
* redistributed under the terms of the GNU Public License.
*/
#include <time.h>
1997-04-26 17:58:21 +04:00
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
1997-04-26 17:21:57 +04:00
#include <et/com_err.h>
#include "e2fsck.h"
1997-04-26 17:34:30 +04:00
static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
void *priv_data);
1997-04-26 17:34:30 +04:00
2003-12-07 09:28:50 +03:00
static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
1997-04-26 17:21:57 +04:00
{
printf(_("Bad block %u out of range; ignored.\n"), blk);
1997-04-26 17:21:57 +04:00
return;
}
void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
1997-04-26 17:21:57 +04:00
int replace_bad_blocks)
{
ext2_filsys fs = ctx->fs;
1997-04-26 17:21:57 +04:00
errcode_t retval;
badblocks_list bb_list = 0;
FILE *f;
1997-04-26 18:37:06 +04:00
char buf[1024];
1997-04-26 17:21:57 +04:00
e2fsck_read_bitmaps(ctx);
1997-04-26 17:34:30 +04:00
/*
* Make sure the bad block inode is sane. If there are any
* illegal blocks, clear them.
*/
retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
check_bb_inode_blocks, 0);
if (retval) {
com_err("ext2fs_block_iterate", retval,
_("while sanity checking the bad blocks inode"));
goto fatal;
1997-04-26 17:34:30 +04:00
}
1997-04-26 17:21:57 +04:00
/*
* If we're appending to the bad blocks inode, read in the
* current bad blocks.
*/
if (!replace_bad_blocks) {
retval = ext2fs_read_bb_inode(fs, &bb_list);
if (retval) {
com_err("ext2fs_read_bb_inode", retval,
_("while reading the bad blocks inode"));
goto fatal;
1997-04-26 17:21:57 +04:00
}
}
/*
1997-04-26 18:37:06 +04:00
* Now read in the bad blocks from the file; if
* bad_blocks_file is null, then try to run the badblocks
* command.
1997-04-26 17:21:57 +04:00
*/
1997-04-26 18:37:06 +04:00
if (bad_blocks_file) {
f = fopen(bad_blocks_file, "r");
if (!f) {
com_err("read_bad_blocks_file", errno,
_("while trying to open %s"), bad_blocks_file);
goto fatal;
1997-04-26 18:37:06 +04:00
}
} else {
sprintf(buf, "badblocks -b %d -X %s%s%s %d", fs->blocksize,
(ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
(ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
fs->device_name, fs->super->s_blocks_count-1);
1997-04-26 18:37:06 +04:00
f = popen(buf, "r");
if (!f) {
com_err("read_bad_blocks_file", errno,
_("while trying popen '%s'"), buf);
goto fatal;
1997-04-26 18:37:06 +04:00
}
1997-04-26 17:21:57 +04:00
}
retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
1997-04-26 18:37:06 +04:00
if (bad_blocks_file)
fclose(f);
else
pclose(f);
1997-04-26 17:21:57 +04:00
if (retval) {
com_err("ext2fs_read_bb_FILE", retval,
_("while reading in list of bad blocks from file"));
goto fatal;
1997-04-26 17:21:57 +04:00
}
/*
* Finally, update the bad blocks from the bad_block_map
*/
retval = ext2fs_update_bb_inode(fs, bb_list);
if (retval) {
com_err("ext2fs_update_bb_inode", retval,
_("while updating bad block inode"));
goto fatal;
1997-04-26 17:21:57 +04:00
}
ext2fs_badblocks_list_free(bb_list);
1997-04-26 17:21:57 +04:00
return;
fatal:
ctx->flags |= E2F_FLAG_ABORT;
return;
1997-04-26 17:21:57 +04:00
}
2003-12-07 09:28:50 +03:00
static int check_bb_inode_blocks(ext2_filsys fs,
blk_t *block_nr,
int blockcnt EXT2FS_ATTR((unused)),
void *priv_data EXT2FS_ATTR((unused)))
1997-04-26 17:34:30 +04:00
{
if (!*block_nr)
return 0;
/*
* If the block number is outrageous, clear it and ignore it.
*/
if (*block_nr >= fs->super->s_blocks_count ||
*block_nr < fs->super->s_first_data_block) {
printf(_("Warning: illegal block %u found in bad block inode. "
"Cleared.\n"), *block_nr);
1997-04-26 17:34:30 +04:00
*block_nr = 0;
return BLOCK_CHANGED;
}
return 0;
}