git: 9fd7cf55e6ff - stable/13 - stand: Change zfs_parsedev() API

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:13:09 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=9fd7cf55e6fff04b049bfda70da48b2c9d4b57b3

commit 9fd7cf55e6fff04b049bfda70da48b2c9d4b57b3
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-11-30 22:08:22 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:38 +0000

    stand: Change zfs_parsedev() API
    
    Change the first argument to zfs_parsedev() to be a pointer to a struct
    devdesc *. This now gets filled in with a malloc'd structure that's
    returned to the caller that the caller is repsonsible for freeing. Most
    nplaces in the tree passed in a malloc'd pointer anyway, and this moves
    knowledge of zfs_devdesc more firmly into the zfs.c code.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D37336
    
    (cherry picked from commit ba11bc368e612e7c5bbc195830a280f8af73ce18)
---
 stand/efi/libefi/devicename.c        |  6 +-----
 stand/i386/libi386/devicename.c      |  6 +-----
 stand/libofw/devicename.c            |  4 ++--
 stand/libsa/zfs/devicename_stubs.c   |  2 +-
 stand/libsa/zfs/libzfs.h             |  2 +-
 stand/libsa/zfs/zfs.c                | 18 ++++++++++--------
 stand/userboot/userboot/devicename.c |  4 ++--
 7 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/stand/efi/libefi/devicename.c b/stand/efi/libefi/devicename.c
index ad4b5d590609..73b1d783c01f 100644
--- a/stand/efi/libefi/devicename.c
+++ b/stand/efi/libefi/devicename.c
@@ -118,11 +118,7 @@ efi_parsedev(struct devdesc **dev, const char *devspec, const char **path)
 
 #ifdef EFI_ZFS_BOOT
 	case DEVT_ZFS:
-		idev = malloc(sizeof(struct zfs_devdesc));
-		if (idev == NULL)
-			return (ENOMEM);
-
-		err = zfs_parsedev((struct zfs_devdesc*)idev, np, path);
+		err = zfs_parsedev(&idev, np, path);
 		if (err != 0)
 			goto fail;
 		break;
diff --git a/stand/i386/libi386/devicename.c b/stand/i386/libi386/devicename.c
index 9ac4c9742593..1d7e4c7d3fa3 100644
--- a/stand/i386/libi386/devicename.c
+++ b/stand/i386/libi386/devicename.c
@@ -119,11 +119,7 @@ i386_parsedev(struct i386_devdesc **dev, const char *devspec, const char **path)
 	break;
 
     case DEVT_ZFS:
-	idev = malloc(sizeof (struct zfs_devdesc));
-	if (idev == NULL)
-	    return (ENOMEM);
-
-	err = zfs_parsedev((struct zfs_devdesc *)idev, np, path);
+	err = zfs_parsedev((struct devdesc **)&idev, np, path);
 	if (err != 0)
 	    goto fail;
 	break;
diff --git a/stand/libofw/devicename.c b/stand/libofw/devicename.c
index 353675db6b1f..292fba927d1a 100644
--- a/stand/libofw/devicename.c
+++ b/stand/libofw/devicename.c
@@ -113,9 +113,9 @@ found:
     idev->dd.d_dev = dv;
     if (dv->dv_type == DEVT_ZFS) {
 	p = devspec + strlen(dv->dv_name);
-	err = zfs_parsedev((struct zfs_devdesc *)idev, p, path);
+	free(idev);
+	err = zfs_parsedev((struct devdesc **)&idev, p, path);
 	if (err != 0) {
-	    free(idev);
 	    return (err);
 	}
     }
diff --git a/stand/libsa/zfs/devicename_stubs.c b/stand/libsa/zfs/devicename_stubs.c
index e58bc9c40e35..4b1ebca8e5a3 100644
--- a/stand/libsa/zfs/devicename_stubs.c
+++ b/stand/libsa/zfs/devicename_stubs.c
@@ -32,7 +32,7 @@ __FBSDID("$FreeBSD$");
 
 __attribute__((weak))
 int
-zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
+zfs_parsedev(struct devdesc **dev, const char *devspec, const char **path)
 {
 	return (EINVAL);
 }
diff --git a/stand/libsa/zfs/libzfs.h b/stand/libsa/zfs/libzfs.h
index 2f0dfd8afc86..a8ce762d2bc2 100644
--- a/stand/libsa/zfs/libzfs.h
+++ b/stand/libsa/zfs/libzfs.h
@@ -48,7 +48,7 @@ struct zfs_devdesc {
 	uint64_t	root_guid;
 };
 
-int	zfs_parsedev(struct zfs_devdesc *dev, const char *devspec,
+int	zfs_parsedev(struct devdesc **dev, const char *devspec,
 		     const char **path);
 char	*zfs_fmtdev(struct devdesc *);
 int	zfs_probe_dev(const char *devname, uint64_t *pool_guid);
diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c
index bee243352f78..a97d5fe59226 100644
--- a/stand/libsa/zfs/zfs.c
+++ b/stand/libsa/zfs/zfs.c
@@ -382,13 +382,8 @@ zfs_mount(const char *dev, const char *path, void **data)
 	int rv;
 
 	errno = 0;
-	zfsdev = malloc(sizeof(*zfsdev));
-	if (zfsdev == NULL)
-		return (errno);
-
-	rv = zfs_parsedev(zfsdev, dev + 3, NULL);
+	rv = zfs_parsedev((struct devdesc **)&zfsdev, dev + 3, NULL);
 	if (rv != 0) {
-		free(zfsdev);
 		return (rv);
 	}
 
@@ -1634,7 +1629,7 @@ struct devsw zfs_dev = {
 };
 
 int
-zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
+zfs_parsedev(struct devdesc **idev, const char *devspec, const char **path)
 {
 	static char	rootname[ZFS_MAXNAMELEN];
 	static char	poolname[ZFS_MAXNAMELEN];
@@ -1643,6 +1638,7 @@ zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
 	const char	*np;
 	const char	*sep;
 	int		rv;
+	struct zfs_devdesc *dev;
 
 	np = devspec;
 	if (*np != ':')
@@ -1667,13 +1663,19 @@ zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
 	spa = spa_find_by_name(poolname);
 	if (!spa)
 		return (ENXIO);
+	dev = malloc(sizeof(*dev));
+	if (dev == NULL)
+		return (ENOMEM);
 	dev->pool_guid = spa->spa_guid;
 	rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
-	if (rv != 0)
+	if (rv != 0) {
+		free(dev);
 		return (rv);
+	}
 	if (path != NULL)
 		*path = (*end == '\0') ? end : end + 1;
 	dev->dd.d_dev = &zfs_dev;
+	*idev = &dev->dd;
 	return (0);
 }
 
diff --git a/stand/userboot/userboot/devicename.c b/stand/userboot/userboot/devicename.c
index 2fffebf282c6..c8b46e7f590c 100644
--- a/stand/userboot/userboot/devicename.c
+++ b/stand/userboot/userboot/devicename.c
@@ -151,8 +151,8 @@ userboot_parsedev(struct devdesc **dev, const char *devspec,
 
 	case DEVT_ZFS:
 #if defined(USERBOOT_ZFS_SUPPORT)
-		/* XXX assumes sizeof disk_devdesc >= sizeof zfs_devdesc */
-		err = zfs_parsedev((struct zfs_devdesc *)idev, np, path);
+		free(idev);
+		err = zfs_parsedev(&idev, np, path);
 		if (err != 0)
 			goto fail;
 		break;