Skip to content

Commit

Permalink
libext2fs: add a way to check the theoretical maximum extent tree depth
Browse files Browse the repository at this point in the history
Add an API so that client programs can discover a reasonable maximum
extent tree depth.  This will eventually be used by e2fsck as one of
the criteria to decide if an extent-based file should have its extent
tree rebuilt.

Turn some related magic numbers into constants while we're at it.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
  • Loading branch information
djwong authored and tytso committed Dec 14, 2014
1 parent 6509eeb commit ffe1b28
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/ext2fs/ext2fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,7 @@ extern errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,
extern errcode_t ext2fs_extent_goto2(ext2_extent_handle_t handle,
int leaf_level, blk64_t blk);
extern errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle);
size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle);

/* fileio.c */
extern errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
Expand Down
2 changes: 2 additions & 0 deletions lib/ext2fs/ext3_extents.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ struct ext3_ext_path {
*/
#define EXT_INIT_MAX_LEN (1UL << 15)
#define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1)
#define EXT_MAX_EXTENT_LBLK (((__u64) 1 << 32) - 1)
#define EXT_MAX_EXTENT_PBLK (((__u64) 1 << 48) - 1)

#define EXT_FIRST_EXTENT(__hdr__) \
((struct ext3_extent *) (((char *) (__hdr__)) + \
Expand Down
33 changes: 29 additions & 4 deletions lib/ext2fs/extent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1697,14 +1697,39 @@ errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,

info->curr_level = handle->level;
info->max_depth = handle->max_depth;
info->max_lblk = ((__u64) 1 << 32) - 1;
info->max_pblk = ((__u64) 1 << 48) - 1;
info->max_len = (1UL << 15);
info->max_uninit_len = (1UL << 15) - 1;
info->max_lblk = EXT_MAX_EXTENT_LBLK;
info->max_pblk = EXT_MAX_EXTENT_PBLK;
info->max_len = EXT_INIT_MAX_LEN;
info->max_uninit_len = EXT_UNINIT_MAX_LEN;

return 0;
}

static int ul_log2(unsigned long arg)
{
int l = 0;

arg >>= 1;
while (arg) {
l++;
arg >>= 1;
}
return l;
}

size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
{
size_t iblock_sz = sizeof(((struct ext2_inode *)NULL)->i_block);
size_t iblock_extents = (iblock_sz - sizeof(struct ext3_extent_header)) /
sizeof(struct ext3_extent);
size_t extents_per_block = (handle->fs->blocksize -
sizeof(struct ext3_extent_header)) /
sizeof(struct ext3_extent);

return 1 + ((ul_log2(EXT_MAX_EXTENT_LBLK) - ul_log2(iblock_extents)) /
ul_log2(extents_per_block));
}

#ifdef DEBUG
/*
* Override debugfs's prompt
Expand Down

0 comments on commit ffe1b28

Please sign in to comment.