static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) { dgrp_t i; unsigned int j; int block_nbytes, inode_nbytes; unsigned int nbits; errcode_t retval; char *block_buf = NULL, *inode_buf = NULL; int csum_flag = 0; blk64_t blk; blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block); ext2_ino_t ino_itr = 1; EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); if (!(fs->flags & EXT2_FLAG_RW)) return EXT2_ET_RO_FILSYS; if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) csum_flag = 1; inode_nbytes = block_nbytes = 0; if (do_block) { block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8; retval = io_channel_alloc_buf(fs->io, 0, &block_buf); if (retval) goto errout; memset(block_buf, 0xff, fs->blocksize); } if (do_inode) { inode_nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8); retval = io_channel_alloc_buf(fs->io, 0, &inode_buf); if (retval) goto errout; memset(inode_buf, 0xff, fs->blocksize); } for (i = 0; i < fs->group_desc_count; i++) { if (!do_block) goto skip_block_bitmap; if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ) goto skip_this_block_bitmap; retval = ext2fs_get_block_bitmap_range2(fs->block_map, blk_itr, block_nbytes << 3, block_buf); if (retval) goto errout; if (i == fs->group_desc_count - 1) { /* Force bitmap padding for the last group */ nbits = EXT2FS_NUM_B2C(fs, ((ext2fs_blocks_count(fs->super) - (__u64) fs->super->s_first_data_block) % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super))); if (nbits) for (j = nbits; j < fs->blocksize * 8; j++) ext2fs_set_bit(j, block_buf); } blk = ext2fs_block_bitmap_loc(fs, i); if (blk) { retval = io_channel_write_blk64(fs->io, blk, 1, block_buf); if (retval) { retval = EXT2_ET_BLOCK_BITMAP_WRITE; goto errout; } } skip_this_block_bitmap: blk_itr += block_nbytes << 3; skip_block_bitmap: if (!do_inode) continue; if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ) goto skip_this_inode_bitmap; retval = ext2fs_get_inode_bitmap_range2(fs->inode_map, ino_itr, inode_nbytes << 3, inode_buf); if (retval) goto errout; blk = ext2fs_inode_bitmap_loc(fs, i); if (blk) { retval = io_channel_write_blk64(fs->io, blk, 1, inode_buf); if (retval) { retval = EXT2_ET_INODE_BITMAP_WRITE; goto errout; } } skip_this_inode_bitmap: ino_itr += inode_nbytes << 3; } if (do_block) { fs->flags &= ~EXT2_FLAG_BB_DIRTY; ext2fs_free_mem(&block_buf); } if (do_inode) { fs->flags &= ~EXT2_FLAG_IB_DIRTY; ext2fs_free_mem(&inode_buf); } return 0; errout: if (inode_buf) ext2fs_free_mem(&inode_buf); if (block_buf) ext2fs_free_mem(&block_buf); return retval; }
static void check_block_bitmaps(e2fsck_t ctx) { ext2_filsys fs = ctx->fs; blk64_t i; unsigned int *free_array; int group = 0; unsigned int blocks = 0; blk64_t free_blocks = 0; blk64_t first_free = ext2fs_blocks_count(fs->super); unsigned int group_free = 0; int actual, bitmap; struct problem_context pctx; int problem, save_problem, fixit, had_problem; errcode_t retval; int csum_flag; int skip_group = 0; int old_desc_blocks = 0; int count = 0; int cmp_block = 0; int redo_flag = 0; blk64_t super_blk, old_desc_blk, new_desc_blk; clear_problem_context(&pctx); free_array = (unsigned int *) e2fsck_allocate_memory(ctx, fs->group_desc_count * sizeof(unsigned int), "free block count array"); if ((B2C(fs->super->s_first_data_block) < ext2fs_get_block_bitmap_start2(ctx->block_found_map)) || (B2C(ext2fs_blocks_count(fs->super)-1) > ext2fs_get_block_bitmap_end2(ctx->block_found_map))) { pctx.num = 1; pctx.blk = B2C(fs->super->s_first_data_block); pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1); pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map); pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map); fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); ctx->flags |= E2F_FLAG_ABORT; /* fatal */ goto errout; } if ((B2C(fs->super->s_first_data_block) < ext2fs_get_block_bitmap_start2(fs->block_map)) || (B2C(ext2fs_blocks_count(fs->super)-1) > ext2fs_get_block_bitmap_end2(fs->block_map))) { pctx.num = 2; pctx.blk = B2C(fs->super->s_first_data_block); pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1); pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map); pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map); fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); ctx->flags |= E2F_FLAG_ABORT; /* fatal */ goto errout; } csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super, EXT4_FEATURE_RO_COMPAT_GDT_CSUM); redo_counts: had_problem = 0; save_problem = 0; pctx.blk = pctx.blk2 = NO_BLK; if (csum_flag && (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) skip_group++; for (i = B2C(fs->super->s_first_data_block); i < ext2fs_blocks_count(fs->super); i += EXT2FS_CLUSTER_RATIO(fs)) { actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i); if (skip_group) { if ((B2C(i) - B2C(fs->super->s_first_data_block)) % fs->super->s_clusters_per_group == 0) { super_blk = 0; old_desc_blk = 0; new_desc_blk = 0; ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk, &new_desc_blk, 0); if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) old_desc_blocks = fs->super->s_first_meta_bg; else old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks; count = 0; cmp_block = fs->super->s_clusters_per_group; if (group == (int)fs->group_desc_count - 1) cmp_block = EXT2FS_NUM_B2C(fs, ext2fs_group_blocks_count(fs, group)); } bitmap = 0; if (EQ_CLSTR(i, super_blk) || (old_desc_blk && old_desc_blocks && GE_CLSTR(i, old_desc_blk) && LE_CLSTR(i, old_desc_blk + old_desc_blocks-1)) || (new_desc_blk && EQ_CLSTR(i, new_desc_blk)) || EQ_CLSTR(i, ext2fs_block_bitmap_loc(fs, group)) || EQ_CLSTR(i, ext2fs_inode_bitmap_loc(fs, group)) || (GE_CLSTR(i, ext2fs_inode_table_loc(fs, group)) && LE_CLSTR(i, (ext2fs_inode_table_loc(fs, group) + fs->inode_blocks_per_group - 1)))) { bitmap = 1; actual = (actual != 0); count++; cmp_block--; } else if ((EXT2FS_B2C(fs, i) - count - EXT2FS_B2C(fs, fs->super->s_first_data_block)) % fs->super->s_clusters_per_group == 0) { /* * When the compare data blocks in block bitmap * are 0, count the free block, * skip the current block group. */ if (ext2fs_test_block_bitmap_range2( ctx->block_found_map, EXT2FS_B2C(fs, i), cmp_block)) { /* * -1 means to skip the current block * group. */ blocks = fs->super->s_clusters_per_group - 1; group_free = cmp_block; free_blocks += cmp_block; /* * The current block group's last block * is set to i. */ i += EXT2FS_C2B(fs, cmp_block - 1); bitmap = 1; goto do_counts; } } } else if (redo_flag) bitmap = actual; else bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i); if (!actual == !bitmap) goto do_counts; if (!actual && bitmap) { /* * Block not used, but marked in use in the bitmap. */ problem = PR_5_BLOCK_UNUSED; } else { /* * Block used, but not marked in use in the bitmap. */ problem = PR_5_BLOCK_USED; if (skip_group) { struct problem_context pctx2; pctx2.blk = i; pctx2.group = group; if (fix_problem(ctx, PR_5_BLOCK_UNINIT,&pctx2)){ ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT); skip_group = 0; } } } if (pctx.blk == NO_BLK) { pctx.blk = pctx.blk2 = i; save_problem = problem; } else { if ((problem == save_problem) && (pctx.blk2 == i-1)) pctx.blk2++; else { print_bitmap_problem(ctx, save_problem, &pctx); pctx.blk = pctx.blk2 = i; save_problem = problem; } } ctx->flags |= E2F_FLAG_PROG_SUPPRESS; had_problem++; /* * If there a problem we should turn off the discard so we * do not compromise the filesystem. */ ctx->options &= ~E2F_OPT_DISCARD; do_counts: if (!bitmap) { group_free++; free_blocks++; if (first_free > i) first_free = i; } else if (i > first_free) { e2fsck_discard_blocks(ctx, first_free, (i - first_free)); first_free = ext2fs_blocks_count(fs->super); } blocks ++; if ((blocks == fs->super->s_clusters_per_group) || (EXT2FS_B2C(fs, i) == EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) { /* * If the last block of this group is free, then we can * discard it as well. */ if (!bitmap && i >= first_free) e2fsck_discard_blocks(ctx, first_free, (i - first_free) + 1); first_free = ext2fs_blocks_count(fs->super); free_array[group] = group_free; group ++; blocks = 0; group_free = 0; skip_group = 0; if (ctx->progress) if ((ctx->progress)(ctx, 5, group, fs->group_desc_count*2)) goto errout; if (csum_flag && (i != ext2fs_blocks_count(fs->super)-1) && ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)) skip_group++; } } if (pctx.blk != NO_BLK) print_bitmap_problem(ctx, save_problem, &pctx); if (had_problem) fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP); else fixit = -1; ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS; if (fixit == 1) { ext2fs_free_block_bitmap(fs->block_map); retval = ext2fs_copy_bitmap(ctx->block_found_map, &fs->block_map); if (retval) { clear_problem_context(&pctx); fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx); ctx->flags |= E2F_FLAG_ABORT; goto errout; } ext2fs_set_bitmap_padding(fs->block_map); ext2fs_mark_bb_dirty(fs); /* Redo the counts */ blocks = 0; free_blocks = 0; group_free = 0; group = 0; memset(free_array, 0, fs->group_desc_count * sizeof(int)); redo_flag++; goto redo_counts; } else if (fixit == 0) ext2fs_unmark_valid(fs); for (i = 0; i < fs->group_desc_count; i++) { if (free_array[i] != ext2fs_bg_free_blocks_count(fs, i)) { pctx.group = i; pctx.blk = ext2fs_bg_free_blocks_count(fs, i); pctx.blk2 = free_array[i]; if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP, &pctx)) { ext2fs_bg_free_blocks_count_set(fs, i, free_array[i]); ext2fs_mark_super_dirty(fs); } else ext2fs_unmark_valid(fs); } } free_blocks = EXT2FS_C2B(fs, free_blocks); if (free_blocks != ext2fs_free_blocks_count(fs->super)) { pctx.group = 0; pctx.blk = ext2fs_free_blocks_count(fs->super); pctx.blk2 = free_blocks; if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) { ext2fs_free_blocks_count_set(fs->super, free_blocks); ext2fs_mark_super_dirty(fs); } } errout: ext2fs_free_mem(&free_array); }