git: 8189dec087b7 - stable/13 - stand: Change disk_parsedev() API

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

URL: https://cgit.FreeBSD.org/src/commit/?id=8189dec087b741744eeaccd697a50c3a6ad9e4b4

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

    stand: Change disk_parsedev() API
    
    Change the first argument to disk_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
    places in the tree passed in a malloc'd pointer anyway, and this moves
    knowledge of disk_devdesc more firmly into the disk.[ch] code.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D37335
    
    (cherry picked from commit 17276525fa1a28028f7ca423f365d5081cbe9a97)
---
 stand/common/disk.c                  |  7 ++++++-
 stand/common/disk.h                  |  2 +-
 stand/efi/libefi/devicename.c        |  6 +-----
 stand/i386/libi386/devicename.c      |  8 ++------
 stand/i386/zfsboot/zfsboot.c         | 12 +++++++-----
 stand/uboot/devicename.c             |  3 ++-
 stand/uboot/main.c                   | 11 ++++++-----
 stand/userboot/userboot/devicename.c | 18 ++++++++++--------
 8 files changed, 35 insertions(+), 32 deletions(-)

diff --git a/stand/common/disk.c b/stand/common/disk.c
index 15daf7e358c6..653f971d46b8 100644
--- a/stand/common/disk.c
+++ b/stand/common/disk.c
@@ -413,11 +413,12 @@ disk_fmtdev(struct devdesc *vdev)
 }
 
 int
-disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
+disk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
 {
 	int unit, slice, partition;
 	const char *np;
 	char *cp;
+	struct disk_devdesc *dev;
 
 	np = devspec;
 	unit = -1;
@@ -470,9 +471,13 @@ disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
 
 	if (*cp != '\0' && *cp != ':')
 		return (EINVAL);
+	dev = malloc(sizeof(*dev));
+	if (dev == NULL)
+		return (ENOMEM);
 	dev->dd.d_unit = unit;
 	dev->d_slice = slice;
 	dev->d_partition = partition;
+	*idev = &dev->dd;
 	if (path != NULL)
 		*path = (*cp == '\0') ? cp: cp + 1;
 	return (0);
diff --git a/stand/common/disk.h b/stand/common/disk.h
index 291999ce960d..a05b532918de 100644
--- a/stand/common/disk.h
+++ b/stand/common/disk.h
@@ -111,7 +111,7 @@ extern int ptblread(void *, void *, size_t, uint64_t);
  * Print information about slices on a disk.
  */
 extern int disk_print(struct disk_devdesc *, char *, int);
-extern int disk_parsedev(struct disk_devdesc *, const char *, const char **);
+extern int disk_parsedev(struct devdesc **, const char *, const char **);
 
 char *disk_fmtdev(struct devdesc *vdev);
 
diff --git a/stand/efi/libefi/devicename.c b/stand/efi/libefi/devicename.c
index 67a2e24d9da1..ad4b5d590609 100644
--- a/stand/efi/libefi/devicename.c
+++ b/stand/efi/libefi/devicename.c
@@ -111,11 +111,7 @@ efi_parsedev(struct devdesc **dev, const char *devspec, const char **path)
 		break;
 
 	case DEVT_DISK:
-		idev = malloc(sizeof(struct disk_devdesc));
-		if (idev == NULL)
-			return (ENOMEM);
-
-		err = disk_parsedev((struct disk_devdesc *)idev, np, path);
+		err = disk_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 73445aeba172..9ac4c9742593 100644
--- a/stand/i386/libi386/devicename.c
+++ b/stand/i386/libi386/devicename.c
@@ -84,7 +84,7 @@ i386_getdev(void **vdev, const char *devspec, const char **path)
 static int
 i386_parsedev(struct i386_devdesc **dev, const char *devspec, const char **path)
 {
-    struct i386_devdesc *idev;
+    struct i386_devdesc *idev = NULL;
     struct devsw	*dv;
     int			i, unit, err;
     char		*cp;
@@ -113,11 +113,7 @@ i386_parsedev(struct i386_devdesc **dev, const char *devspec, const char **path)
 	break;
 
     case DEVT_DISK:
-	idev = malloc(sizeof(struct i386_devdesc));
-	if (idev == NULL)
-	    return (ENOMEM);
-
-	err = disk_parsedev((struct disk_devdesc *)idev, np, path);
+	err = disk_parsedev((struct devdesc **)&idev, np, path);
 	if (err != 0)
 	    goto fail;
 	break;
diff --git a/stand/i386/zfsboot/zfsboot.c b/stand/i386/zfsboot/zfsboot.c
index 930a6fb530af..ea390b6ea7a6 100644
--- a/stand/i386/zfsboot/zfsboot.c
+++ b/stand/i386/zfsboot/zfsboot.c
@@ -168,7 +168,7 @@ main(void)
 {
 	unsigned i;
 	int auto_boot, fd, nextboot = 0;
-	struct disk_devdesc devdesc;
+	struct disk_devdesc *devdesc;
 
 	bios_getmem();
 
@@ -215,11 +215,13 @@ main(void)
 		if (devsw[i]->dv_init != NULL)
 			(devsw[i]->dv_init)();
 
-	disk_parsedev(&devdesc, boot_devname + 4, NULL);
+	/* XXX assumes this will be a disk, but it looks likely give above */
+	disk_parsedev((struct devdesc **)&devdesc, boot_devname + 4, NULL);
 
-	bootdev = MAKEBOOTDEV(dev_maj[DEVT_DISK], devdesc.d_slice + 1,
-	    devdesc.dd.d_unit,
-	    devdesc.d_partition >= 0 ? devdesc.d_partition : 0xff);
+	bootdev = MAKEBOOTDEV(dev_maj[DEVT_DISK], devdesc->d_slice + 1,
+	    devdesc->dd.d_unit,
+	    devdesc->d_partition >= 0 ? devdesc->d_partition : 0xff);
+	free(devdesc);
 
 	/*
 	 * devformat() can be called only after dv_init
diff --git a/stand/uboot/devicename.c b/stand/uboot/devicename.c
index b73a7694f14a..8f867406e6fa 100644
--- a/stand/uboot/devicename.c
+++ b/stand/uboot/devicename.c
@@ -115,7 +115,8 @@ uboot_parsedev(struct uboot_devdesc **dev, const char *devspec,
 
 #ifdef LOADER_DISK_SUPPORT
 	case DEVT_DISK:
-		err = disk_parsedev((struct disk_devdesc *)idev, np, path);
+		free(idev);
+		err = disk_parsedev((struct devdesc **)&idev, np, path);
 		if (err != 0)
 			goto fail;
 		break;
diff --git a/stand/uboot/main.c b/stand/uboot/main.c
index 573995663fe4..cf41917d6ee5 100644
--- a/stand/uboot/main.c
+++ b/stand/uboot/main.c
@@ -206,7 +206,7 @@ device_typename(int type)
 static void
 get_load_device(int *type, int *unit, int *slice, int *partition)
 {
-	struct disk_devdesc dev;
+	struct disk_devdesc *dev;
 	char *devstr;
 	const char *p;
 	char *endp;
@@ -237,10 +237,11 @@ get_load_device(int *type, int *unit, int *slice, int *partition)
 	if (*type & DEV_TYP_STOR) {
 		size_t len = strlen(p);
 		if (strcspn(p, " .") == len && strcspn(p, ":") >= len - 1 &&
-		    disk_parsedev(&dev, p, NULL) == 0) {
-			*unit = dev.dd.d_unit;
-			*slice = dev.d_slice;
-			*partition = dev.d_partition;
+		    disk_parsedev((struct devdesc **)&dev, p, NULL) == 0) {
+			*unit = dev->dd.d_unit;
+			*slice = dev->d_slice;
+			*partition = dev->d_partition;
+			free(dev);
 			return;
 		}
 	}
diff --git a/stand/userboot/userboot/devicename.c b/stand/userboot/userboot/devicename.c
index 45bf9bc6ebd8..2fffebf282c6 100644
--- a/stand/userboot/userboot/devicename.c
+++ b/stand/userboot/userboot/devicename.c
@@ -38,7 +38,7 @@ __FBSDID("$FreeBSD$");
 #include "libzfs.h"
 #endif
 
-static int userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
+static int userboot_parsedev(struct devdesc **dev, const char *devspec,
     const char **path);
 
 /*
@@ -49,7 +49,7 @@ static int userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
 int
 userboot_getdev(void **vdev, const char *devspec, const char **path)
 {
-	struct disk_devdesc **dev = (struct disk_devdesc **)vdev;
+	struct devdesc **dev = (struct devdesc **)vdev;
 	int rv;
 
 	/*
@@ -87,10 +87,10 @@ userboot_getdev(void **vdev, const char *devspec, const char **path)
  *
  */
 static int
-userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
+userboot_parsedev(struct devdesc **dev, const char *devspec,
     const char **path)
 {
-	struct disk_devdesc *idev;
+	struct devdesc *idev;
 	struct devsw *dv;
 	int i, unit, err;
 	const char *cp;
@@ -119,7 +119,8 @@ userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
 		break;
 
 	case DEVT_DISK:
-		err = disk_parsedev(idev, np, path);
+		free(idev);
+		err = disk_parsedev(&idev, np, path);
 		if (err != 0)
 			goto fail;
 		break;
@@ -143,13 +144,14 @@ userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
 			goto fail;
 		}
 
-		idev->dd.d_unit = unit;
+		idev->d_unit = unit;
 		if (path != NULL)
 			*path = (*cp == 0) ? cp : cp + 1;
 		break;
 
 	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);
 		if (err != 0)
 			goto fail;
@@ -162,7 +164,7 @@ userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
 		err = EINVAL;
 		goto fail;
 	}
-	idev->dd.d_dev = dv;
+	idev->d_dev = dv;
 	if (dev == NULL) {
 		free(idev);
 	} else {
@@ -182,7 +184,7 @@ fail:
 int
 userboot_setcurrdev(struct env_var *ev, int flags, const void *value)
 {
-	struct disk_devdesc *ncurr;
+	struct devdesc *ncurr;
 	int rv;
 
 	if ((rv = userboot_parsedev(&ncurr, value, NULL)) != 0)