2 * badblocks.c --- replace/append bad blocks to the bad block inode
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
13 #include <et/com_err.h>
16 static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
20 static void invalid_block(ext2_filsys fs, blk_t blk)
22 printf("Bad block %u out of range; ignored.\n", blk);
26 void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
27 int replace_bad_blocks)
30 badblocks_list bb_list = 0;
37 * Make sure the bad block inode is sane. If there are any
38 * illegal blocks, clear them.
40 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
41 check_bb_inode_blocks, 0);
43 com_err("ext2fs_block_iterate", retval,
44 "while sanity checking the bad blocks inode");
49 * If we're appending to the bad blocks inode, read in the
52 if (!replace_bad_blocks) {
53 retval = ext2fs_read_bb_inode(fs, &bb_list);
55 com_err("ext2fs_read_bb_inode", retval,
56 "while reading the bad blocks inode");
62 * Now read in the bad blocks from the file; if
63 * bad_blocks_file is null, then try to run the badblocks
66 if (bad_blocks_file) {
67 f = fopen(bad_blocks_file, "r");
69 com_err("read_bad_blocks_file", errno,
70 "while trying to open %s", bad_blocks_file);
74 sprintf(buf, "badblocks %s%s %d", preen ? "" : "-s ",
76 fs->super->s_blocks_count);
79 com_err("read_bad_blocks_file", errno,
80 "while trying popen '%s'", buf);
84 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
90 com_err("ext2fs_read_bb_FILE", retval,
91 "while reading in list of bad blocks from file");
96 * Finally, update the bad blocks from the bad_block_map
98 retval = ext2fs_update_bb_inode(fs, bb_list);
100 com_err("ext2fs_update_bb_inode", retval,
101 "while updating bad block inode");
105 badblocks_list_free(bb_list);
109 void test_disk(ext2_filsys fs)
111 read_bad_blocks_file(fs, 0, 1);
114 static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
121 * If the block number is outrageous, clear it and ignore it.
123 if (*block_nr >= fs->super->s_blocks_count ||
124 *block_nr < fs->super->s_first_data_block) {
125 printf("Warning illegal block %u found in bad block inode. Cleared.\n", *block_nr);
127 return BLOCK_CHANGED;