virDevMapperGetTargets: Use a linked list as return type

Of the two callers one simply iterates over the returned paths and the
second one appends the returned paths to another linked list. Simplify
all of this by directly returning a linked list.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-09-20 14:30:59 +02:00
parent 45ea6a9fcf
commit 6c49c2ee9f
5 changed files with 31 additions and 39 deletions

View File

@ -38,6 +38,7 @@
#include "virnuma.h" #include "virnuma.h"
#include "virdevmapper.h" #include "virdevmapper.h"
#include "virutil.h" #include "virutil.h"
#include "virglibutil.h"
#define VIR_FROM_THIS VIR_FROM_QEMU #define VIR_FROM_THIS VIR_FROM_QEMU
@ -60,8 +61,8 @@ qemuSetupImagePathCgroup(virDomainObj *vm,
{ {
qemuDomainObjPrivate *priv = vm->privateData; qemuDomainObjPrivate *priv = vm->privateData;
int perms = VIR_CGROUP_DEVICE_READ; int perms = VIR_CGROUP_DEVICE_READ;
g_auto(GStrv) targetPaths = NULL; g_autoptr(virGSListString) targetPaths = NULL;
size_t i; GSList *n;
int rv; int rv;
if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES))
@ -94,10 +95,10 @@ qemuSetupImagePathCgroup(virDomainObj *vm,
return -1; return -1;
} }
for (i = 0; targetPaths && targetPaths[i]; i++) { for (n = targetPaths; n; n = n->next) {
rv = virCgroupAllowDevicePath(priv->cgroup, targetPaths[i], perms, false); rv = virCgroupAllowDevicePath(priv->cgroup, n->data, perms, false);
virDomainAuditCgroupPath(vm, priv->cgroup, "allow", targetPaths[i], virDomainAuditCgroupPath(vm, priv->cgroup, "allow", n->data,
virCgroupGetDevicePermsString(perms), virCgroupGetDevicePermsString(perms),
rv); rv);
if (rv < 0) if (rv < 0)

View File

@ -251,8 +251,7 @@ qemuDomainSetupDisk(virStorageSource *src,
if (!(tmpPath = virPCIDeviceAddressGetIOMMUGroupDev(&next->nvme->pciAddr))) if (!(tmpPath = virPCIDeviceAddressGetIOMMUGroupDev(&next->nvme->pciAddr)))
return -1; return -1;
} else { } else {
g_auto(GStrv) targetPaths = NULL; GSList *targetPaths;
GStrv tmp;
if (virStorageSourceIsEmpty(next) || if (virStorageSourceIsEmpty(next) ||
!virStorageSourceIsLocalStorage(next)) { !virStorageSourceIsLocalStorage(next)) {
@ -270,10 +269,8 @@ qemuDomainSetupDisk(virStorageSource *src,
return -1; return -1;
} }
if (targetPaths) { if (targetPaths)
for (tmp = targetPaths; *tmp; tmp++) *paths = g_slist_concat(g_slist_reverse(targetPaths), *paths);
*paths = g_slist_prepend(*paths, g_steal_pointer(tmp));
}
} }
*paths = g_slist_prepend(*paths, g_steal_pointer(&tmpPath)); *paths = g_slist_prepend(*paths, g_steal_pointer(&tmpPath));

View File

@ -36,6 +36,7 @@
# include "virstring.h" # include "virstring.h"
# include "virfile.h" # include "virfile.h"
# include "virlog.h" # include "virlog.h"
# include "virglibutil.h"
# define VIR_FROM_THIS VIR_FROM_STORAGE # define VIR_FROM_THIS VIR_FROM_STORAGE
@ -217,18 +218,16 @@ virDMSanitizepath(const char *path)
static int static int
virDevMapperGetTargetsImpl(int controlFD, virDevMapperGetTargetsImpl(int controlFD,
const char *path, const char *path,
char ***devPaths_ret, GSList **devPaths,
unsigned int ttl) unsigned int ttl)
{ {
g_autofree char *sanitizedPath = NULL; g_autofree char *sanitizedPath = NULL;
g_autofree char *buf = NULL; g_autofree char *buf = NULL;
struct dm_ioctl dm; struct dm_ioctl dm;
struct dm_target_deps *deps = NULL; struct dm_target_deps *deps = NULL;
g_auto(GStrv) devPaths = NULL;
size_t i; size_t i;
memset(&dm, 0, sizeof(dm)); memset(&dm, 0, sizeof(dm));
*devPaths_ret = NULL;
if (ttl == 0) { if (ttl == 0) {
errno = ELOOP; errno = ELOOP;
@ -258,24 +257,17 @@ virDevMapperGetTargetsImpl(int controlFD,
return -1; return -1;
} }
devPaths = g_new0(char *, deps->count + 1);
for (i = 0; i < deps->count; i++) { for (i = 0; i < deps->count; i++) {
devPaths[i] = g_strdup_printf("/dev/block/%u:%u", char *curpath = g_strdup_printf("/dev/block/%u:%u",
major(deps->dev[i]), major(deps->dev[i]),
minor(deps->dev[i])); minor(deps->dev[i]));
}
for (i = 0; i < deps->count; i++) { *devPaths = g_slist_prepend(*devPaths, curpath);
g_auto(GStrv) tmpPaths = NULL;
if (virDevMapperGetTargetsImpl(controlFD, devPaths[i], &tmpPaths, ttl - 1) < 0) if (virDevMapperGetTargetsImpl(controlFD, curpath, devPaths, ttl - 1) < 0)
return -1;
if (virStringListMerge(&devPaths, &tmpPaths) < 0)
return -1; return -1;
} }
*devPaths_ret = g_steal_pointer(&devPaths);
return 0; return 0;
} }
@ -283,11 +275,10 @@ virDevMapperGetTargetsImpl(int controlFD,
/** /**
* virDevMapperGetTargets: * virDevMapperGetTargets:
* @path: devmapper target * @path: devmapper target
* @devPaths: returned string list of devices * @devPaths: filled in by a GSList containing the paths
* *
* For given @path figure out its targets, and store them in * For given @path figure out its targets, and store them in
* @devPaths array. Note, @devPaths is a string list so it's NULL * @devPaths.
* terminated.
* *
* If @path is not a devmapper device, @devPaths is set to NULL and * If @path is not a devmapper device, @devPaths is set to NULL and
* success is returned. * success is returned.
@ -301,10 +292,11 @@ virDevMapperGetTargetsImpl(int controlFD,
*/ */
int int
virDevMapperGetTargets(const char *path, virDevMapperGetTargets(const char *path,
char ***devPaths) GSList **devPaths)
{ {
VIR_AUTOCLOSE controlFD = -1; VIR_AUTOCLOSE controlFD = -1;
const unsigned int ttl = 32; const unsigned int ttl = 32;
g_autoptr(virGSListString) paths = NULL;
/* Arbitrary limit on recursion level. A devmapper target can /* Arbitrary limit on recursion level. A devmapper target can
* consist of devices or yet another targets. If that's the * consist of devices or yet another targets. If that's the
@ -321,7 +313,11 @@ virDevMapperGetTargets(const char *path,
return -1; return -1;
} }
return virDevMapperGetTargetsImpl(controlFD, path, devPaths, ttl); if (virDevMapperGetTargetsImpl(controlFD, path, &paths, ttl) < 0)
return -1;
*devPaths = g_slist_reverse(g_steal_pointer(&paths));
return 0;
} }
@ -346,7 +342,7 @@ virIsDevMapperDevice(const char *dev_name)
int int
virDevMapperGetTargets(const char *path G_GNUC_UNUSED, virDevMapperGetTargets(const char *path G_GNUC_UNUSED,
char ***devPaths G_GNUC_UNUSED) GSlist **devPaths G_GNUC_UNUSED)
{ {
errno = ENOSYS; errno = ENOSYS;
return -1; return -1;

View File

@ -24,7 +24,7 @@
int int
virDevMapperGetTargets(const char *path, virDevMapperGetTargets(const char *path,
char ***devPaths) G_GNUC_NO_INLINE; GSList **devPaths) G_GNUC_NO_INLINE;
bool bool
virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1); virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);

View File

@ -54,16 +54,14 @@ qemuDomainGetUnplugTimeout(virDomainObj *vm)
int int
virDevMapperGetTargets(const char *path, virDevMapperGetTargets(const char *path,
char ***devPaths) GSList **devPaths)
{ {
*devPaths = NULL; *devPaths = NULL;
if (STREQ(path, "/dev/mapper/virt")) { if (STREQ(path, "/dev/mapper/virt")) {
*devPaths = g_new0(char *, 4); *devPaths = g_slist_prepend(*devPaths, g_strdup("/dev/block/8:32")); /* /dev/sdc */
(*devPaths)[0] = g_strdup("/dev/block/8:0"); /* /dev/sda */ *devPaths = g_slist_prepend(*devPaths, g_strdup("/dev/block/8:16")); /* /dev/sdb */
(*devPaths)[1] = g_strdup("/dev/block/8:16"); /* /dev/sdb */ *devPaths = g_slist_prepend(*devPaths, g_strdup("/dev/block/8:0")); /* /dev/sda */
(*devPaths)[2] = g_strdup("/dev/block/8:32"); /* /dev/sdc */
(*devPaths)[3] = NULL;
} }
return 0; return 0;