X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lib%2Fext2fs%2Frw_bitmaps.c;h=9db76eb94b67a901f513d14c2c69b80c982a9ebc;hb=2d545e3762636a1f5d4be5ee831c3d188d007ed5;hp=7878be61ec4fba493dad8c66f291b0ed814f63da;hpb=4efbac6fed75c29d3d5f1b676b932754653a2ac5;p=tools%2Fe2fsprogs.git diff --git a/lib/ext2fs/rw_bitmaps.c b/lib/ext2fs/rw_bitmaps.c index 7878be6..9db76eb 100644 --- a/lib/ext2fs/rw_bitmaps.c +++ b/lib/ext2fs/rw_bitmaps.c @@ -4,11 +4,12 @@ * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o. * * %Begin-Header% - * This file may be redistributed under the terms of the GNU Public - * License. + * This file may be redistributed under the terms of the GNU Library + * General Public License, version 2. * %End-Header% */ +#include "config.h" #include #include #if HAVE_UNISTD_H @@ -34,10 +35,10 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) int block_nbytes, inode_nbytes; unsigned int nbits; errcode_t retval; - char *block_buf, *inode_buf; - int csum_flag = 0; - blk_t blk; - blk64_t blk_itr = fs->super->s_first_data_block; + char *block_buf = NULL, *inode_buf = NULL; + int csum_flag; + 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); @@ -45,24 +46,22 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) 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; + csum_flag = ext2fs_has_group_desc_csum(fs); inode_nbytes = block_nbytes = 0; if (do_block) { - block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; - retval = ext2fs_get_mem(fs->blocksize, &block_buf); + block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8; + retval = io_channel_alloc_buf(fs->io, 0, &block_buf); if (retval) - return 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 = ext2fs_get_mem(fs->blocksize, &inode_buf); + retval = io_channel_alloc_buf(fs->io, 0, &inode_buf); if (retval) - return retval; + goto errout; memset(inode_buf, 0xff, fs->blocksize); } @@ -70,30 +69,41 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) if (!do_block) goto skip_block_bitmap; - if (csum_flag && ext2fs_bg_flag_test(fs, i, EXT2_BG_BLOCK_UNINIT) + 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) - return retval; + goto errout; if (i == fs->group_desc_count - 1) { /* Force bitmap padding for the last group */ - nbits = ((ext2fs_blocks_count(fs->super) + nbits = EXT2FS_NUM_B2C(fs, + ((ext2fs_blocks_count(fs->super) - (__u64) fs->super->s_first_data_block) - % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super)); + % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super))); if (nbits) for (j = nbits; j < fs->blocksize * 8; j++) ext2fs_set_bit(j, block_buf); } - blk = fs->group_desc[i].bg_block_bitmap; + + retval = ext2fs_block_bitmap_csum_set(fs, i, block_buf, + block_nbytes); + if (retval) + return retval; + ext2fs_group_desc_csum_set(fs, i); + fs->flags |= EXT2_FLAG_DIRTY; + + blk = ext2fs_block_bitmap_loc(fs, i); if (blk) { retval = io_channel_write_blk64(fs->io, blk, 1, block_buf); - if (retval) - return EXT2_ET_BLOCK_BITMAP_WRITE; + if (retval) { + retval = EXT2_ET_BLOCK_BITMAP_WRITE; + goto errout; + } } skip_this_block_bitmap: blk_itr += block_nbytes << 3; @@ -102,21 +112,30 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) if (!do_inode) continue; - if (csum_flag && ext2fs_bg_flag_test(fs, i, EXT2_BG_INODE_UNINIT) + 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) - return retval; + goto errout; - blk = fs->group_desc[i].bg_inode_bitmap; + retval = ext2fs_inode_bitmap_csum_set(fs, i, inode_buf, + inode_nbytes); + if (retval) + goto errout; + ext2fs_group_desc_csum_set(fs, i); + fs->flags |= EXT2_FLAG_DIRTY; + + blk = ext2fs_inode_bitmap_loc(fs, i); if (blk) { retval = io_channel_write_blk64(fs->io, blk, 1, inode_buf); - if (retval) - return EXT2_ET_INODE_BITMAP_WRITE; + if (retval) { + retval = EXT2_ET_INODE_BITMAP_WRITE; + goto errout; + } } skip_this_inode_bitmap: ino_itr += inode_nbytes << 3; @@ -131,6 +150,49 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) 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 errcode_t mark_uninit_bg_group_blocks(ext2_filsys fs) +{ + dgrp_t i; + blk64_t blk; + ext2fs_block_bitmap bmap = fs->block_map; + + for (i = 0; i < fs->group_desc_count; i++) { + if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) + continue; + + ext2fs_reserve_super_and_bgd(fs, i, bmap); + + /* + * Mark the blocks used for the inode table + */ + blk = ext2fs_inode_table_loc(fs, i); + if (blk) + ext2fs_mark_block_bitmap_range2(bmap, blk, + fs->inode_blocks_per_group); + + /* + * Mark block used for the block bitmap + */ + blk = ext2fs_block_bitmap_loc(fs, i); + if (blk) + ext2fs_mark_block_bitmap2(bmap, blk); + + /* + * Mark block used for the inode bitmap + */ + blk = ext2fs_inode_bitmap_loc(fs, i); + if (blk) + ext2fs_mark_block_bitmap2(bmap, blk); + } + return 0; } static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) @@ -139,24 +201,25 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) char *block_bitmap = 0, *inode_bitmap = 0; char *buf; errcode_t retval; - int block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; + int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8; int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8; - int csum_flag = 0; - int do_image = fs->flags & EXT2_FLAG_IMAGE_FILE; + int csum_flag; unsigned int cnt; - blk_t blk; - blk64_t blk_itr = fs->super->s_first_data_block; - blk_t blk_cnt; + blk64_t blk; + blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block); + blk64_t blk_cnt; ext2_ino_t ino_itr = 1; ext2_ino_t ino_cnt; EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); + if ((block_nbytes > (int) fs->blocksize) || + (inode_nbytes > (int) fs->blocksize)) + return EXT2_ET_CORRUPT_SUPERBLOCK; + fs->write_bitmaps = ext2fs_write_bitmaps; - if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, - EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) - csum_flag = 1; + csum_flag = ext2fs_has_group_desc_csum(fs); retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf); if (retval) @@ -169,8 +232,7 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map); if (retval) goto cleanup; - retval = ext2fs_get_mem(do_image ? fs->blocksize : - (unsigned) block_nbytes, &block_bitmap); + retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap); if (retval) goto cleanup; } else @@ -183,8 +245,7 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map); if (retval) goto cleanup; - retval = ext2fs_get_mem(do_image ? fs->blocksize : - (unsigned) inode_nbytes, &inode_bitmap); + retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap); if (retval) goto cleanup; } else @@ -212,8 +273,8 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) } blk = (fs->image_header->offset_blockmap / fs->blocksize); - blk_cnt = (blk64_t)EXT2_BLOCKS_PER_GROUP(fs->super) * - fs->group_desc_count; + blk_cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super, + fs->group_desc_count); while (block_nbytes > 0) { retval = io_channel_read_blk64(fs->image_io, blk++, 1, block_bitmap); @@ -235,18 +296,27 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) for (i = 0; i < fs->group_desc_count; i++) { if (block_bitmap) { - blk = fs->group_desc[i].bg_block_bitmap; + blk = ext2fs_block_bitmap_loc(fs, i); if (csum_flag && - ext2fs_bg_flag_test(fs, i, EXT2_BG_BLOCK_UNINIT) && + ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) && ext2fs_group_desc_csum_verify(fs, i)) blk = 0; if (blk) { retval = io_channel_read_blk64(fs->io, blk, - -block_nbytes, block_bitmap); + 1, block_bitmap); if (retval) { retval = EXT2_ET_BLOCK_BITMAP_READ; goto cleanup; } + /* verify block bitmap checksum */ + if (!(fs->flags & + EXT2_FLAG_IGNORE_CSUM_ERRORS) && + !ext2fs_block_bitmap_csum_verify(fs, i, + block_bitmap, block_nbytes)) { + retval = + EXT2_ET_BLOCK_BITMAP_CSUM_INVALID; + goto cleanup; + } } else memset(block_bitmap, 0, block_nbytes); cnt = block_nbytes << 3; @@ -257,18 +327,28 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) blk_itr += block_nbytes << 3; } if (inode_bitmap) { - blk = fs->group_desc[i].bg_inode_bitmap; + blk = ext2fs_inode_bitmap_loc(fs, i); if (csum_flag && - ext2fs_bg_flag_test(fs, i, EXT2_BG_INODE_UNINIT) && + ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) && ext2fs_group_desc_csum_verify(fs, i)) blk = 0; if (blk) { retval = io_channel_read_blk64(fs->io, blk, - -inode_nbytes, inode_bitmap); + 1, inode_bitmap); if (retval) { retval = EXT2_ET_INODE_BITMAP_READ; goto cleanup; } + + /* verify inode bitmap checksum */ + if (!(fs->flags & + EXT2_FLAG_IGNORE_CSUM_ERRORS) && + !ext2fs_inode_bitmap_csum_verify(fs, i, + inode_bitmap, inode_nbytes)) { + retval = + EXT2_ET_INODE_BITMAP_CSUM_INVALID; + goto cleanup; + } } else memset(inode_bitmap, 0, inode_nbytes); cnt = inode_nbytes << 3; @@ -279,6 +359,14 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) ino_itr += inode_nbytes << 3; } } + + /* Mark group blocks for any BLOCK_UNINIT groups */ + if (do_block) { + retval = mark_uninit_bg_group_blocks(fs); + if (retval) + goto cleanup; + } + success_cleanup: if (inode_bitmap) ext2fs_free_mem(&inode_bitmap);