This patch adds the following new APIs: Count the total number of blocks occupied by inode including intermediate extent tree nodes. extern blk64_t ext2fs_count_blocks(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode); Convert ext3_extent to ext2fs_extent. extern void ext2fs_convert_extent(struct ext2fs_extent *to, struct ext3_extent *from); Signed-off-by: Harshad Shirwadkar <harshadshirwadkar@xxxxxxxxx> --- lib/ext2fs/ext2fs.h | 4 ++++ lib/ext2fs/extent.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 01132245..afa9c5e4 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -1332,6 +1332,10 @@ extern errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle); extern size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle); extern errcode_t ext2fs_fix_extents_checksums(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode); +extern blk64_t ext2fs_count_blocks(ext2_filsys fs, ext2_ino_t ino, + struct ext2_inode *inode); +extern void ext2fs_convert_extent(struct ext2fs_extent *to, + struct ext3_extent *from); /* fallocate.c */ #define EXT2_FALLOCATE_ZERO_BLOCKS (0x1) diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c index ac3dbfec..43feea0a 100644 --- a/lib/ext2fs/extent.c +++ b/lib/ext2fs/extent.c @@ -1785,6 +1785,62 @@ out: return errcode; } +void ext2fs_convert_extent(struct ext2fs_extent *to, struct ext3_extent *from) +{ + to->e_pblk = ext2fs_le32_to_cpu(from->ee_start) + + ((__u64) ext2fs_le16_to_cpu(from->ee_start_hi) + << 32); + to->e_lblk = ext2fs_le32_to_cpu(from->ee_block); + to->e_len = ext2fs_le16_to_cpu(from->ee_len); + to->e_flags |= EXT2_EXTENT_FLAGS_LEAF; + if (to->e_len > EXT_INIT_MAX_LEN) { + to->e_len -= EXT_INIT_MAX_LEN; + to->e_flags |= EXT2_EXTENT_FLAGS_UNINIT; + } +} + +blk64_t ext2fs_count_blocks(ext2_filsys fs, ext2_ino_t ino, + struct ext2_inode *inode) +{ + ext2_extent_handle_t handle; + struct ext2fs_extent extent; + errcode_t errcode; + int i; + blk64_t blkcount = 0; + blk64_t *intermediate_nodes; + + errcode = ext2fs_extent_open2(fs, ino, inode, &handle); + if (errcode) + goto out; + + errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent); + if (errcode) + goto out; + + ext2fs_get_array(handle->max_depth, sizeof(blk64_t), + &intermediate_nodes); + blkcount = handle->level; + while (!errcode) { + if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) { + blkcount += extent.e_len; + for (i = 0; i < handle->level; i++) { + if (intermediate_nodes[i] != + handle->path[i].end_blk) { + blkcount++; + intermediate_nodes[i] = + handle->path[i].end_blk; + } + } + } + errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent); + } + ext2fs_free_mem(&intermediate_nodes); +out: + ext2fs_extent_free(handle); + + return blkcount; +} + #ifdef DEBUG /* * Override debugfs's prompt -- 2.29.2.454.gaff20da3a2-goog