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.
14 #include <et/com_err.h>
17 static int check_bb_inode_blocks(ext2_filsys fs, blk_t *block_nr, int blockcnt,
21 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
23 printf(_("Bad block %u out of range; ignored.\n"), blk);
27 void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
28 int replace_bad_blocks)
30 ext2_filsys fs = ctx->fs;
32 badblocks_list bb_list = 0;
36 e2fsck_read_bitmaps(ctx);
39 * Make sure the bad block inode is sane. If there are any
40 * illegal blocks, clear them.
42 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
43 check_bb_inode_blocks, 0);
45 com_err("ext2fs_block_iterate", retval, "%s",
46 _("while sanity checking the bad blocks inode"));
51 * If we're appending to the bad blocks inode, read in the
54 if (!replace_bad_blocks) {
55 retval = ext2fs_read_bb_inode(fs, &bb_list);
57 com_err("ext2fs_read_bb_inode", retval, "%s",
58 _("while reading the bad blocks inode"));
64 * Now read in the bad blocks from the file; if
65 * bad_blocks_file is null, then try to run the badblocks
68 if (bad_blocks_file) {
69 f = fopen(bad_blocks_file, "r");
71 com_err("read_bad_blocks_file", errno,
72 _("while trying to open %s"), bad_blocks_file);
76 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
77 (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
78 (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
80 (unsigned long long) ext2fs_blocks_count(fs->super)-1);
83 com_err("read_bad_blocks_file", errno,
84 _("while trying popen '%s'"), buf);
88 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
94 com_err("ext2fs_read_bb_FILE", retval, "%s",
95 _("while reading in list of bad blocks from file"));
100 * Finally, update the bad blocks from the bad_block_map
102 printf("%s: Updating bad block inode.\n", ctx->device_name);
103 retval = ext2fs_update_bb_inode(fs, bb_list);
105 com_err("ext2fs_update_bb_inode", retval, "%s",
106 _("while updating bad block inode"));
110 ext2fs_badblocks_list_free(bb_list);
114 ctx->flags |= E2F_FLAG_ABORT;
116 ext2fs_badblocks_list_free(bb_list);
121 static int check_bb_inode_blocks(ext2_filsys fs,
123 int blockcnt EXT2FS_ATTR((unused)),
124 void *priv_data EXT2FS_ATTR((unused)))
130 * If the block number is outrageous, clear it and ignore it.
132 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
133 *block_nr < fs->super->s_first_data_block) {
134 printf(_("Warning: illegal block %u found in bad block inode. "
135 "Cleared.\n"), *block_nr);
137 return BLOCK_CHANGED;