public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: Christoph Hellwig <hch@lst.de>
Cc: Andrey Albershteyn <aalbersh@kernel.org>,
	Damien Le Moal <dlemoal@kernel.org>,
	Carlos Maiolino <cem@kernel.org>,
	linux-xfs@vger.kernel.org
Subject: Re: [PATCH 3/4] libfrog: lift common zone reporting code from mkfs and repair
Date: Fri, 9 Jan 2026 08:31:36 -0800	[thread overview]
Message-ID: <20260109163136.GS15551@frogsfrogsfrogs> (raw)
In-Reply-To: <20260109162324.2386829-4-hch@lst.de>

On Fri, Jan 09, 2026 at 05:22:54PM +0100, Christoph Hellwig wrote:
> From: Damien Le Moal <dlemoal@kernel.org>
> 
> Define the new helper function xfrog_report_zones() to report zones of
> a zoned block device.  This function is implemented in the new file
> libfrog/zones.c and defined in the header file libfrog/zones.h and
> use it from mkfs and repair instead of the previous open coded versions.
> 
> xfrog_report_zones() allocates and returns a struct blk_zone_report
> structure, which can be be reused by subsequent invocations.  It is the
> responsibility of the caller to free this structure after use.
> 
> Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
> [hch: refactored to allow buffer reuse]
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Looks good now, thanx for avoiding the buffer reallocations
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>

--D

> ---
>  libfrog/Makefile |  6 ++++--
>  libfrog/zones.c  | 39 +++++++++++++++++++++++++++++++++++++++
>  libfrog/zones.h  | 18 ++++++++++++++++++
>  mkfs/xfs_mkfs.c  | 41 ++++++++++++-----------------------------
>  repair/zoned.c   | 35 +++++++++++------------------------
>  5 files changed, 84 insertions(+), 55 deletions(-)
>  create mode 100644 libfrog/zones.c
>  create mode 100644 libfrog/zones.h
> 
> diff --git a/libfrog/Makefile b/libfrog/Makefile
> index 268fa26638d7..9f405ffe3475 100644
> --- a/libfrog/Makefile
> +++ b/libfrog/Makefile
> @@ -35,7 +35,8 @@ radix-tree.c \
>  randbytes.c \
>  scrub.c \
>  util.c \
> -workqueue.c
> +workqueue.c \
> +zones.c
>  
>  HFILES = \
>  avl64.h \
> @@ -65,7 +66,8 @@ radix-tree.h \
>  randbytes.h \
>  scrub.h \
>  statx.h \
> -workqueue.h
> +workqueue.h \
> +zones.h
>  
>  GETTEXT_PY = \
>  	gettext.py
> diff --git a/libfrog/zones.c b/libfrog/zones.c
> new file mode 100644
> index 000000000000..2276c56bec9c
> --- /dev/null
> +++ b/libfrog/zones.c
> @@ -0,0 +1,39 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2025, Western Digital Corporation or its affiliates.
> + */
> +#include "platform_defs.h"
> +#include "libfrog/zones.h"
> +#include <sys/ioctl.h>
> +
> +/* random size that allows efficient processing */
> +#define ZONES_PER_REPORT		16384
> +
> +struct xfrog_zone_report *
> +xfrog_report_zones(
> +	int			fd,
> +	uint64_t		sector,
> +	struct xfrog_zone_report *rep)
> +{
> +	if (!rep) {
> +		rep = calloc(1, struct_size(rep, zones, ZONES_PER_REPORT));
> +		if (!rep) {
> +			fprintf(stderr, "%s\n",
> +_("Failed to allocate memory for reporting zones."));
> +			return NULL;
> +		}
> +	}
> +
> +	rep->rep.sector = sector;
> +	rep->rep.nr_zones = ZONES_PER_REPORT;
> +
> +	if (ioctl(fd, BLKREPORTZONE, &rep->rep)) {
> +		fprintf(stderr, "%s %s\n",
> +_("ioctl(BLKREPORTZONE) failed:\n"),
> +			strerror(-errno));
> +		free(rep);
> +		return NULL;
> +	}
> +
> +	return rep;
> +}
> diff --git a/libfrog/zones.h b/libfrog/zones.h
> new file mode 100644
> index 000000000000..33c1da7ef192
> --- /dev/null
> +++ b/libfrog/zones.h
> @@ -0,0 +1,18 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2025, Western Digital Corporation or its affiliates.
> + */
> +#ifndef __LIBFROG_ZONES_H__
> +#define __LIBFROG_ZONES_H__
> +
> +#include <linux/blkzoned.h>
> +
> +struct xfrog_zone_report {
> +	struct blk_zone_report	rep;
> +	struct blk_zone		zones[];
> +};
> +
> +struct xfrog_zone_report *
> +xfrog_report_zones(int fd, uint64_t sector, struct xfrog_zone_report *rep);
> +
> +#endif /* __LIBFROG_ZONES_H__ */
> diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
> index 215cff8db7b1..9c165f29c298 100644
> --- a/mkfs/xfs_mkfs.c
> +++ b/mkfs/xfs_mkfs.c
> @@ -15,6 +15,7 @@
>  #include "libfrog/crc32cselftest.h"
>  #include "libfrog/dahashselftest.h"
>  #include "libfrog/fsproperties.h"
> +#include "libfrog/zones.h"
>  #include "proto.h"
>  #include <ini.h>
>  
> @@ -2542,9 +2543,6 @@ struct zone_topology {
>  	struct zone_info	log;
>  };
>  
> -/* random size that allows efficient processing */
> -#define ZONES_PER_IOCTL			16384
> -
>  static void
>  zone_validate_capacity(
>  	struct zone_info	*zi,
> @@ -2572,12 +2570,11 @@ report_zones(
>  	const char		*name,
>  	struct zone_info	*zi)
>  {
> -	struct blk_zone_report	*rep;
> +	struct xfrog_zone_report *rep = NULL;
>  	bool			found_seq = false;
> -	int			fd, ret = 0;
> +	int			fd;
>  	uint64_t		device_size;
>  	uint64_t		sector = 0;
> -	size_t			rep_size;
>  	unsigned int		i, n = 0;
>  	struct stat		st;
>  
> @@ -2604,32 +2601,18 @@ report_zones(
>  	zi->nr_zones = device_size / zi->zone_size;
>  	zi->nr_conv_zones = 0;
>  
> -	rep_size = sizeof(struct blk_zone_report) +
> -		   sizeof(struct blk_zone) * ZONES_PER_IOCTL;
> -	rep = malloc(rep_size);
> -	if (!rep) {
> -		fprintf(stderr,
> -_("Failed to allocate memory for zone reporting.\n"));
> -		exit(1);
> -	}
> -
>  	while (n < zi->nr_zones) {
> -		struct blk_zone *zones = (struct blk_zone *)(rep + 1);
> +		struct blk_zone *zones;
>  
> -		memset(rep, 0, rep_size);
> -		rep->sector = sector;
> -		rep->nr_zones = ZONES_PER_IOCTL;
> -
> -		ret = ioctl(fd, BLKREPORTZONE, rep);
> -		if (ret) {
> -			fprintf(stderr,
> -_("ioctl(BLKREPORTZONE) failed: %d!\n"), -errno);
> +		rep = xfrog_report_zones(fd, sector, rep);
> +		if (!rep)
>  			exit(1);
> -		}
> -		if (!rep->nr_zones)
> +
> +		if (!rep->rep.nr_zones)
>  			break;
>  
> -		for (i = 0; i < rep->nr_zones; i++) {
> +		zones = rep->zones;
> +		for (i = 0; i < rep->rep.nr_zones; i++) {
>  			if (n >= zi->nr_zones)
>  				break;
>  
> @@ -2676,8 +2659,8 @@ _("Unknown zone type (0x%x) found.\n"), zones[i].type);
>  
>  			n++;
>  		}
> -		sector = zones[rep->nr_zones - 1].start +
> -			 zones[rep->nr_zones - 1].len;
> +		sector = zones[rep->rep.nr_zones - 1].start +
> +			 zones[rep->rep.nr_zones - 1].len;
>  	}
>  
>  	free(rep);
> diff --git a/repair/zoned.c b/repair/zoned.c
> index 206b0158f95f..5102d43e218d 100644
> --- a/repair/zoned.c
> +++ b/repair/zoned.c
> @@ -7,6 +7,7 @@
>  #include "libxfs_priv.h"
>  #include "libxfs.h"
>  #include "xfs_zones.h"
> +#include "libfrog/zones.h"
>  #include "err_protos.h"
>  #include "zoned.h"
>  
> @@ -51,8 +52,7 @@ check_zones(
>  	uint64_t		sector = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rtstart);
>  	unsigned int		zone_size, zone_capacity;
>  	uint64_t		device_size;
> -	size_t			rep_size;
> -	struct blk_zone_report	*rep;
> +	struct xfrog_zone_report *rep = NULL;
>  	unsigned int		i, n = 0;
>  
>  	if (ioctl(fd, BLKGETSIZE64, &device_size))
> @@ -67,31 +67,18 @@ check_zones(
>  		return;
>  	}
>  
> -	rep_size = sizeof(struct blk_zone_report) +
> -		   sizeof(struct blk_zone) * ZONES_PER_IOCTL;
> -	rep = malloc(rep_size);
> -	if (!rep) {
> -		do_warn(_("malloc failed for zone report\n"));
> -		return;
> -	}
> -
>  	while (n < mp->m_sb.sb_rgcount) {
> -		struct blk_zone *zones = (struct blk_zone *)(rep + 1);
> -		int ret;
> +		struct blk_zone *zones;
>  
> -		memset(rep, 0, rep_size);
> -		rep->sector = sector;
> -		rep->nr_zones = ZONES_PER_IOCTL;
> +		rep = xfrog_report_zones(fd, sector, rep);
> +		if (!rep)
> +			return;
>  
> -		ret = ioctl(fd, BLKREPORTZONE, rep);
> -		if (ret) {
> -			do_error(_("ioctl(BLKREPORTZONE) failed: %d!\n"), ret);
> -			goto out_free;
> -		}
> -		if (!rep->nr_zones)
> +		if (!rep->rep.nr_zones)
>  			break;
>  
> -		for (i = 0; i < rep->nr_zones; i++) {
> +		zones = rep->zones;
> +		for (i = 0; i < rep->rep.nr_zones; i++) {
>  			if (n >= mp->m_sb.sb_rgcount)
>  				break;
>  
> @@ -130,8 +117,8 @@ _("Inconsistent zone capacity!\n"));
>  			report_zones_cb(mp, &zones[i]);
>  			n++;
>  		}
> -		sector = zones[rep->nr_zones - 1].start +
> -			 zones[rep->nr_zones - 1].len;
> +		sector = zones[rep->rep.nr_zones - 1].start +
> +			 zones[rep->rep.nr_zones - 1].len;
>  	}
>  
>  out_free:
> -- 
> 2.47.3
> 
> 

  reply	other threads:[~2026-01-09 16:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-09 16:22 enable cached zone report v4 Christoph Hellwig
2026-01-09 16:22 ` [PATCH 1/4] xfs: use blkdev_report_zones_cached() Christoph Hellwig
2026-01-20 14:28   ` Andrey Albershteyn
2026-01-20 17:31     ` Darrick J. Wong
2026-01-21  6:49       ` Christoph Hellwig
2026-01-27 16:39         ` [PATCH] libfrog: adjust header order for BLK_ZONE_COND_ACTIVE #ifndef check Andrey Albershteyn
2026-01-27 16:43           ` Christoph Hellwig
2026-01-27 16:55             ` Andrey Albershteyn
2026-01-27 16:59               ` Andrey Albershteyn
2026-01-27 17:01                 ` Christoph Hellwig
2026-01-09 16:22 ` [PATCH 2/4] mkfs: remove unnecessary return value affectation Christoph Hellwig
2026-01-09 16:22 ` [PATCH 3/4] libfrog: lift common zone reporting code from mkfs and repair Christoph Hellwig
2026-01-09 16:31   ` Darrick J. Wong [this message]
2026-01-09 16:22 ` [PATCH 4/4] libfrog: enable cached report zones Christoph Hellwig
2026-01-09 16:32   ` Darrick J. Wong
2026-01-12 11:02 ` enable cached zone report v4 Damien Le Moal

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=20260109163136.GS15551@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=aalbersh@kernel.org \
    --cc=cem@kernel.org \
    --cc=dlemoal@kernel.org \
    --cc=hch@lst.de \
    --cc=linux-xfs@vger.kernel.org \
    /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