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
This commit is contained in:
Warner Losh 2022-11-30 15:08:15 -07:00
parent 34120c0c52
commit 17276525fa
8 changed files with 35 additions and 32 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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 @@ userboot_parsedev(struct disk_devdesc **dev, const char *devspec,
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)