From 1da5ef707904cf99300a0fb36b9ae3b29dbc8bde Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 4 Jun 2011 10:20:47 -0400 Subject: [PATCH] libext2fs: change fs->clustersize to fs->cluster_ratio_bits The log2 of the ratio of cluster size to block size is far more useful than just storing the cluster size. So make this change, and then define basic utility macros: EXT2FS_CLUSTER_RATIO(), EXT2FS_CLUSTER_MASK(), EXT2FS_B2C(), EXT2FS_C2B(), and EXT2FS_NUM_B2C(). Signed-off-by: "Theodore Ts'o" --- lib/ext2fs/ext2fs.h | 13 ++++++++++++- lib/ext2fs/initialize.c | 3 ++- lib/ext2fs/openfs.c | 8 +++++++- misc/mke2fs.c | 5 +++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 516eb1a..5379422 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -195,7 +195,7 @@ struct struct_ext2_filsys { char * device_name; struct ext2_super_block * super; unsigned int blocksize; - int clustersize; + int cluster_ratio_bits; dgrp_t group_desc_count; unsigned long desc_blocks; struct ext2_group_desc * group_desc; @@ -547,6 +547,17 @@ typedef struct ext2_icount *ext2_icount_t; #define EXT2_LIB_SOFTSUPP_INCOMPAT (0) #define EXT2_LIB_SOFTSUPP_RO_COMPAT (EXT4_FEATURE_RO_COMPAT_BIGALLOC) + +/* Translate a block number to a cluster number */ +#define EXT2FS_CLUSTER_RATIO(fs) (1 << (fs)->cluster_ratio_bits) +#define EXT2FS_CLUSTER_MASK(fs) (EXT2FS_CLUSTER_RATIO(fs) - 1) +#define EXT2FS_B2C(fs, blk) ((blk) >> (fs)->cluster_ratio_bits) +/* Translate a cluster number to a block number */ +#define EXT2FS_C2B(fs, cluster) ((cluster) << (fs)->cluster_ratio_bits) +/* Translate # of blks to # of clusters */ +#define EXT2FS_NUM_B2C(fs, blks) (((blks) + EXT2FS_CLUSTER_MASK(fs)) >> \ + (fs)->cluster_ratio_bits) + /* * function prototypes */ diff --git a/lib/ext2fs/initialize.c b/lib/ext2fs/initialize.c index c109d08..b9d45ae 100644 --- a/lib/ext2fs/initialize.c +++ b/lib/ext2fs/initialize.c @@ -178,7 +178,8 @@ errcode_t ext2fs_initialize(const char *name, int flags, super->s_creator_os = CREATOR_OS; fs->blocksize = EXT2_BLOCK_SIZE(super); - fs->clustersize = EXT2_CLUSTER_SIZE(super); + fs->cluster_ratio_bits = super->s_log_cluster_size - + super->s_log_block_size; /* default: (fs->blocksize*8) blocks/group, up to 2^16 (GDT limit) */ set_field(s_blocks_per_group, fs->blocksize * 8); diff --git a/lib/ext2fs/openfs.c b/lib/ext2fs/openfs.c index f87c171..a598ca0 100644 --- a/lib/ext2fs/openfs.c +++ b/lib/ext2fs/openfs.c @@ -250,7 +250,13 @@ errcode_t ext2fs_open2(const char *name, const char *io_options, retval = EXT2_ET_CORRUPT_SUPERBLOCK; goto cleanup; } - fs->clustersize = EXT2_CLUSTER_SIZE(fs->super); + fs->cluster_ratio_bits = fs->super->s_log_cluster_size - + fs->super->s_log_block_size; + if (EXT2_BLOCKS_PER_GROUP(fs->super) != + EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) { + retval = EXT2_ET_CORRUPT_SUPERBLOCK; + goto cleanup; + } fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) * EXT2_INODE_SIZE(fs->super) + EXT2_BLOCK_SIZE(fs->super) - 1) / diff --git a/misc/mke2fs.c b/misc/mke2fs.c index 9f28901..c46df6c 100644 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@ -619,9 +619,10 @@ static void show_stats(ext2_filsys fs) if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, EXT4_FEATURE_RO_COMPAT_BIGALLOC)) printf(_("Cluster size=%u (log=%u)\n"), - fs->clustersize, s->s_log_cluster_size); + fs->blocksize << fs->cluster_ratio_bits, + s->s_log_cluster_size); else - printf(_("Fragment size=%u (log=%u)\n"), fs->clustersize, + printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s), s->s_log_cluster_size); printf(_("Stride=%u blocks, Stripe width=%u blocks\n"), s->s_raid_stride, s->s_raid_stripe_width); -- 1.8.3.1