- if (!blk)
- return 0;
-
- p = (struct process_block_struct_1 *) priv_data;
- ctx = p->ctx;
- pctx = p->pctx;
-
- pctx->ino = EXT2_BAD_INO;
- pctx->blk = blk;
- pctx->blkcount = blockcnt;
-
- if ((blk < fs->super->s_first_data_block) ||
- (blk >= fs->super->s_blocks_count)) {
- if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
- *block_nr = 0;
- return BLOCK_CHANGED;
- } else
- return 0;
- }
-
- if (blockcnt < 0) {
- if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
- p->bbcheck = 1;
- if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
- *block_nr = 0;
- return BLOCK_CHANGED;
- }
- } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
- blk)) {
- p->bbcheck = 1;
- if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
- pctx)) {
- *block_nr = 0;
- return BLOCK_CHANGED;
- }
- if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
- return BLOCK_ABORT;
- } else
- mark_block_used(ctx, blk);
- return 0;
- }
-
- ctx->fs_badblocks_count++;
- /*
- * If the block is not used, then mark it as used and return.
- * If it is already marked as found, this must mean that
- * there's an overlap between the filesystem table blocks
- * (bitmaps and inode table) and the bad block list.
- */
- if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
- ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
- return 0;
- }
- /*
- * Try to find the where the filesystem block was used...
- */
- first_block = fs->super->s_first_data_block;
-
- for (i = 0; i < fs->group_desc_count; i++ ) {
- pctx->group = i;
- pctx->blk = blk;
- if (!ext2fs_bg_has_super(fs, i))
- goto skip_super;
- if (blk == first_block) {
- if (i == 0) {
- if (fix_problem(ctx,
- PR_1_BAD_PRIMARY_SUPERBLOCK,
- pctx)) {
- *block_nr = 0;
- return BLOCK_CHANGED;
- }
- return 0;
- }
- fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
- return 0;
- }
- if ((blk > first_block) &&
- (blk <= first_block + fs->desc_blocks)) {
- if (i == 0) {
- pctx->blk = *block_nr;
- if (fix_problem(ctx,
- PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
- *block_nr = 0;
- return BLOCK_CHANGED;
- }
- return 0;
- }
- fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
- return 0;
- }
- skip_super:
- if (blk == fs->group_desc[i].bg_block_bitmap) {
- if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
- ctx->invalid_block_bitmap_flag[i]++;
- ctx->invalid_bitmaps++;
- }
- return 0;
- }
- if (blk == fs->group_desc[i].bg_inode_bitmap) {
- if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
- ctx->invalid_inode_bitmap_flag[i]++;
- ctx->invalid_bitmaps++;
- }
- return 0;
- }
- if ((blk >= fs->group_desc[i].bg_inode_table) &&
- (blk < (fs->group_desc[i].bg_inode_table +
- fs->inode_blocks_per_group))) {
- /*
- * If there are bad blocks in the inode table,
- * the inode scan code will try to do
- * something reasonable automatically.
- */
- return 0;
- }
- first_block += fs->super->s_blocks_per_group;
- }
- /*
- * If we've gotten to this point, then the only
- * possibility is that the bad block inode meta data
- * is using a bad block.
- */
- if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
- (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
- (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
- p->bbcheck = 1;
- if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
- *block_nr = 0;
- return BLOCK_CHANGED;
- }
- if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
- return BLOCK_ABORT;
- return 0;
- }
-
- pctx->group = -1;
-
- /* Warn user that the block wasn't claimed */
- fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
-
- return 0;
-}
-
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
- const char *name, int num, blk_t *new_block)
-{
- ext2_filsys fs = ctx->fs;
- blk_t old_block = *new_block;
- int i;
- char *buf;
- struct problem_context pctx;
-
- clear_problem_context(&pctx);
-
- pctx.group = group;
- pctx.blk = old_block;
- pctx.str = name;
-
- pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
- first_block + fs->super->s_blocks_per_group,
- num, ctx->block_found_map, new_block);
- if (pctx.errcode) {
- pctx.num = num;
- fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
- ext2fs_unmark_valid(fs);
- return;
- }
- pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
- if (pctx.errcode) {
- fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
- ext2fs_unmark_valid(fs);
- return;
- }
- ext2fs_mark_super_dirty(fs);
- fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
- pctx.blk2 = *new_block;
- fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
- PR_1_RELOC_TO), &pctx);
- pctx.blk2 = 0;
- for (i = 0; i < num; i++) {
- pctx.blk = i;
- ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
- if (old_block) {
- pctx.errcode = io_channel_read_blk(fs->io,
- old_block + i, 1, buf);
- if (pctx.errcode)
- fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
- } else
- memset(buf, 0, fs->blocksize);
-
- pctx.blk = (*new_block) + i;
- pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
- 1, buf);
- if (pctx.errcode)
- fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
- }
- ext2fs_free_mem(&buf);