* [PATCH 1/4] btrfs-progs: zoned: use cached zone reports if available
2026-11-17 8:49 [PATCH 0/4] btrfs-progs: zoned: detect and repair FS with exhausted active zones Johannes Thumshirn
@ 2026-11-17 8:49 ` Johannes Thumshirn
2026-11-17 11:59 ` Damien Le Moal
2026-11-17 8:49 ` [PATCH 2/4] btrfs-progs: zoned: add btrfs_finish_dev_zone() Johannes Thumshirn
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 8:49 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Naohiro Aota, Damien Le Moal, Qu Wenruo,
Mark Korenberg, Dongjiang Zhu, Johannes Thumshirn
Since Linux 6.19, BLKREPORTZONEV2 with BLK_ZONE_REP_CACHED reports zones
from the kernel's cached zone information instead of asking the device.
Use it when the headers and the kernel support it, fall back to
BLKREPORTZONE otherwise.
Cached reports return open and closed zones as BLK_ZONE_COND_ACTIVE,
handle it where open or closed zones are counted, and provide a fallback
definition for older headers.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
kernel-shared/zoned.c | 35 +++++++++++++++++++++++++++++++----
kernel-shared/zoned.h | 5 +++++
2 files changed, 36 insertions(+), 4 deletions(-)
diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c
index b4ed4702..a06abdea 100644
--- a/kernel-shared/zoned.c
+++ b/kernel-shared/zoned.c
@@ -272,6 +272,31 @@ static int sb_write_pointer(int fd, struct blk_zone *zones, u64 *wp_ret)
return 0;
}
+/*
+ * Report zones from the kernel's cached zone information if possible
+ * (BLKREPORTZONEV2 with BLK_ZONE_REP_CACHED, since Linux 6.19), which avoids
+ * a device zone report. Open and closed zones are then reported with the
+ * BLK_ZONE_COND_ACTIVE condition. Fall back to BLKREPORTZONE if the kernel
+ * or the headers don't support it.
+ */
+static int zone_report_ioctl(int fd, struct blk_zone_report *rep)
+{
+#ifdef BLKREPORTZONEV2
+ static bool use_cached_report = true;
+
+ if (use_cached_report) {
+ rep->flags = BLK_ZONE_REP_CACHED;
+ if (ioctl(fd, BLKREPORTZONEV2, rep) == 0)
+ return 0;
+ if (errno != ENOTTY && errno != EINVAL)
+ return -1;
+ use_cached_report = false;
+ rep->flags = 0;
+ }
+#endif
+ return ioctl(fd, BLKREPORTZONE, rep);
+}
+
int btrfs_reset_dev_zone(int fd, struct blk_zone *zone)
{
struct blk_zone_range range;
@@ -285,7 +310,7 @@ int btrfs_reset_dev_zone(int fd, struct blk_zone *zone)
rep->sector = zone->start;
rep->nr_zones = 1;
- if (ioctl(fd, BLKREPORTZONE, rep) == 0 && rep->nr_zones == 1)
+ if (zone_report_ioctl(fd, rep) == 0 && rep->nr_zones == 1)
*zone = *(struct blk_zone *)(rep + 1);
if (zone->cond == BLK_ZONE_COND_EMPTY)
@@ -410,7 +435,7 @@ static int report_zones(int fd, const char *file,
rep->nr_zones = BTRFS_REPORT_NR_ZONES;
if (zinfo->model != ZONED_NONE) {
- ret = ioctl(fd, BLKREPORTZONE, rep);
+ ret = zone_report_ioctl(fd, rep);
if (ret != 0) {
error("zoned: ioctl BLKREPORTZONE failed (%m)");
exit(1);
@@ -441,6 +466,7 @@ static int report_zones(int fd, const char *file,
case BLK_ZONE_COND_IMP_OPEN:
case BLK_ZONE_COND_EXP_OPEN:
case BLK_ZONE_COND_CLOSED:
+ case BLK_ZONE_COND_ACTIVE:
set_bit(nreported, zinfo->active_zones);
nactive++;
break;
@@ -511,7 +537,8 @@ int btrfs_reset_zones(int fd, struct btrfs_zoned_device_info *zinfo, u64 byte_co
continue;
if (cond == BLK_ZONE_COND_IMP_OPEN ||
cond == BLK_ZONE_COND_EXP_OPEN ||
- cond == BLK_ZONE_COND_CLOSED)
+ cond == BLK_ZONE_COND_CLOSED ||
+ cond == BLK_ZONE_COND_ACTIVE)
return EBUSY;
}
@@ -669,7 +696,7 @@ size_t btrfs_sb_io(int fd, void *buf, off_t offset, int rw)
rep->sector = zone_num * (sector_t)zone_size_sector;
rep->nr_zones = 2;
- ret = ioctl(fd, BLKREPORTZONE, rep);
+ ret = zone_report_ioctl(fd, rep);
if (ret) {
if (errno == ENOTTY || errno == EINVAL) {
/*
diff --git a/kernel-shared/zoned.h b/kernel-shared/zoned.h
index 2d01da7d..7dd86e4c 100644
--- a/kernel-shared/zoned.h
+++ b/kernel-shared/zoned.h
@@ -31,6 +31,11 @@ struct btrfs_fs_info;
#ifdef BTRFS_ZONED
#include <linux/blkzoned.h>
+
+/* Reported by cached zone reports for open and closed zones, Linux 6.19 */
+#ifndef BLK_ZONE_COND_ACTIVE
+#define BLK_ZONE_COND_ACTIVE 0xFF
+#endif
#else
struct blk_zone {
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 1/4] btrfs-progs: zoned: use cached zone reports if available
2026-11-17 8:49 ` [PATCH 1/4] btrfs-progs: zoned: use cached zone reports if available Johannes Thumshirn
@ 2026-11-17 11:59 ` Damien Le Moal
0 siblings, 0 replies; 8+ messages in thread
From: Damien Le Moal @ 2026-11-17 11:59 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs
Cc: David Sterba, Naohiro Aota, Qu Wenruo, Mark Korenberg,
Dongjiang Zhu
On 11/17/26 00:49, Johannes Thumshirn wrote:
> Since Linux 6.19, BLKREPORTZONEV2 with BLK_ZONE_REP_CACHED reports zones
> from the kernel's cached zone information instead of asking the device.
> Use it when the headers and the kernel support it, fall back to
> BLKREPORTZONE otherwise.
>
> Cached reports return open and closed zones as BLK_ZONE_COND_ACTIVE,
> handle it where open or closed zones are counted, and provide a fallback
> definition for older headers.
>
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/4] btrfs-progs: zoned: add btrfs_finish_dev_zone()
2026-11-17 8:49 [PATCH 0/4] btrfs-progs: zoned: detect and repair FS with exhausted active zones Johannes Thumshirn
2026-11-17 8:49 ` [PATCH 1/4] btrfs-progs: zoned: use cached zone reports if available Johannes Thumshirn
@ 2026-11-17 8:49 ` Johannes Thumshirn
2026-11-17 11:59 ` Damien Le Moal
2026-11-17 8:49 ` [PATCH 3/4] btrfs-progs: zoned: add btrfs_get_max_active_zones() Johannes Thumshirn
2026-11-17 8:49 ` [PATCH 4/4] btrfs-progs: check: detect and repair exhausted zoned active zones Johannes Thumshirn
3 siblings, 1 reply; 8+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 8:49 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Naohiro Aota, Damien Le Moal, Qu Wenruo,
Mark Korenberg, Dongjiang Zhu, Johannes Thumshirn
Add a helper to finish a zone with BLKFINISHZONE and update the cached
zone state, the counterpart of btrfs_reset_dev_zone().
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
kernel-shared/zoned.c | 19 +++++++++++++++++++
kernel-shared/zoned.h | 1 +
2 files changed, 20 insertions(+)
diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c
index a06abdea..48313ccc 100644
--- a/kernel-shared/zoned.c
+++ b/kernel-shared/zoned.c
@@ -329,6 +329,25 @@ int btrfs_reset_dev_zone(int fd, struct blk_zone *zone)
return 0;
}
+int btrfs_finish_dev_zone(int fd, struct blk_zone *zone)
+{
+ struct blk_zone_range range;
+
+ if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
+ return 0;
+
+ range.sector = zone->start;
+ range.nr_sectors = zone->len;
+
+ if (ioctl(fd, BLKFINISHZONE, &range) < 0)
+ return -errno;
+
+ zone->cond = BLK_ZONE_COND_FULL;
+ zone->wp = zone->start + zone->len;
+
+ return 0;
+}
+
static int report_zones(int fd, const char *file,
struct btrfs_zoned_device_info *zinfo)
{
diff --git a/kernel-shared/zoned.h b/kernel-shared/zoned.h
index 7dd86e4c..363ef4a7 100644
--- a/kernel-shared/zoned.h
+++ b/kernel-shared/zoned.h
@@ -158,6 +158,7 @@ static inline bool btrfs_dev_is_empty_zone(struct btrfs_device *device, u64 pos)
bool zoned_profile_supported(u64 map_type, bool rst);
int btrfs_reset_dev_zone(int fd, struct blk_zone *zone);
+int btrfs_finish_dev_zone(int fd, struct blk_zone *zone);
u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
u64 hole_end, u64 num_bytes);
int btrfs_load_block_group_zone_info(struct btrfs_fs_info *fs_info,
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/4] btrfs-progs: zoned: add btrfs_get_max_active_zones()
2026-11-17 8:49 [PATCH 0/4] btrfs-progs: zoned: detect and repair FS with exhausted active zones Johannes Thumshirn
2026-11-17 8:49 ` [PATCH 1/4] btrfs-progs: zoned: use cached zone reports if available Johannes Thumshirn
2026-11-17 8:49 ` [PATCH 2/4] btrfs-progs: zoned: add btrfs_finish_dev_zone() Johannes Thumshirn
@ 2026-11-17 8:49 ` Johannes Thumshirn
2026-11-17 12:03 ` Damien Le Moal
2026-11-17 8:49 ` [PATCH 4/4] btrfs-progs: check: detect and repair exhausted zoned active zones Johannes Thumshirn
3 siblings, 1 reply; 8+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 8:49 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Naohiro Aota, Damien Le Moal, Qu Wenruo,
Mark Korenberg, Dongjiang Zhu, Johannes Thumshirn
Return the active zone limit the kernel uses for a device, the same as
btrfs_get_max_active_zones() there: the smaller non-zero of
max_active_zones and max_open_zones, a default otherwise, at least
BTRFS_MIN_ACTIVE_ZONES.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
kernel-shared/zoned.c | 23 +++++++++++++++++++++++
kernel-shared/zoned.h | 1 +
2 files changed, 24 insertions(+)
diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c
index 48313ccc..89ba544a 100644
--- a/kernel-shared/zoned.c
+++ b/kernel-shared/zoned.c
@@ -67,6 +67,7 @@ static u64 emulated_zone_size = DEFAULT_EMULATED_ZONE_SIZE;
* - 1 zone for relocation
*/
#define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
+#define BTRFS_DEFAULT_MAX_ACTIVE_ZONES 128
static int btrfs_get_dev_zone_info(struct btrfs_device *device);
@@ -155,6 +156,28 @@ static unsigned int max_active_zone_count(const char *file)
return strtoul((const char *)buf, NULL, 10);
}
+/*
+ * Same limit as btrfs_get_max_active_zones() in the kernel. Unlike the kernel
+ * this returns it instead of setting zone_info->max_active_zones, which stays
+ * the device's max_active_zones in progs.
+ */
+unsigned int btrfs_get_max_active_zones(struct btrfs_device *device)
+{
+ unsigned int max_active_zones = max_active_zone_count(device->name);
+ unsigned int max_open_zones = 0;
+ char buf[32];
+
+ if (device_get_queue_param(device->name, "max_open_zones", buf, sizeof(buf)) > 0)
+ max_open_zones = strtoul(buf, NULL, 10);
+ if (!max_active_zones || (max_open_zones && max_open_zones < max_active_zones))
+ max_active_zones = max_open_zones;
+ if (max_active_zones == 0)
+ max_active_zones = min_t(u32, device->zone_info->nr_zones / 4,
+ BTRFS_DEFAULT_MAX_ACTIVE_ZONES);
+
+ return max_t(unsigned int, max_active_zones, BTRFS_MIN_ACTIVE_ZONES);
+}
+
#ifdef BTRFS_ZONED
/*
* Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
diff --git a/kernel-shared/zoned.h b/kernel-shared/zoned.h
index 363ef4a7..e323baf1 100644
--- a/kernel-shared/zoned.h
+++ b/kernel-shared/zoned.h
@@ -99,6 +99,7 @@ static inline void btrfs_free_zoned_device_info(struct btrfs_zoned_device_info *
}
int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
+unsigned int btrfs_get_max_active_zones(struct btrfs_device *device);
int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
#ifdef BTRFS_ZONED
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 3/4] btrfs-progs: zoned: add btrfs_get_max_active_zones()
2026-11-17 8:49 ` [PATCH 3/4] btrfs-progs: zoned: add btrfs_get_max_active_zones() Johannes Thumshirn
@ 2026-11-17 12:03 ` Damien Le Moal
0 siblings, 0 replies; 8+ messages in thread
From: Damien Le Moal @ 2026-11-17 12:03 UTC (permalink / raw)
To: Johannes Thumshirn, linux-btrfs
Cc: David Sterba, Naohiro Aota, Qu Wenruo, Mark Korenberg,
Dongjiang Zhu
On 11/17/26 00:49, Johannes Thumshirn wrote:
> Return the active zone limit the kernel uses for a device, the same as
> btrfs_get_max_active_zones() there: the smaller non-zero of
> max_active_zones and max_open_zones, a default otherwise, at least
> BTRFS_MIN_ACTIVE_ZONES.
>
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> ---
> kernel-shared/zoned.c | 23 +++++++++++++++++++++++
> kernel-shared/zoned.h | 1 +
> 2 files changed, 24 insertions(+)
>
> diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c
> index 48313ccc..89ba544a 100644
> --- a/kernel-shared/zoned.c
> +++ b/kernel-shared/zoned.c
> @@ -67,6 +67,7 @@ static u64 emulated_zone_size = DEFAULT_EMULATED_ZONE_SIZE;
> * - 1 zone for relocation
> */
> #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
> +#define BTRFS_DEFAULT_MAX_ACTIVE_ZONES 128
>
> static int btrfs_get_dev_zone_info(struct btrfs_device *device);
>
> @@ -155,6 +156,28 @@ static unsigned int max_active_zone_count(const char *file)
> return strtoul((const char *)buf, NULL, 10);
> }
>
> +/*
> + * Same limit as btrfs_get_max_active_zones() in the kernel. Unlike the kernel
> + * this returns it instead of setting zone_info->max_active_zones, which stays
> + * the device's max_active_zones in progs.
> + */
> +unsigned int btrfs_get_max_active_zones(struct btrfs_device *device)
> +{
> + unsigned int max_active_zones = max_active_zone_count(device->name);
> + unsigned int max_open_zones = 0;
> + char buf[32];
> +
> + if (device_get_queue_param(device->name, "max_open_zones", buf, sizeof(buf)) > 0)
> + max_open_zones = strtoul(buf, NULL, 10);
> + if (!max_active_zones || (max_open_zones && max_open_zones < max_active_zones))
> + max_active_zones = max_open_zones;
> + if (max_active_zones == 0)
> + max_active_zones = min_t(u32, device->zone_info->nr_zones / 4,
> + BTRFS_DEFAULT_MAX_ACTIVE_ZONES);
> +
> + return max_t(unsigned int, max_active_zones, BTRFS_MIN_ACTIVE_ZONES);
I am confused here. If we endup with max_active_zones being lower than
BTRFS_MIN_ACTIVE_ZONES, because the max open zone limit is lower or the drive
indeed has a lower limit than 128, it seems to be very wrong to return the
larger BTRFS_MIN_ACTIVE_ZONES...
So shouldn't this be:
if (max_active_zones)
return max_active_zones;
return BTRFS_MIN_ACTIVE_ZONES;
> +}
> +
> #ifdef BTRFS_ZONED
> /*
> * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
> diff --git a/kernel-shared/zoned.h b/kernel-shared/zoned.h
> index 363ef4a7..e323baf1 100644
> --- a/kernel-shared/zoned.h
> +++ b/kernel-shared/zoned.h
> @@ -99,6 +99,7 @@ static inline void btrfs_free_zoned_device_info(struct btrfs_zoned_device_info *
> }
>
> int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info);
> +unsigned int btrfs_get_max_active_zones(struct btrfs_device *device);
> int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
>
> #ifdef BTRFS_ZONED
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 4/4] btrfs-progs: check: detect and repair exhausted zoned active zones
2026-11-17 8:49 [PATCH 0/4] btrfs-progs: zoned: detect and repair FS with exhausted active zones Johannes Thumshirn
` (2 preceding siblings ...)
2026-11-17 8:49 ` [PATCH 3/4] btrfs-progs: zoned: add btrfs_get_max_active_zones() Johannes Thumshirn
@ 2026-11-17 8:49 ` Johannes Thumshirn
3 siblings, 0 replies; 8+ messages in thread
From: Johannes Thumshirn @ 2026-11-17 8:49 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Naohiro Aota, Damien Le Moal, Qu Wenruo,
Mark Korenberg, Dongjiang Zhu, Johannes Thumshirn
Kernels before "btrfs: zoned: don't release active zone reservations
for conventional zones" can leave all active zones to data. The first
transaction commit after mount then can't activate a metadata
block-group and aborts with -EAGAIN.
Report when the active zones in use exceed the kernel's limit minus its
metadata reservation. With --repair, finish the fullest single profile
data zones until it fits again, plus one zone for the relocation
block-group.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
check/main.c | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 187 insertions(+)
diff --git a/check/main.c b/check/main.c
index 717d576f..f065555f 100644
--- a/check/main.c
+++ b/check/main.c
@@ -9203,6 +9203,188 @@ static int check_zoned_active_zones(void)
return ret;
}
+
+static bool zone_is_active(const struct blk_zone *zone)
+{
+ if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
+ return false;
+
+ switch (zone->cond) {
+ case BLK_ZONE_COND_IMP_OPEN:
+ case BLK_ZONE_COND_EXP_OPEN:
+ case BLK_ZONE_COND_CLOSED:
+ case BLK_ZONE_COND_ACTIVE:
+ return true;
+ default:
+ return false;
+ }
+}
+
+struct finish_candidate {
+ u32 zno;
+ u64 left;
+};
+
+static int cmp_finish_candidate(const void *a, const void *b)
+{
+ const struct finish_candidate *ca = a, *cb = b;
+
+ return ca->left < cb->left ? -1 : ca->left > cb->left;
+}
+
+/*
+ * The kernel keeps active zones in reserve for a metadata, a tree-log and a
+ * system block-group, and only activates metadata block-groups when writing
+ * them. If more zones are active on disk than the kernel's limit minus that
+ * reservation, the first transaction commit after mount can't activate a
+ * metadata block-group and aborts with -EAGAIN. Older kernels could leave
+ * filesystems in that state.
+ *
+ * Mirror the kernel's limit and reservation, see btrfs_get_max_active_zones()
+ * and btrfs_check_active_zone_reservation(), and finish the zones of the
+ * fullest single profile data block-groups until it fits again. Leave one
+ * zone more, for the data relocation block-group activated on mount.
+ *
+ * Return 0 if fine or repaired, 1 if a problem was found, <0 on error.
+ */
+static int check_dev_active_reservation(struct btrfs_device *dev, int reserved)
+{
+ struct btrfs_root *dev_root = gfs_info->dev_root;
+ struct btrfs_zoned_device_info *zinfo = dev->zone_info;
+ struct finish_candidate *cand;
+ BTRFS_PATH_AUTO_RELEASE(path);
+ struct btrfs_key key;
+ int max_active, nactive = 0, ncand = 0;
+ int shift = ilog2(zinfo->zone_size);
+ int left, need;
+ int ret;
+
+ max_active = btrfs_get_max_active_zones(dev);
+ for (int i = 0; i < zinfo->nr_zones; i++)
+ nactive += zone_is_active(&zinfo->zones[i]);
+ /* The kernel disables active zone tracking in this case. */
+ if (nactive > max_active && !zinfo->max_active_zones)
+ return 0;
+
+ cand = calloc(zinfo->nr_zones, sizeof(*cand));
+ if (!cand)
+ return -ENOMEM;
+
+ key.objectid = dev->devid;
+ key.type = BTRFS_DEV_EXTENT_KEY;
+ key.offset = 0;
+ ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
+ if (ret < 0)
+ goto out;
+
+ while (1) {
+ struct btrfs_block_group *bg;
+ struct btrfs_dev_extent *devext;
+ u64 pstart, plen, chunk;
+
+ if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
+ ret = btrfs_next_leaf(dev_root, &path);
+ if (ret < 0)
+ goto out;
+ if (ret > 0)
+ break;
+ }
+
+ btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+ if (key.objectid != dev->devid || key.type != BTRFS_DEV_EXTENT_KEY)
+ break;
+
+ devext = btrfs_item_ptr(path.nodes[0], path.slots[0],
+ struct btrfs_dev_extent);
+ pstart = key.offset;
+ plen = btrfs_dev_extent_length(path.nodes[0], devext);
+ chunk = btrfs_dev_extent_chunk_offset(path.nodes[0], devext);
+ bg = btrfs_lookup_block_group(gfs_info, chunk);
+ path.slots[0]++;
+ if (!bg)
+ continue;
+
+ for (u64 zno = pstart >> shift;
+ zno <= (pstart + plen - 1) >> shift && zno < zinfo->nr_zones;
+ zno++) {
+ struct blk_zone *zone = &zinfo->zones[zno];
+
+ if (!zone_is_active(zone))
+ continue;
+ if (bg->flags & (BTRFS_BLOCK_GROUP_METADATA |
+ BTRFS_BLOCK_GROUP_SYSTEM)) {
+ reserved--;
+ } else if (!(bg->flags & BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
+ cand[ncand].zno = zno;
+ cand[ncand].left = (zone->start + zone->capacity -
+ zone->wp) << SECTOR_SHIFT;
+ ncand++;
+ }
+ }
+ }
+
+ ret = 0;
+ reserved = max(reserved, 0);
+ left = max_active - nactive;
+ if (left >= reserved)
+ goto out;
+ need = reserved + 1 - left;
+
+ ret = 1;
+ error(
+"zoned: devid %llu has %d of %d active zones in use and %d reserved for metadata, the first commit after mount can fail",
+ dev->devid, nactive, max_active, reserved);
+ if (!opt_check_repair)
+ goto out;
+
+ if (ncand < need) {
+ error("zoned: only %d data zones on devid %llu can be finished, need %d",
+ ncand, dev->devid, need);
+ goto out;
+ }
+
+ qsort(cand, ncand, sizeof(*cand), cmp_finish_candidate);
+ for (int i = 0; i < need; i++) {
+ struct blk_zone *zone = &zinfo->zones[cand[i].zno];
+
+ ret = btrfs_finish_dev_zone(dev->fd, zone);
+ if (ret) {
+ errno = -ret;
+ error("zoned: failed to finish zone %u on devid %llu: %m",
+ cand[i].zno, dev->devid);
+ goto out;
+ }
+ printf("Finished zone %u on devid %llu, %llu bytes left unused\n",
+ cand[i].zno, dev->devid, cand[i].left);
+ }
+out:
+ free(cand);
+ return ret;
+}
+
+static int check_zoned_active_reservation(void)
+{
+ struct btrfs_device *dev;
+ int reserved;
+ int ret = 0;
+
+ reserved = (gfs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP) ? 4 : 2;
+ reserved += (gfs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP) ? 2 : 1;
+
+ list_for_each_entry(dev, &gfs_info->fs_devices->devices, dev_list) {
+ int err;
+
+ if (!dev->zone_info || dev->zone_info->model != ZONED_HOST_MANAGED)
+ continue;
+
+ err = check_dev_active_reservation(dev, reserved);
+ if (err < 0)
+ return err;
+ ret |= err;
+ }
+
+ return ret;
+}
#endif
/*
@@ -9219,6 +9401,7 @@ static int check_zoned_active_zones(void)
static int check_zoned(void)
{
int ret = 0;
+ int err __maybe_unused;
if (!btrfs_is_zoned(gfs_info))
return 0;
@@ -9227,6 +9410,10 @@ static int check_zoned(void)
ret = check_zoned_active_zones();
if (ret < 0)
return ret;
+ err = check_zoned_active_reservation();
+ if (err < 0)
+ return err;
+ ret |= err;
#endif
return ret;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 8+ messages in thread