/* Basic allocation info */
        u64 ram_bytes;
        u64 num_bytes;
+       u64 min_alloc_size;
        u64 empty_size;
        u64 flags;
        int delalloc;
            ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
                ffe_ctl->orig_have_caching_bg = true;
 
-       if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
-           ffe_ctl->have_caching_bg)
-               return 1;
-
-       if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
-               return 1;
-
        if (ins->objectid) {
                found_extent(ffe_ctl, ins);
                return 0;
        }
 
+       if (ffe_ctl->max_extent_size >= ffe_ctl->min_alloc_size &&
+           !btrfs_can_activate_zone(fs_info->fs_devices, ffe_ctl->index)) {
+               /*
+                * If we have enough free space left in an already active block
+                * group and we can't activate any other zone now, retry the
+                * active ones with a smaller allocation size.  Returning early
+                * from here will tell btrfs_reserve_extent() to haven the
+                * size.
+                */
+               return -ENOSPC;
+       }
+
+       if (ffe_ctl->loop >= LOOP_CACHING_WAIT && ffe_ctl->have_caching_bg)
+               return 1;
+
+       ffe_ctl->index++;
+       if (ffe_ctl->index < BTRFS_NR_RAID_TYPES)
+               return 1;
+
        /*
         * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
         *                      caching kthreads as we move along
 
        ffe_ctl.ram_bytes = ram_bytes;
        ffe_ctl.num_bytes = num_bytes;
+       ffe_ctl.min_alloc_size = min_alloc_size;
        ffe_ctl.empty_size = empty_size;
        ffe_ctl.flags = flags;
        ffe_ctl.delalloc = delalloc;
 
 
        return ret;
 }
+
+bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, int raid_index)
+{
+       struct btrfs_device *device;
+       bool ret = false;
+
+       if (!btrfs_is_zoned(fs_devices->fs_info))
+               return true;
+
+       /* Non-single profiles are not supported yet */
+       if (raid_index != BTRFS_RAID_SINGLE)
+               return false;
+
+       /* Check if there is a device with active zones left */
+       mutex_lock(&fs_devices->device_list_mutex);
+       list_for_each_entry(device, &fs_devices->devices, dev_list) {
+               struct btrfs_zoned_device_info *zinfo = device->zone_info;
+
+               if (!device->bdev)
+                       continue;
+
+               if (!zinfo->max_active_zones ||
+                   atomic_read(&zinfo->active_zones_left)) {
+                       ret = true;
+                       break;
+               }
+       }
+       mutex_unlock(&fs_devices->device_list_mutex);
+
+       return ret;
+}
 
                                            u64 logical, u64 length);
 bool btrfs_zone_activate(struct btrfs_block_group *block_group);
 int btrfs_zone_finish(struct btrfs_block_group *block_group);
+bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
+                            int raid_index);
 #else /* CONFIG_BLK_DEV_ZONED */
 static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
                                     struct blk_zone *zone)
        return 0;
 }
 
+static inline bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices,
+                                          int raid_index)
+{
+       return true;
+}
+
 #endif
 
 static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)