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 EXT2FS_ATTR((unused)), blk_t blk)
22 printf(_("Bad block %u out of range; ignored.\n"), blk);
26 void read_bad_blocks_file(e2fsck_t ctx, const char *bad_blocks_file,
27 int replace_bad_blocks)
29 ext2_filsys fs = ctx->fs;
31 badblocks_list bb_list = 0;
35 e2fsck_read_bitmaps(ctx);
38 * Make sure the bad block inode is sane. If there are any
39 * illegal blocks, clear them.
41 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
42 check_bb_inode_blocks, 0);
44 com_err("ext2fs_block_iterate", retval,
45 _("while sanity checking the bad blocks inode"));
50 * If we're appending to the bad blocks inode, read in the
53 if (!replace_bad_blocks) {
54 retval = ext2fs_read_bb_inode(fs, &bb_list);
56 com_err("ext2fs_read_bb_inode", retval,
57 _("while reading the bad blocks inode"));
63 * Now read in the bad blocks from the file; if
64 * bad_blocks_file is null, then try to run the badblocks
67 if (bad_blocks_file) {
68 f = fopen(bad_blocks_file, "r");
70 com_err("read_bad_blocks_file", errno,
71 _("while trying to open %s"), bad_blocks_file);
75 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
76 (ctx->options & E2F_OPT_PREEN) ? "" : "-s ",
77 (ctx->options & E2F_OPT_WRITECHECK) ? "-n " : "",
78 fs->device_name, ext2fs_blocks_count(fs->super)-1);
81 com_err("read_bad_blocks_file", errno,
82 _("while trying popen '%s'"), buf);
86 retval = ext2fs_read_bb_FILE(fs, f, &bb_list, invalid_block);
92 com_err("ext2fs_read_bb_FILE", retval,
93 _("while reading in list of bad blocks from file"));
98 * Finally, update the bad blocks from the bad_block_map
100 printf("%s: Updating bad block inode.\n", ctx->device_name);
101 retval = ext2fs_update_bb_inode(fs, bb_list);
103 com_err("ext2fs_update_bb_inode", retval,
104 _("while updating bad block inode"));
108 ext2fs_badblocks_list_free(bb_list);
112 ctx->flags |= E2F_FLAG_ABORT;
117 static int check_bb_inode_blocks(ext2_filsys fs,
119 int blockcnt EXT2FS_ATTR((unused)),
120 void *priv_data EXT2FS_ATTR((unused)))
126 * If the block number is outrageous, clear it and ignore it.
128 if (*block_nr >= ext2fs_blocks_count(fs->super) ||
129 *block_nr < fs->super->s_first_data_block) {
130 printf(_("Warning: illegal block %u found in bad block inode. "
131 "Cleared.\n"), *block_nr);
133 return BLOCK_CHANGED;