From: Qu Wenruo <wqu@suse.com>
To: Johannes Thumshirn <johannes.thumshirn@wdc.com>,
linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>,
Naohiro Aota <naohiro.aota@wdc.com>,
Damien Le Moal <dlemoal@kernel.org>,
Mark Korenberg <socketpair@gmail.com>,
Dongjiang Zhu <zhudongjiang@fygo.io>
Subject: Re: [PATCH 4/4] btrfs-progs: check: detect and repair exhausted zoned active zones
Date: Wed, 18 Nov 2026 18:10:59 +1030 [thread overview]
Message-ID: <5739cbcc-bb71-4e9f-95c3-bd2e18d44e53@suse.com> (raw)
In-Reply-To: <20261117084948.3848762-5-johannes.thumshirn@wdc.com>
在 2026/11/17 19:19, 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);
Shoudln't zinfo->active_zones or active_zones_left also to be updated
here or by the btrfs_finish_dev_zone() itself?
> + 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;
> }
prev parent reply other threads:[~2026-11-18 7:41 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 11:59 ` Damien Le Moal
2026-11-17 8:49 ` [PATCH 2/4] btrfs-progs: zoned: add btrfs_finish_dev_zone() 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 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
2026-11-18 7:40 ` Qu Wenruo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5739cbcc-bb71-4e9f-95c3-bd2e18d44e53@suse.com \
--to=wqu@suse.com \
--cc=dlemoal@kernel.org \
--cc=dsterba@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=naohiro.aota@wdc.com \
--cc=socketpair@gmail.com \
--cc=zhudongjiang@fygo.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox