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.
10 #include <et/com_err.h>
13 static void invalid_block(ext2_filsys fs, blk_t blk)
15 printf("Bad block %lu out of range; ignored.\n", blk);
19 void read_bad_blocks_file(ext2_filsys fs, const char *bad_blocks_file,
20 int replace_bad_blocks)
23 badblocks_list bb_list = 0;
29 * If we're appending to the bad blocks inode, read in the
32 if (!replace_bad_blocks) {
33 retval = ext2fs_read_bb_inode(fs, &bb_list);
35 com_err("ext2fs_read_bb_inode", retval,
36 "while reading the bad blocks inode");
42 * Now read in the bad blocks from the file.
44 f = fopen(bad_blocks_file, "r");
46 com_err("read_bad_blocks_file", errno,
47 "while trying to open %s", bad_blocks_file);
50 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
53 com_err("ext2fs_read_bb_FILE", retval,
54 "while reading in list of bad blocks from file");
59 * Finally, update the bad blocks from the bad_block_map
61 retval = ext2fs_update_bb_inode(fs, bb_list);
63 com_err("ext2fs_update_bb_inode", retval,
64 "while updating bad block inode");
68 badblocks_list_free(bb_list);
72 void test_disk(ext2_filsys fs)
75 badblocks_list bb_list = 0;
82 * Always read in the current list of bad blocks.
84 retval = ext2fs_read_bb_inode(fs, &bb_list);
86 com_err("ext2fs_read_bb_inode", retval,
87 "while reading the bad blocks inode");
92 * Now run the bad blocks program
94 sprintf(buf, "badblocks %s%s %ld", preen ? "" : "-s ",
96 fs->super->s_blocks_count);
98 printf("Running command: %s\n", buf);
101 com_err("popen", errno,
102 "while trying to run %s", buf);
105 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
108 com_err("ext2fs_read_bb_FILE", retval,
109 "while processing list of bad blocks from program");
114 * Finally, update the bad blocks from the bad_block_map
116 retval = ext2fs_update_bb_inode(fs, bb_list);
118 com_err("ext2fs_update_bb_inode", retval,
119 "while updating bad block inode");
123 badblocks_list_free(bb_list);