diff --git a/lwext4/ext4_super.c b/lwext4/ext4_super.c index f02d014..f39f4fd 100644 --- a/lwext4/ext4_super.c +++ b/lwext4/ext4_super.c @@ -126,6 +126,42 @@ bool ext4_sb_check(struct ext4_sblock *s) return true; } +static inline int is_multiple(uint32_t a, uint32_t b) +{ + while (1) { + if (a < b) + return 0; + if (a == b) + return 1; + if ((a % b) != 0) + return 0; + a = a / b; + } +} + +static int ext4_sb_sparse(uint32_t group) +{ + if (group <= 1) + return 1; + + if (!(group & 1)) + return 0; + + return (is_multiple(group, 7) || is_multiple(group, 5) || + is_multiple(group, 3)); +} + + +bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group) +{ + if (ext4_sb_check_read_only(s, + EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && + !ext4_sb_sparse(group)) + return false; + return true; +} + + /** * @} */ diff --git a/lwext4/ext4_super.h b/lwext4/ext4_super.h index 9a08bbb..9011c46 100644 --- a/lwext4/ext4_super.h +++ b/lwext4/ext4_super.h @@ -177,6 +177,11 @@ int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s); * @return true if OK*/ bool ext4_sb_check(struct ext4_sblock *s); +/**@brief Superblock presence in block group. + * @param s superblock dsecriptor + * @param block_group block group id + * @return true if block group has superblock*/ +bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t block_group); #endif /* EXT4_SUPER_H_ */