diff mbox series

[v2,1/3] blkid: implement zone-aware probing

Message ID 20210414013339.2936229-2-naohiro.aota@wdc.com (mailing list archive)
State New, archived
Headers show
Series implement zone-aware probing/wiping for zoned btrfs | expand

Commit Message

Naohiro Aota April 14, 2021, 1:33 a.m. UTC
This patch makes libblkid zone-aware. It can probe the magic located at
some offset from the beginning of some specific zone of a device.

Ths patch introduces some new fields to struct blkid_idmag. They indicate
the magic location is placed related to a zone, and the offset in the zone.

Also, this commit introduces `zone_size` to struct blkid_struct_probe. It
stores the size of zones of a device.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 configure.ac          |  1 +
 libblkid/src/blkidP.h |  5 +++++
 libblkid/src/probe.c  | 29 +++++++++++++++++++++++++++--
 3 files changed, 33 insertions(+), 2 deletions(-)

Comments

Damien Le Moal April 14, 2021, 9:14 a.m. UTC | #1
On 2021/04/14 10:33, Naohiro Aota wrote:
> This patch makes libblkid zone-aware. It can probe the magic located at
> some offset from the beginning of some specific zone of a device.
> 
> Ths patch introduces some new fields to struct blkid_idmag. They indicate
> the magic location is placed related to a zone, and the offset in the zone.
> 
> Also, this commit introduces `zone_size` to struct blkid_struct_probe. It
> stores the size of zones of a device.
> 
> Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
> ---
>  configure.ac          |  1 +
>  libblkid/src/blkidP.h |  5 +++++
>  libblkid/src/probe.c  | 29 +++++++++++++++++++++++++++--
>  3 files changed, 33 insertions(+), 2 deletions(-)
> 
> diff --git a/configure.ac b/configure.ac
> index bebb4085425a..52b164e834db 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -302,6 +302,7 @@ AC_CHECK_HEADERS([ \
>  	lastlog.h \
>  	libutil.h \
>  	linux/btrfs.h \
> +	linux/blkzoned.h \
>  	linux/capability.h \
>  	linux/cdrom.h \
>  	linux/falloc.h \
> diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h
> index a3fe6748a969..e3a160aa97c0 100644
> --- a/libblkid/src/blkidP.h
> +++ b/libblkid/src/blkidP.h
> @@ -150,6 +150,10 @@ struct blkid_idmag
>  	const char	*hoff;		/* hint which contains byte offset to kboff */
>  	long		kboff;		/* kilobyte offset of superblock */
>  	unsigned int	sboff;		/* byte offset within superblock */
> +
> +	int		is_zoned;	/* indicate magic location is calcluated based on zone position  */
> +	long		zonenum;	/* zone number which has superblock */
> +	long		kboff_inzone;	/* kilobyte offset of superblock in a zone */
>  };
>  
>  /*
> @@ -206,6 +210,7 @@ struct blkid_struct_probe
>  	dev_t			disk_devno;	/* devno of the whole-disk or 0 */
>  	unsigned int		blkssz;		/* sector size (BLKSSZGET ioctl) */
>  	mode_t			mode;		/* struct stat.sb_mode */
> +	uint64_t		zone_size;	/* zone size (BLKGETZONESZ ioctl) */
>  
>  	int			flags;		/* private library flags */
>  	int			prob_flags;	/* always zeroized by blkid_do_*() */
> diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
> index a47a8720d4ac..9d180aab5242 100644
> --- a/libblkid/src/probe.c
> +++ b/libblkid/src/probe.c
> @@ -94,6 +94,9 @@
>  #ifdef HAVE_LINUX_CDROM_H
>  #include <linux/cdrom.h>
>  #endif
> +#ifdef HAVE_LINUX_BLKZONED_H
> +#include <linux/blkzoned.h>
> +#endif
>  #ifdef HAVE_SYS_STAT_H
>  #include <sys/stat.h>
>  #endif
> @@ -897,6 +900,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
>  	pr->wipe_off = 0;
>  	pr->wipe_size = 0;
>  	pr->wipe_chain = NULL;
> +	pr->zone_size = 0;
>  
>  	if (fd < 0)
>  		return 1;
> @@ -996,6 +1000,15 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
>  #endif
>  	free(dm_uuid);
>  
> +# ifdef HAVE_LINUX_BLKZONED_H
> +	if (S_ISBLK(sb.st_mode)) {
> +		uint32_t zone_size_sector;
> +
> +		if (!ioctl(pr->fd, BLKGETZONESZ, &zone_size_sector))
> +			pr->zone_size = zone_size_sector << 9;
> +	}
> +# endif
> +
>  	DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64"",
>  				pr->off, pr->size));
>  	DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
> @@ -1064,12 +1077,24 @@ int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
>  	/* try to detect by magic string */
>  	while(mag && mag->magic) {
>  		unsigned char *buf;
> +		uint64_t kboff;
>  		uint64_t hint_offset;
>  
>  		if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0)
>  			hint_offset = 0;
>  
> -		off = hint_offset + ((mag->kboff + (mag->sboff >> 10)) << 10);
> +		/* If the magic is for zoned device, skip non-zoned device */
> +		if (mag->is_zoned && !pr->zone_size) {
> +			mag++;
> +			continue;
> +		}
> +
> +		if (!mag->is_zoned)
> +			kboff = mag->kboff;
> +		else
> +			kboff = ((mag->zonenum * pr->zone_size) >> 10) + mag->kboff_inzone;
> +
> +		off = hint_offset + ((kboff + (mag->sboff >> 10)) << 10);
>  		buf = blkid_probe_get_buffer(pr, off, 1024);
>  
>  		if (!buf && errno)
> @@ -1079,7 +1104,7 @@ int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
>  				buf + (mag->sboff & 0x3ff), mag->len)) {
>  
>  			DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld",
> -				mag->sboff, mag->kboff));
> +				mag->sboff, kboff));
>  			if (offset)
>  				*offset = off + (mag->sboff & 0x3ff);
>  			if (res)
> 

Looks OK to me.

Reviewed-by: Damien Le Moal <damien.lemoal@wdc.com>
Johannes Thumshirn April 14, 2021, 9:17 a.m. UTC | #2
Looks good,
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Karel Zak April 14, 2021, 1:31 p.m. UTC | #3
On Wed, Apr 14, 2021 at 10:33:37AM +0900, Naohiro Aota wrote:
> --- a/configure.ac
> +++ b/configure.ac
> @@ -302,6 +302,7 @@ AC_CHECK_HEADERS([ \
>  	lastlog.h \
>  	libutil.h \
>  	linux/btrfs.h \
> +	linux/blkzoned.h \

unnecessary, there is already AC_CHECK_HEADERS([linux/blkzoned.h]) on
another place.

>  	linux/capability.h \
>  	linux/cdrom.h \
>  	linux/falloc.h \
> diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h
> index a3fe6748a969..e3a160aa97c0 100644
> --- a/libblkid/src/blkidP.h
> +++ b/libblkid/src/blkidP.h
> @@ -150,6 +150,10 @@ struct blkid_idmag
>  	const char	*hoff;		/* hint which contains byte offset to kboff */
>  	long		kboff;		/* kilobyte offset of superblock */
>  	unsigned int	sboff;		/* byte offset within superblock */
> +
> +	int		is_zoned;	/* indicate magic location is calcluated based on zone position  */
> +	long		zonenum;	/* zone number which has superblock */
> +	long		kboff_inzone;	/* kilobyte offset of superblock in a zone */

It would be better to use 'flags' struct field and

  #define BLKID_FL_ZONED_DEV (1 << 6)

like we use for another stuff.

> diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
> index a47a8720d4ac..9d180aab5242 100644
> --- a/libblkid/src/probe.c
> +++ b/libblkid/src/probe.c
> @@ -94,6 +94,9 @@
>  #ifdef HAVE_LINUX_CDROM_H
>  #include <linux/cdrom.h>
>  #endif
> +#ifdef HAVE_LINUX_BLKZONED_H
> +#include <linux/blkzoned.h>
> +#endif
>  #ifdef HAVE_SYS_STAT_H
>  #include <sys/stat.h>
>  #endif
> @@ -897,6 +900,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
>  	pr->wipe_off = 0;
>  	pr->wipe_size = 0;
>  	pr->wipe_chain = NULL;
> +	pr->zone_size = 0;

you also need to update blkid_clone_probe() function

  Karel
Naohiro Aota April 14, 2021, 3:03 p.m. UTC | #4
On Wed, Apr 14, 2021 at 03:31:01PM +0200, Karel Zak wrote:
> On Wed, Apr 14, 2021 at 10:33:37AM +0900, Naohiro Aota wrote:
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -302,6 +302,7 @@ AC_CHECK_HEADERS([ \
> >  	lastlog.h \
> >  	libutil.h \
> >  	linux/btrfs.h \
> > +	linux/blkzoned.h \
> 
> unnecessary, there is already AC_CHECK_HEADERS([linux/blkzoned.h]) on
> another place.

Ah, I missed that. I will drop it.

> >  	linux/capability.h \
> >  	linux/cdrom.h \
> >  	linux/falloc.h \
> > diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h
> > index a3fe6748a969..e3a160aa97c0 100644
> > --- a/libblkid/src/blkidP.h
> > +++ b/libblkid/src/blkidP.h
> > @@ -150,6 +150,10 @@ struct blkid_idmag
> >  	const char	*hoff;		/* hint which contains byte offset to kboff */
> >  	long		kboff;		/* kilobyte offset of superblock */
> >  	unsigned int	sboff;		/* byte offset within superblock */
> > +
> > +	int		is_zoned;	/* indicate magic location is calcluated based on zone position  */
> > +	long		zonenum;	/* zone number which has superblock */
> > +	long		kboff_inzone;	/* kilobyte offset of superblock in a zone */
> 
> It would be better to use 'flags' struct field and
> 
>   #define BLKID_FL_ZONED_DEV (1 << 6)
> 
> like we use for another stuff.

BLKID_FL_* flags looks like to indicate a device's property. Instead,
this one indicates a magic is placed relative to a zone. I do not see
blkid_idmag is currently using "flags" filed. Should we really add it
and follow the flag style? I thought, we can do it later when other
use case exists.

> > diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
> > index a47a8720d4ac..9d180aab5242 100644
> > --- a/libblkid/src/probe.c
> > +++ b/libblkid/src/probe.c
> > @@ -94,6 +94,9 @@
> >  #ifdef HAVE_LINUX_CDROM_H
> >  #include <linux/cdrom.h>
> >  #endif
> > +#ifdef HAVE_LINUX_BLKZONED_H
> > +#include <linux/blkzoned.h>
> > +#endif
> >  #ifdef HAVE_SYS_STAT_H
> >  #include <sys/stat.h>
> >  #endif
> > @@ -897,6 +900,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
> >  	pr->wipe_off = 0;
> >  	pr->wipe_size = 0;
> >  	pr->wipe_chain = NULL;
> > +	pr->zone_size = 0;
> 
> you also need to update blkid_clone_probe() function

Will do. I completely missed that. Thanks.

>   Karel
> 
> -- 
>  Karel Zak  <kzak@redhat.com>
>  http://karelzak.blogspot.com
>
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index bebb4085425a..52b164e834db 100644
--- a/configure.ac
+++ b/configure.ac
@@ -302,6 +302,7 @@  AC_CHECK_HEADERS([ \
 	lastlog.h \
 	libutil.h \
 	linux/btrfs.h \
+	linux/blkzoned.h \
 	linux/capability.h \
 	linux/cdrom.h \
 	linux/falloc.h \
diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h
index a3fe6748a969..e3a160aa97c0 100644
--- a/libblkid/src/blkidP.h
+++ b/libblkid/src/blkidP.h
@@ -150,6 +150,10 @@  struct blkid_idmag
 	const char	*hoff;		/* hint which contains byte offset to kboff */
 	long		kboff;		/* kilobyte offset of superblock */
 	unsigned int	sboff;		/* byte offset within superblock */
+
+	int		is_zoned;	/* indicate magic location is calcluated based on zone position  */
+	long		zonenum;	/* zone number which has superblock */
+	long		kboff_inzone;	/* kilobyte offset of superblock in a zone */
 };
 
 /*
@@ -206,6 +210,7 @@  struct blkid_struct_probe
 	dev_t			disk_devno;	/* devno of the whole-disk or 0 */
 	unsigned int		blkssz;		/* sector size (BLKSSZGET ioctl) */
 	mode_t			mode;		/* struct stat.sb_mode */
+	uint64_t		zone_size;	/* zone size (BLKGETZONESZ ioctl) */
 
 	int			flags;		/* private library flags */
 	int			prob_flags;	/* always zeroized by blkid_do_*() */
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
index a47a8720d4ac..9d180aab5242 100644
--- a/libblkid/src/probe.c
+++ b/libblkid/src/probe.c
@@ -94,6 +94,9 @@ 
 #ifdef HAVE_LINUX_CDROM_H
 #include <linux/cdrom.h>
 #endif
+#ifdef HAVE_LINUX_BLKZONED_H
+#include <linux/blkzoned.h>
+#endif
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
@@ -897,6 +900,7 @@  int blkid_probe_set_device(blkid_probe pr, int fd,
 	pr->wipe_off = 0;
 	pr->wipe_size = 0;
 	pr->wipe_chain = NULL;
+	pr->zone_size = 0;
 
 	if (fd < 0)
 		return 1;
@@ -996,6 +1000,15 @@  int blkid_probe_set_device(blkid_probe pr, int fd,
 #endif
 	free(dm_uuid);
 
+# ifdef HAVE_LINUX_BLKZONED_H
+	if (S_ISBLK(sb.st_mode)) {
+		uint32_t zone_size_sector;
+
+		if (!ioctl(pr->fd, BLKGETZONESZ, &zone_size_sector))
+			pr->zone_size = zone_size_sector << 9;
+	}
+# endif
+
 	DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64"",
 				pr->off, pr->size));
 	DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
@@ -1064,12 +1077,24 @@  int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
 	/* try to detect by magic string */
 	while(mag && mag->magic) {
 		unsigned char *buf;
+		uint64_t kboff;
 		uint64_t hint_offset;
 
 		if (!mag->hoff || blkid_probe_get_hint(pr, mag->hoff, &hint_offset) < 0)
 			hint_offset = 0;
 
-		off = hint_offset + ((mag->kboff + (mag->sboff >> 10)) << 10);
+		/* If the magic is for zoned device, skip non-zoned device */
+		if (mag->is_zoned && !pr->zone_size) {
+			mag++;
+			continue;
+		}
+
+		if (!mag->is_zoned)
+			kboff = mag->kboff;
+		else
+			kboff = ((mag->zonenum * pr->zone_size) >> 10) + mag->kboff_inzone;
+
+		off = hint_offset + ((kboff + (mag->sboff >> 10)) << 10);
 		buf = blkid_probe_get_buffer(pr, off, 1024);
 
 		if (!buf && errno)
@@ -1079,7 +1104,7 @@  int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
 				buf + (mag->sboff & 0x3ff), mag->len)) {
 
 			DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld",
-				mag->sboff, mag->kboff));
+				mag->sboff, kboff));
 			if (offset)
 				*offset = off + (mag->sboff & 0x3ff);
 			if (res)