@@ -473,10 +473,14 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,
ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
if (S_ISDIR(mode) &&
- ((parent == d_inode(sb->s_root)) ||
- (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
+ ((parent == d_inode(sb->s_root)) ||
+ (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
+ unsigned int inodes_per_flex_group;
+ unsigned long int clusters_per_flex_group;
int best_ndir = inodes_per_group;
int ret = -1;
+ inodes_per_flex_group = inodes_per_group * flex_size;
+ clusters_per_flex_group = sbi->s_clusters_per_group * flex_size;
if (qstr) {
hinfo.hash_version = DX_HASH_HALF_MD4;
@@ -489,6 +493,10 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,
for (i = 0; i < ngroups; i++) {
g = (parent_group + i) % ngroups;
get_orlov_stats(sb, g, flex_size, &stats);
+ /* the group can't get any better than empty */
+ if (inodes_per_flex_group == stats.free_inodes &&
+ clusters_per_flex_group == stats.free_clusters)
+ goto found_flex_bg;
if (!stats.free_inodes)
continue;
if (stats.used_dirs >= best_ndir)