From nobody Thu May 2 22:06:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690822631544.6230371040265; Mon, 10 Jul 2017 05:47:02 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E6A804E4C3; Mon, 10 Jul 2017 12:47:00 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A47B361B8B; Mon, 10 Jul 2017 12:47:00 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4237A266; Mon, 10 Jul 2017 12:46:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACkuEn024828 for ; Mon, 10 Jul 2017 08:46:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6AA041797E; Mon, 10 Jul 2017 12:46:56 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id E46CB17A7B for ; Mon, 10 Jul 2017 12:46:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E6A804E4C3 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E6A804E4C3 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:40 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 01/11] util/virhash: add name parameter to virHashSearch X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 10 Jul 2017 12:47:01 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" While searching for an element using a function it may be desirable to know the element key for future operation. Signed-off-by: Pavel Hrdina Reviewed-by: Jiri Denemark --- src/conf/virdomainobjlist.c | 2 +- src/conf/virnetworkobj.c | 4 ++-- src/conf/virsecretobj.c | 2 +- src/qemu/qemu_capabilities.c | 5 +++-- src/util/virhash.c | 11 ++++++++--- src/util/virhash.h | 2 +- src/xen/xm_internal.c | 5 +++-- tests/virhashtest.c | 2 +- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index c121382bcb..7bd4cd29b9 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -119,7 +119,7 @@ virDomainObjListFindByIDInternal(virDomainObjListPtr do= ms, { virDomainObjPtr obj; virObjectLock(doms); - obj =3D virHashSearch(doms->objs, virDomainObjListSearchID, &id); + obj =3D virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL); if (ref) { virObjectRef(obj); virObjectUnlock(doms); diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index 88e42b5b3e..ccde72e727 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -208,7 +208,7 @@ virNetworkObjFindByNameLocked(virNetworkObjListPtr nets, { virNetworkObjPtr ret =3D NULL; =20 - ret =3D virHashSearch(nets->objs, virNetworkObjSearchName, name); + ret =3D virHashSearch(nets->objs, virNetworkObjSearchName, name, NULL); if (ret) virObjectRef(ret); return ret; @@ -980,7 +980,7 @@ virNetworkObjBridgeInUse(virNetworkObjListPtr nets, struct virNetworkObjBridgeInUseHelperData data =3D {bridge, skipname}; =20 virObjectLock(nets); - obj =3D virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &dat= a); + obj =3D virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &dat= a, NULL); virObjectUnlock(nets); =20 return obj !=3D NULL; diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c index 0311335ba3..8ba8784d1e 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -251,7 +251,7 @@ virSecretObjListFindByUsageLocked(virSecretObjListPtr s= ecrets, struct virSecretSearchData data =3D { .usageType =3D usageType, .usageID =3D usageID }; =20 - obj =3D virHashSearch(secrets->objs, virSecretObjSearchName, &data); + obj =3D virHashSearch(secrets->objs, virSecretObjSearchName, &data, NU= LL); if (obj) virObjectRef(obj); return obj; diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 61c9a10661..6659744a34 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -5504,14 +5504,15 @@ virQEMUCapsCacheLookupByArch(virCapsPtr caps, struct virQEMUCapsSearchData data =3D { .arch =3D arch }; =20 virMutexLock(&cache->lock); - ret =3D virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data); + ret =3D virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data, = NULL); if (!ret) { /* If the first attempt at finding capabilities has failed, try * again using the QEMU target as lookup key instead */ target =3D virQEMUCapsFindTarget(virArchFromHost(), data.arch); if (target !=3D data.arch) { data.arch =3D target; - ret =3D virHashSearch(cache->binaries, virQEMUCapsCompareArch,= &data); + ret =3D virHashSearch(cache->binaries, virQEMUCapsCompareArch, + &data, NULL); } } =20 diff --git a/src/util/virhash.c b/src/util/virhash.c index a8e0edfd3b..7fa2992f18 100644 --- a/src/util/virhash.c +++ b/src/util/virhash.c @@ -702,15 +702,18 @@ virHashRemoveAll(virHashTablePtr table) * @table: the hash table to search * @iter: an iterator to identify the desired element * @data: extra opaque information passed to the iter + * @name: the name of found user data, pass NULL to ignore * * Iterates over the hash table calling the 'iter' callback * for each element. The first element for which the iter * returns non-zero will be returned by this function. - * The elements are processed in a undefined order + * The elements are processed in a undefined order. Caller is + * responsible for freeing the @name. */ void *virHashSearch(const virHashTable *ctable, virHashSearcher iter, - const void *data) + const void *data, + void **name) { size_t i; =20 @@ -730,6 +733,8 @@ void *virHashSearch(const virHashTable *ctable, for (entry =3D table->table[i]; entry; entry =3D entry->next) { if (iter(entry->payload, entry->name, data)) { table->iterating =3D false; + if (name) + *name =3D table->keyCopy(entry->name); return entry->payload; } } @@ -824,7 +829,7 @@ bool virHashEqual(const virHashTable *table1, virHashSize(table1) !=3D virHashSize(table2)) return false; =20 - virHashSearch(table1, virHashEqualSearcher, &data); + virHashSearch(table1, virHashEqualSearcher, &data, NULL); =20 return data.equal; } diff --git a/src/util/virhash.h b/src/util/virhash.h index 61c172b9e0..00b2550e70 100644 --- a/src/util/virhash.h +++ b/src/util/virhash.h @@ -194,7 +194,7 @@ bool virHashEqual(const virHashTable *table1, int virHashForEach(virHashTablePtr table, virHashIterator iter, void *data= ); ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, cons= t void *data); void *virHashSearch(const virHashTable *table, virHashSearcher iter, - const void *data); + const void *data, void **name); =20 /* Convenience for when VIR_FREE(value) is sufficient as a data freer. */ void virHashValueFree(void *value, const void *name); diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c index fba814aa19..123f379572 100644 --- a/src/xen/xm_internal.c +++ b/src/xen/xm_internal.c @@ -880,7 +880,8 @@ xenXMDomainLookupByUUID(virConnectPtr conn, const unsig= ned char *uuid) if (!xenInotifyActive(conn) && xenXMConfigCacheRefresh(conn) < 0) goto cleanup; =20 - if (!(entry =3D virHashSearch(priv->configCache, xenXMDomainSearchForU= UID, (const void *)uuid))) + if (!(entry =3D virHashSearch(priv->configCache, xenXMDomainSearchForU= UID, + (const void *)uuid, NULL))) goto cleanup; =20 ret =3D virDomainDefNewFull(entry->def->name, uuid, -1); @@ -971,7 +972,7 @@ xenXMDomainDefineXML(virConnectPtr conn, virDomainDefPt= r def) * it has the same name */ if ((entry =3D virHashSearch(priv->configCache, xenXMDomainSearchForUU= ID, - (const void *)&(def->uuid))) !=3D NULL) { + (const void *)&(def->uuid), NULL)) !=3D NUL= L) { if ((entry->def !=3D NULL) && (entry->def->name !=3D NULL) && (STRNEQ(def->name, entry->def->name))) { char uuidstr[VIR_UUID_STRING_BUFLEN]; diff --git a/tests/virhashtest.c b/tests/virhashtest.c index dbf07ae7f7..9407f98c4b 100644 --- a/tests/virhashtest.c +++ b/tests/virhashtest.c @@ -436,7 +436,7 @@ testHashSearch(const void *data ATTRIBUTE_UNUSED) if (!(hash =3D testHashInit(0))) return -1; =20 - entry =3D virHashSearch(hash, testHashSearchIter, NULL); + entry =3D virHashSearch(hash, testHashSearchIter, NULL, NULL); =20 if (!entry || STRNEQ(uuids_subset[testSearchIndex], entry)) { VIR_TEST_VERBOSE("\nvirHashSearch didn't find entry '%s'\n", --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690823084629.8585829929965; Mon, 10 Jul 2017 05:47:03 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 314843D945; Mon, 10 Jul 2017 12:47:01 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F42287D516; Mon, 10 Jul 2017 12:47:00 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4856E267; Mon, 10 Jul 2017 12:47:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACkvdM024838 for ; Mon, 10 Jul 2017 08:46:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id 69D3917A6B; Mon, 10 Jul 2017 12:46:57 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id B974E5C6A1 for ; Mon, 10 Jul 2017 12:46:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 314843D945 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 314843D945 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:41 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 02/11] util: introduce virFileCache X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 10 Jul 2017 12:47:01 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The new virFileCache will nicely handle the caching logic for any data that we would like to cache. For each type of data we will just need to implement few handlers that will take care of creating, validating, loading and saving the cached data. The cached data must be an instance of virObject. Currently we cache QEMU capabilities which will start using virFileCache. Signed-off-by: Pavel Hrdina --- po/POTFILES.in | 1 + src/Makefile.am | 1 + src/libvirt_private.syms | 9 ++ src/util/virfilecache.c | 391 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virfilecache.h | 136 +++++++++++++++++ 5 files changed, 538 insertions(+) create mode 100644 src/util/virfilecache.c create mode 100644 src/util/virfilecache.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 275df1f297..ae81a57220 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -209,6 +209,7 @@ src/util/vireventpoll.c src/util/virfcp.c src/util/virfdstream.c src/util/virfile.c +src/util/virfilecache.c src/util/virfirewall.c src/util/virfirmware.c src/util/virhash.c diff --git a/src/Makefile.am b/src/Makefile.am index eae32dc58a..a58118db67 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -194,6 +194,7 @@ UTIL_SOURCES =3D \ util/virxdrdefs.h \ util/virxml.c util/virxml.h \ util/virmdev.c util/virmdev.h \ + util/virfilecache.c util/virfilecache.h \ $(NULL) =20 EXTRA_DIST +=3D \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 888412ac78..054a00c1d1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1707,6 +1707,15 @@ virFileWriteStr; virFindFileInPath; =20 =20 +# util/virfilecache.h +virFileCacheGetPriv; +virFileCacheInsertData; +virFileCacheLookup; +virFileCacheLookupByFunc; +virFileCacheNew; +virFileCacheSetPriv; + + # util/virfirewall.h virFirewallAddRuleFull; virFirewallApply; diff --git a/src/util/virfilecache.c b/src/util/virfilecache.c new file mode 100644 index 0000000000..0b0e361daa --- /dev/null +++ b/src/util/virfilecache.c @@ -0,0 +1,391 @@ +/* + * virfilecache.c: file caching for data + * + * Copyright (C) 2017 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + + +#include + +#include "internal.h" + +#include "viralloc.h" +#include "vircrypto.h" +#include "virerror.h" +#include "virfile.h" +#include "virfilecache.h" +#include "virhash.h" +#include "virlog.h" +#include "virobject.h" +#include "virstring.h" + +#include +#include +#include + +#define VIR_FROM_THIS VIR_FROM_NONE + +VIR_LOG_INIT("util.filecache") + + +struct _virFileCache { + virObjectLockable object; + + virHashTablePtr table; + + char *dir; + void *priv; + + virFileCacheHandlers handlers; +}; + + +static virClassPtr virFileCacheClass; + + +static void +virFileCacheDispose(void *obj) +{ + virFileCachePtr cache =3D obj; + + VIR_FREE(cache->dir); + + virHashFree(cache->table); + + if (cache->priv && cache->handlers.privFree) + cache->handlers.privFree(cache->priv); +} + + +static int +virFileCacheOnceInit(void) +{ + if (!(virFileCacheClass =3D virClassNew(virClassForObjectLockable(), + "virFileCache", + sizeof(virFileCache), + virFileCacheDispose))) + return -1; + + return 0; +} + + +VIR_ONCE_GLOBAL_INIT(virFileCache) + + +static char * +virFileCacheGetFileName(virFileCachePtr cache, + const char *name) +{ + char *file =3D NULL; + char *namehash =3D NULL; + + if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, name, &namehash) < 0) + goto cleanup; + + if (virAsprintf(&file, "%s/%s.cache", cache->dir, namehash) < 0) + goto cleanup; + + if (virFileMakePath(cache->dir) < 0) { + virReportSystemError(errno, + _("Unable to create directory '%s'"), + cache->dir); + VIR_FREE(file); + file =3D NULL; + } + + cleanup: + VIR_FREE(namehash); + return file; +} + + +static int +virFileCacheLoad(virFileCachePtr cache, + const char *name, + void **data) +{ + char *file =3D NULL; + int ret =3D -1; + + if (!(file =3D virFileCacheGetFileName(cache, name))) + return ret; + + if (!virFileExists(file)) { + if (errno =3D=3D ENOENT) { + VIR_DEBUG("No cached data '%s' for '%s'", file, name); + ret =3D 0; + goto cleanup; + } + virReportSystemError(errno, + _("Unable to access cache '%s' for '%s'"), + file, name); + goto cleanup; + } + + if (!(*data =3D cache->handlers.loadFile(file, name, cache->priv))) { + virErrorPtr err =3D virGetLastError(); + VIR_WARN("Failed to load cached data from '%s' for '%s': %s", + file, name, err ? NULLSTR(err->message) : "unknown error"= ); + virResetLastError(); + ret =3D 0; + goto cleanup; + } + + if (!cache->handlers.isValid(*data, cache->priv)) { + VIR_DEBUG("Outdated cached capabilities '%s' for '%s'", file, name= ); + ignore_value(unlink(file)); + ret =3D 0; + goto cleanup; + } + + VIR_DEBUG("Loaded cached data '%s' for '%s'", file, name); + + ret =3D 1; + + cleanup: + VIR_FREE(file); + return ret; +} + + +static int +virFileCacheSave(virFileCachePtr cache, + const char *name, + void *data) +{ + char *file =3D NULL; + int ret =3D -1; + + if (!(file =3D virFileCacheGetFileName(cache, name))) + return ret; + + if (cache->handlers.saveFile(data, file, cache->priv) < 0) + goto cleanup; + + ret =3D 0; + + cleanup: + VIR_FREE(file); + return ret; +} + + +static void * +virFileCacheNewData(virFileCachePtr cache, + const char *name) +{ + void *data =3D NULL; + int rv; + + if ((rv =3D virFileCacheLoad(cache, name, &data)) < 0) + goto cleanup; + + if (rv =3D=3D 0) { + virObjectUnref(data); + if (!(data =3D cache->handlers.newData(name, cache->priv))) + goto cleanup; + + if (virFileCacheSave(cache, name, data) < 0) { + virObjectUnref(data); + data =3D NULL; + } + } + + cleanup: + return data; +} + + +/** + * virFileCacheNew: + * @dir: the cache directory where all the cache files will be stored + * @handlers: filled structure with all required handlers + * + * Creates a new cache object which handles caching any data to files + * stored on a filesystem. + * + * Returns new cache object or NULL on error. + */ +virFileCachePtr +virFileCacheNew(const char *dir, + virFileCacheHandlers handlers) +{ + virFileCachePtr cache; + + if (virFileCacheInitialize() < 0) + return NULL; + + if (!(cache =3D virObjectNew(virFileCacheClass))) + return NULL; + + if (!(cache->table =3D virHashCreate(10, virObjectFreeHashData))) + goto cleanup; + + if (VIR_STRDUP(cache->dir, dir) < 0) + goto cleanup; + + cache->handlers =3D handlers; + + return cache; + + cleanup: + virObjectUnref(cache); + return NULL; +} + + +static void +virFileCacheValidate(virFileCachePtr cache, + const char *name, + void **data) +{ + if (*data && !cache->handlers.isValid(*data, cache->priv)) { + VIR_DEBUG("Cached data '%p' no longer valid for '%s'", + *data, NULLSTR(name)); + if (name) + virHashRemoveEntry(cache->table, name); + *data =3D NULL; + } + + if (!*data && name) { + VIR_DEBUG("Creating data for '%s'", name); + *data =3D virFileCacheNewData(cache, name); + if (*data) { + VIR_DEBUG("Caching data '%p' for '%s'", *data, name); + if (virHashAddEntry(cache->table, name, *data) < 0) { + virObjectUnref(*data); + *data =3D NULL; + } + } + } +} + + +/** + * virFileCacheLookup: + * @cache: existing cache object + * @name: name of the data stored in a cache + * + * Lookup a data specified by name. This tries to find a file with + * cached data, if it doesn't exist or is no longer valid new data + * is created. + * + * Returns data object or NULL on error. The caller is responsible for + * unrefing the data. + */ +void * +virFileCacheLookup(virFileCachePtr cache, + const char *name) +{ + void *data =3D NULL; + + virObjectLock(cache); + + data =3D virHashLookup(cache->table, name); + virFileCacheValidate(cache, name, &data); + + virObjectRef(data); + virObjectUnlock(cache); + + return data; +} + + +/** + * virFileCacheLookupByFunc: + * @cache: existing cache object + * @iter: an iterator to identify the desired data + * @iterData: extra opaque information passed to the @iter + * + * Similar to virFileCacheLookup() except it search by @iter. + * + * Returns data object or NULL on error. The caller is responsible for + * unrefing the data. + */ +void * +virFileCacheLookupByFunc(virFileCachePtr cache, + virHashSearcher iter, + const void *iterData) +{ + void *data =3D NULL; + char *name =3D NULL; + + virObjectLock(cache); + + data =3D virHashSearch(cache->table, iter, iterData, (void **)&name); + virFileCacheValidate(cache, name, &data); + + virObjectRef(data); + virObjectUnlock(cache); + + VIR_FREE(name); + + return data; +} + + +/** + * virFileCacheGetPriv: + * @cache: existing cache object + * + * Returns private data used by @handlers. + */ +void * +virFileCacheGetPriv(virFileCachePtr cache) +{ + return cache->priv; +} + + +/** + * virFileCacheSetPriv: + * @cache: existing cache object + * @priv: private data to set + * + * Sets private data used by @handlers. + */ +void +virFileCacheSetPriv(virFileCachePtr cache, + void *priv) +{ + cache->priv =3D priv; +} + + +/** + * virFileCacheInsertData: + * @cache: existing cache object + * @name: name of the new data + * @data: the actual data object to store in cache + * + * Adds a new data into a cache but doesn't store the data into + * a file. This function should be used only by testing code. + * + * Returns 0 on success, -1 on error. + */ +int +virFileCacheInsertData(virFileCachePtr cache, + const char *name, + void *data) +{ + if (virHashUpdateEntry(cache->table, name, data) < 0) + return -1; + + return 0; +} diff --git a/src/util/virfilecache.h b/src/util/virfilecache.h new file mode 100644 index 0000000000..3447616361 --- /dev/null +++ b/src/util/virfilecache.h @@ -0,0 +1,136 @@ +/* + * virfilecache.h: file caching for data + * + * Copyright (C) 2017 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + +#ifndef __VIR_FILE_CACHE_H__ +# define __VIR_FILE_CACHE_H__ + +# include "internal.h" + +# include "virobject.h" +# include "virhash.h" + +typedef struct _virFileCache virFileCache; +typedef virFileCache *virFileCachePtr; + +/** + * virFileCacheIsValidPtr: + * @data: data object to validate + * @priv: private data created together with cache + * + * Validates the cached data whether it needs to be refreshed + * or no. + * + * Returns *true* if it's valid or *false* if not valid. + */ +typedef bool +(*virFileCacheIsValidPtr)(void *data, + void *priv); + +/** + * virFileCacheNewDataPtr: + * @name: name of the new data + * @priv: private data created together with cache + * + * Creates a new data based on the @name. The returned data must be + * an instance of virObject. + * + * Returns data object or NULL on error. + */ +typedef void * +(*virFileCacheNewDataPtr)(const char *name, + void *priv); + +/** + * virFileCacheLoadFilePtr: + * @filename: name of a file with cached data + * @name: name of the cached data + * @priv: private data created together with cache + * + * Loads the cached data from a file @filename. + * + * Returns cached data object or NULL on error. + */ +typedef void * +(*virFileCacheLoadFilePtr)(const char *filename, + const char *name, + void *priv); + +/** + * virFileCacheSaveFilePtr: + * @data: data object to save into a file + * @filename: name of the file where to store the cached data + * @priv: private data created together with cache + * + * Stores the cached to a file @filename. + * + * Returns 0 on success, -1 on error. + */ +typedef int +(*virFileCacheSaveFilePtr)(void *data, + const char *filename, + void *priv); + +/** + * virFileCachePrivFreePtr: + * @priv: private data created together with cache + * + * This is used to free the private data when the cache object + * is removed. + */ +typedef void +(*virFileCachePrivFreePtr)(void *priv); + +typedef struct _virFileCacheHandlers virFileCacheHandlers; +typedef virFileCacheHandlers *virFileCacheHandlersPtr; +struct _virFileCacheHandlers { + virFileCacheIsValidPtr isValid; + virFileCacheNewDataPtr newData; + virFileCacheLoadFilePtr loadFile; + virFileCacheSaveFilePtr saveFile; + virFileCachePrivFreePtr privFree; +}; + +virFileCachePtr +virFileCacheNew(const char *dir, + virFileCacheHandlers handlers); + +void * +virFileCacheLookup(virFileCachePtr cache, + const char *name); + +void * +virFileCacheLookupByFunc(virFileCachePtr cache, + virHashSearcher iter, + const void *iterData); + +void * +virFileCacheGetPriv(virFileCachePtr cache); + +void +virFileCacheSetPriv(virFileCachePtr cache, + void *priv); + +int +virFileCacheInsertData(virFileCachePtr cache, + const char *name, + void *data); + +#endif /* __VIR_FILE_CACHE_H__ */ --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690836997293.98013807581265; Mon, 10 Jul 2017 05:47:16 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2D068BC6B4; Mon, 10 Jul 2017 12:47:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 066BC7D559; Mon, 10 Jul 2017 12:47:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A2EBB1803B26; Mon, 10 Jul 2017 12:47:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACkw8o024846 for ; Mon, 10 Jul 2017 08:46:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 57FDC17A77; Mon, 10 Jul 2017 12:46:58 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id D387817A6B for ; Mon, 10 Jul 2017 12:46:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2D068BC6B4 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2D068BC6B4 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:42 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 03/11] tests: add virfilecachetest X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 10 Jul 2017 12:47:15 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Implements 3 test cases that covers how the cache is used. We have to mock unlink() function because the caching code unlinks files that are no longer valid and we don't want to do it in our tests. Signed-off-by: Pavel Hrdina --- tests/Makefile.am | 12 ++ ...a15b1658aa16923e46497dd8deeb6be287ddb0ca0.cache | 1 + ...ae4bc3a28e75bc3e262757001e8b953580f5e75ef.cache | 1 + tests/virfilecachetest.c | 238 +++++++++++++++++= ++++ 4 files changed, 252 insertions(+) create mode 100644 tests/virfilecachedata/5f3154560c130108b282a2aa15b1658a= a16923e46497dd8deeb6be287ddb0ca0.cache create mode 120000 tests/virfilecachedata/9ca150bf3119b75dcac8e8bae4bc3a28= e75bc3e262757001e8b953580f5e75ef.cache create mode 100644 tests/virfilecachetest.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 19986dc992..7ec048066e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -182,6 +182,7 @@ test_programs =3D virshtest sockettest \ virpcitest \ virendiantest \ virfiletest \ + virfilecachetest \ virfirewalltest \ viriscsitest \ virkeycodetest \ @@ -211,6 +212,7 @@ test_libraries =3D libshunload.la \ virrandommock.la \ virhostcpumock.la \ domaincapsmock.la \ + virfilecachemock.la \ $(NULL) =20 if WITH_REMOTE @@ -1138,6 +1140,12 @@ virhostcpumock_la_CFLAGS =3D $(AM_CFLAGS) virhostcpumock_la_LDFLAGS =3D $(MOCKLIBS_LDFLAGS) virhostcpumock_la_LIBADD =3D $(MOCKLIBS_LIBS) =20 +virfilecachemock_la_SOURCES =3D \ + virfilecachemock.c +virfilecachemock_la_CFLAGS =3D $(AM_CFLAGS) +virfilecachemock_la_LDFLAGS =3D $(MOCKLIBS_LDFLAGS) +virfilecachemock_la_LIBADD =3D $(MOCKLIBS_LIBS) + if WITH_LINUX vircaps2xmltest_SOURCES =3D \ vircaps2xmltest.c testutils.h testutils.c virfilewrapper.h virfilewrapper= .c @@ -1370,6 +1378,10 @@ virfiletest_SOURCES =3D \ virfiletest.c testutils.h testutils.c virfiletest_LDADD =3D $(LDADDS) =20 +virfilecachetest_SOURCES =3D \ + virfilecachetest.c testutils.h testutils.c +virfilecachetest_LDADD =3D $(LDADDS) + virfirewalltest_SOURCES =3D \ virfirewalltest.c testutils.h testutils.c virfirewalltest_LDADD =3D $(LDADDS) $(DBUS_LIBS) diff --git a/tests/virfilecachedata/5f3154560c130108b282a2aa15b1658aa16923e= 46497dd8deeb6be287ddb0ca0.cache b/tests/virfilecachedata/5f3154560c130108b2= 82a2aa15b1658aa16923e46497dd8deeb6be287ddb0ca0.cache new file mode 100644 index 0000000000..72943a16fb --- /dev/null +++ b/tests/virfilecachedata/5f3154560c130108b282a2aa15b1658aa16923e46497dd= 8deeb6be287ddb0ca0.cache @@ -0,0 +1 @@ +aaa diff --git a/tests/virfilecachedata/9ca150bf3119b75dcac8e8bae4bc3a28e75bc3e= 262757001e8b953580f5e75ef.cache b/tests/virfilecachedata/9ca150bf3119b75dca= c8e8bae4bc3a28e75bc3e262757001e8b953580f5e75ef.cache new file mode 120000 index 0000000000..94df26de63 --- /dev/null +++ b/tests/virfilecachedata/9ca150bf3119b75dcac8e8bae4bc3a28e75bc3e2627570= 01e8b953580f5e75ef.cache @@ -0,0 +1 @@ +5f3154560c130108b282a2aa15b1658aa16923e46497dd8deeb6be287ddb0ca0.cache \ No newline at end of file diff --git a/tests/virfilecachetest.c b/tests/virfilecachetest.c new file mode 100644 index 0000000000..98c46f86fc --- /dev/null +++ b/tests/virfilecachetest.c @@ -0,0 +1,238 @@ +/* + * Copyright (C) 2017 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + */ + +#include + +#include "testutils.h" +#include "virfile.h" +#include "virfilecache.h" + + +#define VIR_FROM_THIS VIR_FROM_NONE + + +struct _testFileCacheObj { + virObject object; + char *data; +}; +typedef struct _testFileCacheObj testFileCacheObj; +typedef testFileCacheObj *testFileCacheObjPtr; + + +static virClassPtr testFileCacheObjClass; + + +static void +testFileCacheObjDispose(void *opaque) +{ + testFileCacheObjPtr obj =3D opaque; + VIR_FREE(obj->data); +} + + +static int +testFileCacheObjOnceInit(void) +{ + if (!(testFileCacheObjClass =3D virClassNew(virClassForObject(), + "testFileCacheObj", + sizeof(testFileCacheObj), + testFileCacheObjDispose))) + return -1; + + return 0; +} + + +VIR_ONCE_GLOBAL_INIT(testFileCacheObj) + + +static testFileCacheObjPtr +testFileCacheObjNew(const char *data) +{ + testFileCacheObjPtr obj; + + if (testFileCacheObjInitialize() < 0) + return NULL; + + if (!(obj =3D virObjectNew(testFileCacheObjClass))) + return NULL; + + if (VIR_STRDUP(obj->data, data) < 0) + goto error; + + return obj; + + error: + virObjectUnref(obj); + return NULL; +} + + +struct _testFileCachePriv { + bool dataSaved; + const char *newData; + const char *expectData; +}; +typedef struct _testFileCachePriv testFileCachePriv; +typedef testFileCachePriv *testFileCachePrivPtr; + + +static bool +testFileCacheIsValid(void *data, + void *priv) +{ + testFileCachePrivPtr testPriv =3D priv; + testFileCacheObjPtr obj =3D data; + + return STREQ(testPriv->expectData, obj->data); +} + + +static void * +testFileCacheNewData(const char *name ATTRIBUTE_UNUSED, + void *priv) +{ + testFileCachePrivPtr testPriv =3D priv; + + return testFileCacheObjNew(testPriv->newData); +} + + +static void * +testFileCacheLoadFile(const char *filename, + const char *name ATTRIBUTE_UNUSED, + void *priv ATTRIBUTE_UNUSED) +{ + testFileCacheObjPtr obj; + char *data; + + if (virFileReadAll(filename, 20, &data) < 0) + return NULL; + + obj =3D testFileCacheObjNew(data); + + VIR_FREE(data); + return obj; +} + + +static int +testFileCacheSaveFile(void *data ATTRIBUTE_UNUSED, + const char *filename ATTRIBUTE_UNUSED, + void *priv) +{ + testFileCachePrivPtr testPriv =3D priv; + + testPriv->dataSaved =3D true; + + return 0; +} + + +virFileCacheHandlers testFileCacheHandlers =3D { + .isValid =3D testFileCacheIsValid, + .newData =3D testFileCacheNewData, + .loadFile =3D testFileCacheLoadFile, + .saveFile =3D testFileCacheSaveFile +}; + + +struct _testFileCacheData { + virFileCachePtr cache; + const char *name; + const char *newData; + const char *expectData; + bool expectSave; +}; +typedef struct _testFileCacheData testFileCacheData; +typedef testFileCacheData *testFileCacheDataPtr; + + +static int +testFileCache(const void *opaque) +{ + int ret =3D -1; + const testFileCacheData *data =3D opaque; + testFileCacheObjPtr obj =3D NULL; + testFileCachePrivPtr testPriv =3D virFileCacheGetPriv(data->cache); + + testPriv->dataSaved =3D false; + testPriv->newData =3D data->newData; + testPriv->expectData =3D data->expectData; + + if (!(obj =3D virFileCacheLookup(data->cache, data->name))) { + fprintf(stderr, "Getting cached data failed.\n"); + goto cleanup; + } + + if (!obj->data || STRNEQ(data->expectData, obj->data)) { + fprintf(stderr, "Expect data '%s', loaded data '%s'.\n", + data->expectData, NULLSTR(obj->data)); + goto cleanup; + } + + if (data->expectSave !=3D testPriv->dataSaved) { + fprintf(stderr, "Expect data to be saved '%s', data saved '%s'.\n", + data->expectSave ? "yes" : "no", + testPriv->dataSaved ? "yes" : "no"); + goto cleanup; + } + + ret =3D 0; + + cleanup: + virObjectUnref(obj); + return ret; +} + + +static int +mymain(void) +{ + int ret =3D 0; + testFileCachePriv testPriv =3D {0}; + virFileCachePtr cache =3D NULL; + + if (!(cache =3D virFileCacheNew(abs_srcdir "/virfilecachedata", + testFileCacheHandlers))) + return EXIT_FAILURE; + + virFileCacheSetPriv(cache, &testPriv); + +#define TEST_RUN(name, newData, expectData, expectSave) = \ + do { = \ + testFileCacheData data =3D { = \ + cache, name, newData, expectData, expectSave = \ + }; = \ + if (virTestRun(name, testFileCache, &data) < 0) = \ + ret =3D -1; = \ + } while (0) + + /* The cache file name is created using: + * '$ echo -n $TEST_NAME | sha256sum' */ + TEST_RUN("cacheValid", NULL, "aaa\n", false); + TEST_RUN("cacheInvalid", "bbb\n", "bbb\n", true); + TEST_RUN("cacheMissing", "ccc\n", "ccc\n", true); + + virObjectUnref(cache); + + return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} + +VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilecachemock.so") --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690841391504.0509377268826; Mon, 10 Jul 2017 05:47:21 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8839C624C0; Mon, 10 Jul 2017 12:47:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 534967D51B; Mon, 10 Jul 2017 12:47:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 064E927F; Mon, 10 Jul 2017 12:47:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACkxW0024853 for ; Mon, 10 Jul 2017 08:46:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2B29017A6B; Mon, 10 Jul 2017 12:46:59 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id A70405C6A1 for ; Mon, 10 Jul 2017 12:46:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8839C624C0 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8839C624C0 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:43 +0200 Message-Id: <641033b75b510e4ad43703b4428a3b188dfeeb34.1499690733.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 04/11] qemu: move libvirt ctime and version into _virQEMUCaps struct X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 10 Jul 2017 12:47:20 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Cleanups the code a little bit and reduces amount of arguments passed throughout the functions. Signed-off-by: Pavel Hrdina Reviewed-by: Jiri Denemark --- src/qemu/qemu_capabilities.c | 44 +++++++++++++++++++++-------------------= ---- src/qemu/qemu_capspriv.h | 8 ++------ tests/qemucapabilitiestest.c | 4 ++-- tests/testutilsqemu.c | 5 +---- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 6659744a34..c9c28da556 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -474,11 +474,13 @@ struct _virQEMUCaps { =20 char *binary; time_t ctime; + time_t libvirtCtime; =20 virBitmapPtr flags; =20 unsigned int version; unsigned int kvmVersion; + unsigned int libvirtVersion; char *package; =20 virArch arch; @@ -3783,9 +3785,7 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps, int virQEMUCapsLoadCache(virCapsPtr caps, virQEMUCapsPtr qemuCaps, - const char *filename, - time_t *selfctime, - unsigned long *selfvers) + const char *filename) { xmlDocPtr doc =3D NULL; int ret =3D -1; @@ -3827,11 +3827,11 @@ virQEMUCapsLoadCache(virCapsPtr caps, _("missing selfctime in QEMU capabilities XML")); goto cleanup; } - *selfctime =3D (time_t)l; + qemuCaps->libvirtCtime =3D (time_t)l; =20 - *selfvers =3D 0; + qemuCaps->libvirtVersion =3D 0; if (virXPathULong("string(./selfvers)", ctxt, &lu) =3D=3D 0) - *selfvers =3D lu; + qemuCaps->libvirtVersion =3D lu; =20 qemuCaps->usedQMP =3D virXPathBoolean("count(./usedQMP) > 0", ctxt) > 0; @@ -4104,9 +4104,7 @@ virQEMUCapsFormatCPUModels(virQEMUCapsPtr qemuCaps, =20 =20 char * -virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps, - time_t selfCTime, - unsigned long selfVersion) +virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps) { virBuffer buf =3D VIR_BUFFER_INITIALIZER; char *ret =3D NULL; @@ -4118,9 +4116,9 @@ virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps, virBufferAsprintf(&buf, "%llu\n", (long long) qemuCaps->ctime); virBufferAsprintf(&buf, "%llu\n", - (long long) selfCTime); + (long long) qemuCaps->libvirtCtime); virBufferAsprintf(&buf, "%lu\n", - (unsigned long) selfVersion); + (unsigned long) qemuCaps->libvirtVersion); =20 if (qemuCaps->usedQMP) virBufferAddLit(&buf, "\n"); @@ -4195,9 +4193,7 @@ virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const c= har *filename) char *xml =3D NULL; int ret =3D -1; =20 - xml =3D virQEMUCapsFormatCache(qemuCaps, - virGetSelfLastChanged(), - LIBVIR_VERSION_NUMBER); + xml =3D virQEMUCapsFormatCache(qemuCaps); =20 if (virFileWriteStr(filename, xml, 0600) < 0) { virReportSystemError(errno, @@ -4209,7 +4205,7 @@ virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const c= har *filename) VIR_DEBUG("Saved caps '%s' for '%s' with (%lld, %lld)", filename, qemuCaps->binary, (long long)qemuCaps->ctime, - (long long)virGetSelfLastChanged()); + (long long)qemuCaps->libvirtCtime); =20 ret =3D 0; cleanup: @@ -4299,8 +4295,6 @@ virQEMUCapsInitCached(virCapsPtr caps, char *binaryhash =3D NULL; struct stat sb; time_t qemuctime =3D qemuCaps->ctime; - time_t selfctime; - unsigned long selfvers; =20 if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0) goto cleanup; @@ -4333,8 +4327,7 @@ virQEMUCapsInitCached(virCapsPtr caps, goto cleanup; } =20 - if (virQEMUCapsLoadCache(caps, qemuCaps, capsfile, - &selfctime, &selfvers) < 0) { + if (virQEMUCapsLoadCache(caps, qemuCaps, capsfile) < 0) { VIR_WARN("Failed to load cached caps from '%s' for '%s': %s", capsfile, qemuCaps->binary, virGetLastErrorMessage()); virResetLastError(); @@ -4345,13 +4338,15 @@ virQEMUCapsInitCached(virCapsPtr caps, goto discard; =20 /* Discard cache if QEMU binary or libvirtd changed */ - if (selfctime !=3D virGetSelfLastChanged() || - selfvers !=3D LIBVIR_VERSION_NUMBER) { + if (qemuCaps->libvirtCtime !=3D virGetSelfLastChanged() || + qemuCaps->libvirtVersion !=3D LIBVIR_VERSION_NUMBER) { VIR_DEBUG("Outdated capabilities for '%s': libvirt changed " "(%lld vs %lld, %lu vs %lu)", qemuCaps->binary, - (long long)selfctime, (long long)virGetSelfLastChanged(), - selfvers, (unsigned long)LIBVIR_VERSION_NUMBER); + (long long)qemuCaps->libvirtCtime, + (long long)virGetSelfLastChanged(), + (unsigned long)qemuCaps->libvirtVersion, + (unsigned long)LIBVIR_VERSION_NUMBER); goto discard; } =20 @@ -5248,6 +5243,9 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps, goto error; } =20 + qemuCaps->libvirtCtime =3D virGetSelfLastChanged(); + qemuCaps->libvirtVersion =3D LIBVIR_VERSION_NUMBER; + if (cacheDir && virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0) goto error; diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h index 94fa75b960..1162e0b284 100644 --- a/src/qemu/qemu_capspriv.h +++ b/src/qemu/qemu_capspriv.h @@ -50,12 +50,8 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps, =20 int virQEMUCapsLoadCache(virCapsPtr caps, virQEMUCapsPtr qemuCaps, - const char *filename, - time_t *selfctime, - unsigned long *selfvers); -char *virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps, - time_t selfCTime, - unsigned long selfVersion); + const char *filename); +char *virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps); =20 int virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps, diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index bbb683eb83..45d2a885bd 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -66,7 +66,7 @@ testQemuCaps(const void *opaque) qemuMonitorTestGetMonitor(mon)) < 0) goto cleanup; =20 - if (!(actual =3D virQEMUCapsFormatCache(capsActual, 0, 0))) + if (!(actual =3D virQEMUCapsFormatCache(capsActual))) goto cleanup; =20 if (virTestCompareToFile(actual, capsFile) < 0) @@ -108,7 +108,7 @@ testQemuCapsCopy(const void *opaque) if (!(copy =3D virQEMUCapsNewCopy(orig))) goto cleanup; =20 - if (!(actual =3D virQEMUCapsFormatCache(copy, 0, 0))) + if (!(actual =3D virQEMUCapsFormatCache(copy))) goto cleanup; =20 if (virTestCompareToFile(actual, capsFile) < 0) diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index ee4853841c..88e11ba14a 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -568,12 +568,9 @@ qemuTestParseCapabilities(virCapsPtr caps, const char *capsFile) { virQEMUCapsPtr qemuCaps =3D NULL; - time_t selfctime; - unsigned long version; =20 if (!(qemuCaps =3D virQEMUCapsNew()) || - virQEMUCapsLoadCache(caps, qemuCaps, capsFile, - &selfctime, &version) < 0) + virQEMUCapsLoadCache(caps, qemuCaps, capsFile) < 0) goto error; =20 return qemuCaps; --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690836722328.6569903221464; Mon, 10 Jul 2017 05:47:16 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5D17F3B738; Mon, 10 Jul 2017 12:47:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 321AD17A95; Mon, 10 Jul 2017 12:47:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D521D27E; Mon, 10 Jul 2017 12:47:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl0TN024859 for ; Mon, 10 Jul 2017 08:47:00 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6737F17A6B; Mon, 10 Jul 2017 12:47:00 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id E39F35C6A1 for ; Mon, 10 Jul 2017 12:46:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5D17F3B738 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 5D17F3B738 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:44 +0200 Message-Id: <386aaa5e2bf3f12f75eacfd15f5c97a4ab0da38d.1499690733.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 05/11] qemu: move virQEMUCapsIsValid before its usage and make it static X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 10 Jul 2017 12:47:15 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Jiri Denemark --- src/qemu/qemu_capabilities.c | 112 +++++++++++++++++++++------------------= ---- src/qemu/qemu_capabilities.h | 5 -- 2 files changed, 56 insertions(+), 61 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index c9c28da556..8a2a73dc5b 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -4282,6 +4282,62 @@ virQEMUCapsReset(virQEMUCapsPtr qemuCaps) } =20 =20 +static bool +virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, + time_t qemuctime, + uid_t runUid, + gid_t runGid) +{ + bool kvmUsable; + + if (!qemuCaps->binary) + return true; + + if (!qemuctime) { + struct stat sb; + + if (stat(qemuCaps->binary, &sb) < 0) { + char ebuf[1024]; + VIR_DEBUG("Failed to stat QEMU binary '%s': %s", + qemuCaps->binary, + virStrerror(errno, ebuf, sizeof(ebuf))); + return false; + } + qemuctime =3D sb.st_ctime; + } + + if (qemuctime !=3D qemuCaps->ctime) { + VIR_DEBUG("Outdated capabilities for '%s': QEMU binary changed " + "(%lld vs %lld)", + qemuCaps->binary, + (long long) qemuctime, (long long) qemuCaps->ctime); + return false; + } + + kvmUsable =3D virFileAccessibleAs("/dev/kvm", R_OK | W_OK, + runUid, runGid) =3D=3D 0; + + if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) && + virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KVM) && + kvmUsable) { + VIR_DEBUG("KVM was not enabled when probing '%s', " + "but it should be usable now", + qemuCaps->binary); + return false; + } + + if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) && + !kvmUsable) { + VIR_DEBUG("KVM was enabled when probing '%s', " + "but it is not available now", + qemuCaps->binary); + return false; + } + + return true; +} + + static int virQEMUCapsInitCached(virCapsPtr caps, virQEMUCapsPtr qemuCaps, @@ -5277,62 +5333,6 @@ virQEMUCapsNewForBinary(virCapsPtr caps, } =20 =20 -bool -virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, - time_t qemuctime, - uid_t runUid, - gid_t runGid) -{ - bool kvmUsable; - - if (!qemuCaps->binary) - return true; - - if (!qemuctime) { - struct stat sb; - - if (stat(qemuCaps->binary, &sb) < 0) { - char ebuf[1024]; - VIR_DEBUG("Failed to stat QEMU binary '%s': %s", - qemuCaps->binary, - virStrerror(errno, ebuf, sizeof(ebuf))); - return false; - } - qemuctime =3D sb.st_ctime; - } - - if (qemuctime !=3D qemuCaps->ctime) { - VIR_DEBUG("Outdated capabilities for '%s': QEMU binary changed " - "(%lld vs %lld)", - qemuCaps->binary, - (long long) qemuctime, (long long) qemuCaps->ctime); - return false; - } - - kvmUsable =3D virFileAccessibleAs("/dev/kvm", R_OK | W_OK, - runUid, runGid) =3D=3D 0; - - if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) && - virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KVM) && - kvmUsable) { - VIR_DEBUG("KVM was not enabled when probing '%s', " - "but it should be usable now", - qemuCaps->binary); - return false; - } - - if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) && - !kvmUsable) { - VIR_DEBUG("KVM was enabled when probing '%s', " - "but it is not available now", - qemuCaps->binary); - return false; - } - - return true; -} - - struct virQEMUCapsMachineTypeFilter { const char *machineType; virQEMUCapsFlags *flags; diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 6e95876e8d..7f5382df62 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -494,11 +494,6 @@ int virQEMUCapsGetMachineTypesCaps(virQEMUCapsPtr qemu= Caps, size_t *nmachines, virCapsGuestMachinePtr **machines); =20 -bool virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, - time_t ctime, - uid_t runUid, - gid_t runGid); - void virQEMUCapsFilterByMachineType(virQEMUCapsPtr qemuCaps, const char *machineType); =20 --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:29 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690840819463.9499214496524; Mon, 10 Jul 2017 05:47:20 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 38E15C04B92B; Mon, 10 Jul 2017 12:47:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1332B757A9; Mon, 10 Jul 2017 12:47:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id AC87D281; Mon, 10 Jul 2017 12:47:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl1aV024865 for ; Mon, 10 Jul 2017 08:47:01 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4D7D417A6B; Mon, 10 Jul 2017 12:47:01 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id C8BDC5C6A1 for ; Mon, 10 Jul 2017 12:47:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 38E15C04B92B Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 38E15C04B92B From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:45 +0200 Message-Id: <2e6721735499ac94daab6c0d55c54592d8353811.1499690733.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 06/11] qemu: move libvirt ctime and version check into virQEMUCapsIsValid X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 10 Jul 2017 12:47:19 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Jiri Denemark --- src/qemu/qemu_capabilities.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 8a2a73dc5b..4d8890aaaf 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -4293,6 +4293,18 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, if (!qemuCaps->binary) return true; =20 + if (qemuCaps->libvirtCtime !=3D virGetSelfLastChanged() || + qemuCaps->libvirtVersion !=3D LIBVIR_VERSION_NUMBER) { + VIR_DEBUG("Outdated capabilities for '%s': libvirt changed " + "(%lld vs %lld, %lu vs %lu)", + qemuCaps->binary, + (long long)qemuCaps->libvirtCtime, + (long long)virGetSelfLastChanged(), + (unsigned long)qemuCaps->libvirtVersion, + (unsigned long)LIBVIR_VERSION_NUMBER); + return false; + } + if (!qemuctime) { struct stat sb; =20 @@ -4393,19 +4405,6 @@ virQEMUCapsInitCached(virCapsPtr caps, if (!virQEMUCapsIsValid(qemuCaps, qemuctime, runUid, runGid)) goto discard; =20 - /* Discard cache if QEMU binary or libvirtd changed */ - if (qemuCaps->libvirtCtime !=3D virGetSelfLastChanged() || - qemuCaps->libvirtVersion !=3D LIBVIR_VERSION_NUMBER) { - VIR_DEBUG("Outdated capabilities for '%s': libvirt changed " - "(%lld vs %lld, %lu vs %lu)", - qemuCaps->binary, - (long long)qemuCaps->libvirtCtime, - (long long)virGetSelfLastChanged(), - (unsigned long)qemuCaps->libvirtVersion, - (unsigned long)LIBVIR_VERSION_NUMBER); - goto discard; - } - VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=3D%d", capsfile, qemuCaps->binary, (long long)qemuCaps->ctime, qemuCaps->usedQMP); --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690845734460.6126597673208; Mon, 10 Jul 2017 05:47:25 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2C986C04574B; Mon, 10 Jul 2017 12:47:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EB5B77D571; Mon, 10 Jul 2017 12:47:22 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7D2AA1804753; Mon, 10 Jul 2017 12:47:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl2xZ024882 for ; Mon, 10 Jul 2017 08:47:02 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2770D17A6B; Mon, 10 Jul 2017 12:47:02 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F3805C6A1 for ; Mon, 10 Jul 2017 12:47:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2C986C04574B Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2C986C04574B From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:46 +0200 Message-Id: <72a356d40ea2d1dd54a9039b8eb727185c5a82d2.1499690733.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 07/11] qemu: don't pass qemuctime into virQEMUCapsIsValid X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 10 Jul 2017 12:47:24 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" It's not required and following patches will change the code. Signed-off-by: Pavel Hrdina --- src/qemu/qemu_capabilities.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 4d8890aaaf..4d26e04fc3 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -4284,11 +4284,11 @@ virQEMUCapsReset(virQEMUCapsPtr qemuCaps) =20 static bool virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, - time_t qemuctime, uid_t runUid, gid_t runGid) { bool kvmUsable; + struct stat sb; =20 if (!qemuCaps->binary) return true; @@ -4305,24 +4305,19 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, return false; } =20 - if (!qemuctime) { - struct stat sb; - - if (stat(qemuCaps->binary, &sb) < 0) { - char ebuf[1024]; - VIR_DEBUG("Failed to stat QEMU binary '%s': %s", - qemuCaps->binary, - virStrerror(errno, ebuf, sizeof(ebuf))); - return false; - } - qemuctime =3D sb.st_ctime; + if (stat(qemuCaps->binary, &sb) < 0) { + char ebuf[1024]; + VIR_DEBUG("Failed to stat QEMU binary '%s': %s", + qemuCaps->binary, + virStrerror(errno, ebuf, sizeof(ebuf))); + return false; } =20 - if (qemuctime !=3D qemuCaps->ctime) { + if (sb.st_ctime !=3D qemuCaps->ctime) { VIR_DEBUG("Outdated capabilities for '%s': QEMU binary changed " "(%lld vs %lld)", qemuCaps->binary, - (long long) qemuctime, (long long) qemuCaps->ctime); + (long long) sb.st_ctime, (long long) qemuCaps->ctime); return false; } =20 @@ -4362,7 +4357,6 @@ virQEMUCapsInitCached(virCapsPtr caps, int ret =3D -1; char *binaryhash =3D NULL; struct stat sb; - time_t qemuctime =3D qemuCaps->ctime; =20 if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0) goto cleanup; @@ -4402,7 +4396,7 @@ virQEMUCapsInitCached(virCapsPtr caps, goto discard; } =20 - if (!virQEMUCapsIsValid(qemuCaps, qemuctime, runUid, runGid)) + if (!virQEMUCapsIsValid(qemuCaps, runUid, runGid)) goto discard; =20 VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=3D%d", @@ -4411,7 +4405,6 @@ virQEMUCapsInitCached(virCapsPtr caps, =20 ret =3D 1; cleanup: - qemuCaps->ctime =3D qemuctime; VIR_FREE(binaryhash); VIR_FREE(capsfile); VIR_FREE(capsdir); @@ -5413,7 +5406,7 @@ virQEMUCapsCacheValidate(virQEMUCapsCachePtr cache, virQEMUCapsPtr *qemuCaps) { if (*qemuCaps && - !virQEMUCapsIsValid(*qemuCaps, 0, cache->runUid, cache->runGid)) { + !virQEMUCapsIsValid(*qemuCaps, cache->runUid, cache->runGid)) { VIR_DEBUG("Cached capabilities %p no longer valid for %s", *qemuCaps, binary); virHashRemoveEntry(cache->binaries, binary); --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 14996908561492.450051159986401; Mon, 10 Jul 2017 05:47:36 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A140580477; Mon, 10 Jul 2017 12:47:32 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E5497ED83; Mon, 10 Jul 2017 12:47:32 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 388AA1853E36; Mon, 10 Jul 2017 12:47:28 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl3SW024909 for ; Mon, 10 Jul 2017 08:47:03 -0400 Received: by smtp.corp.redhat.com (Postfix) id 61B2C17A6B; Mon, 10 Jul 2017 12:47:03 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB7915C6A1 for ; Mon, 10 Jul 2017 12:47:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A140580477 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A140580477 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:47 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 08/11] qemu: separate virQEMUCapsInitCached out of virQEMUCapsNewForBinaryInternal X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 10 Jul 2017 12:47:33 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Preparation for switching to virFileCache where there are two callbacks, one to get a new data and second one to load a cached data. This also removes virQEMUCapsReset which is no longer required. Signed-off-by: Pavel Hrdina --- src/qemu/qemu_capabilities.c | 141 +++++++++++++++++++--------------------= ---- src/qemu/qemu_capspriv.h | 1 - tests/qemucapsprobe.c | 2 +- 3 files changed, 63 insertions(+), 81 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 4d26e04fc3..1f3240b942 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -4251,37 +4251,6 @@ virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, c= onst char *cacheDir) } =20 =20 -static void -virQEMUCapsReset(virQEMUCapsPtr qemuCaps) -{ - size_t i; - - virBitmapClearAll(qemuCaps->flags); - qemuCaps->version =3D qemuCaps->kvmVersion =3D 0; - VIR_FREE(qemuCaps->package); - qemuCaps->arch =3D VIR_ARCH_NONE; - qemuCaps->usedQMP =3D false; - - virObjectUnref(qemuCaps->kvmCPUModels); - qemuCaps->kvmCPUModels =3D NULL; - virObjectUnref(qemuCaps->tcgCPUModels); - qemuCaps->tcgCPUModels =3D NULL; - - for (i =3D 0; i < qemuCaps->nmachineTypes; i++) { - VIR_FREE(qemuCaps->machineTypes[i].name); - VIR_FREE(qemuCaps->machineTypes[i].alias); - } - VIR_FREE(qemuCaps->machineTypes); - qemuCaps->nmachineTypes =3D 0; - - VIR_FREE(qemuCaps->gicCapabilities); - qemuCaps->ngicCapabilities =3D 0; - - virQEMUCapsHostCPUDataClear(&qemuCaps->kvmCPU); - virQEMUCapsHostCPUDataClear(&qemuCaps->tcgCPU); -} - - static bool virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, uid_t runUid, @@ -4347,7 +4316,8 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, =20 static int virQEMUCapsInitCached(virCapsPtr caps, - virQEMUCapsPtr qemuCaps, + virQEMUCapsPtr *qemuCaps, + const char *binary, const char *cacheDir, uid_t runUid, gid_t runGid) @@ -4357,13 +4327,12 @@ virQEMUCapsInitCached(virCapsPtr caps, int ret =3D -1; char *binaryhash =3D NULL; struct stat sb; + virQEMUCapsPtr qemuCapsNew =3D NULL; =20 if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0) goto cleanup; =20 - if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, - qemuCaps->binary, - &binaryhash) < 0) + if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, binary, &binaryhash) <= 0) goto cleanup; =20 if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0) @@ -4379,31 +4348,38 @@ virQEMUCapsInitCached(virCapsPtr caps, if (stat(capsfile, &sb) < 0) { if (errno =3D=3D ENOENT) { VIR_DEBUG("No cached capabilities '%s' for '%s'", - capsfile, qemuCaps->binary); + capsfile, binary); ret =3D 0; goto cleanup; } virReportSystemError(errno, _("Unable to access cache '%s' for '%s'"), - capsfile, qemuCaps->binary); + capsfile, binary); goto cleanup; } =20 - if (virQEMUCapsLoadCache(caps, qemuCaps, capsfile) < 0) { + if (!(qemuCapsNew =3D virQEMUCapsNew())) + goto cleanup; + + if (VIR_STRDUP(qemuCapsNew->binary, binary) < 0) + goto discard; + + if (virQEMUCapsLoadCache(caps, qemuCapsNew, capsfile) < 0) { VIR_WARN("Failed to load cached caps from '%s' for '%s': %s", - capsfile, qemuCaps->binary, virGetLastErrorMessage()); + capsfile, qemuCapsNew->binary, virGetLastErrorMessage()); virResetLastError(); goto discard; } =20 - if (!virQEMUCapsIsValid(qemuCaps, runUid, runGid)) + if (!virQEMUCapsIsValid(qemuCapsNew, runUid, runGid)) goto discard; =20 VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=3D%d", - capsfile, qemuCaps->binary, - (long long)qemuCaps->ctime, qemuCaps->usedQMP); + capsfile, qemuCapsNew->binary, + (long long)qemuCapsNew->ctime, qemuCapsNew->usedQMP); =20 ret =3D 1; + *qemuCaps =3D qemuCapsNew; cleanup: VIR_FREE(binaryhash); VIR_FREE(capsfile); @@ -4412,9 +4388,9 @@ virQEMUCapsInitCached(virCapsPtr caps, =20 discard: VIR_DEBUG("Dropping cached capabilities '%s' for '%s'", - capsfile, qemuCaps->binary); + capsfile, qemuCapsNew->binary); ignore_value(unlink(capsfile)); - virQEMUCapsReset(qemuCaps); + virObjectUnref(qemuCapsNew); ret =3D 0; goto cleanup; } @@ -5230,14 +5206,12 @@ virQEMUCapsPtr virQEMUCapsNewForBinaryInternal(virCapsPtr caps, const char *binary, const char *libDir, - const char *cacheDir, uid_t runUid, gid_t runGid, bool qmpOnly) { virQEMUCapsPtr qemuCaps; struct stat sb; - int rv; char *qmperr =3D NULL; =20 if (!(qemuCaps =3D virQEMUCapsNew())) @@ -5265,42 +5239,30 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps, goto error; } =20 - if (!cacheDir) - rv =3D 0; - else if ((rv =3D virQEMUCapsInitCached(caps, qemuCaps, cacheDir, - runUid, runGid)) < 0) + if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid, &qmperr) < 0)= { + virQEMUCapsLogProbeFailure(binary); goto error; + } =20 - if (rv =3D=3D 0) { - if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid, &qmperr) = < 0) { - virQEMUCapsLogProbeFailure(binary); - goto error; - } - - if (qmpOnly && !qemuCaps->usedQMP) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to probe QEMU binary with QMP: %s"), - qmperr ? qmperr : _("unknown error")); - virQEMUCapsLogProbeFailure(binary); - goto error; - } - - if (!qemuCaps->usedQMP && - virQEMUCapsInitHelp(qemuCaps, runUid, runGid, qmperr) < 0) { - virQEMUCapsLogProbeFailure(binary); - goto error; - } + if (qmpOnly && !qemuCaps->usedQMP) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to probe QEMU binary with QMP: %s"), + qmperr ? qmperr : _("unknown error")); + virQEMUCapsLogProbeFailure(binary); + goto error; + } =20 - qemuCaps->libvirtCtime =3D virGetSelfLastChanged(); - qemuCaps->libvirtVersion =3D LIBVIR_VERSION_NUMBER; + if (!qemuCaps->usedQMP && + virQEMUCapsInitHelp(qemuCaps, runUid, runGid, qmperr) < 0) { + virQEMUCapsLogProbeFailure(binary); + goto error; + } =20 - if (cacheDir && - virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0) - goto error; + qemuCaps->libvirtCtime =3D virGetSelfLastChanged(); + qemuCaps->libvirtVersion =3D LIBVIR_VERSION_NUMBER; =20 - virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_KVM); - virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_QEMU); - } + virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_KVM); + virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_QEMU); =20 cleanup: VIR_FREE(qmperr); @@ -5320,8 +5282,29 @@ virQEMUCapsNewForBinary(virCapsPtr caps, uid_t runUid, gid_t runGid) { - return virQEMUCapsNewForBinaryInternal(caps, binary, libDir, cacheDir, - runUid, runGid, false); + int rv; + virQEMUCapsPtr qemuCaps =3D NULL; + + if ((rv =3D virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir, + runUid, runGid)) < 0) + goto error; + + if (rv =3D=3D 0) { + if (!(qemuCaps =3D virQEMUCapsNewForBinaryInternal(caps, binary, + libDir, runUid, + runGid, false))) { + goto error; + } + + if (virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0) + goto error; + } + + return qemuCaps; + + error: + virObjectUnref(qemuCaps); + return NULL; } =20 =20 diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h index 1162e0b284..3458fc0700 100644 --- a/src/qemu/qemu_capspriv.h +++ b/src/qemu/qemu_capspriv.h @@ -43,7 +43,6 @@ virQEMUCapsPtr virQEMUCapsNewForBinaryInternal(virCapsPtr caps, const char *binary, const char *libDir, - const char *cacheDir, uid_t runUid, gid_t runGid, bool qmpOnly); diff --git a/tests/qemucapsprobe.c b/tests/qemucapsprobe.c index 662c7036ed..581ac38465 100644 --- a/tests/qemucapsprobe.c +++ b/tests/qemucapsprobe.c @@ -70,7 +70,7 @@ main(int argc, char **argv) if (virThreadCreate(&thread, false, eventLoop, NULL) < 0) return EXIT_FAILURE; =20 - if (!(caps =3D virQEMUCapsNewForBinaryInternal(NULL, argv[1], "/tmp", = NULL, + if (!(caps =3D virQEMUCapsNewForBinaryInternal(NULL, argv[1], "/tmp", -1, -1, true))) return EXIT_FAILURE; =20 --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 149969083833935.81333389990368; Mon, 10 Jul 2017 05:47:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C3A15BC6BB; Mon, 10 Jul 2017 12:47:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 973B017A95; Mon, 10 Jul 2017 12:47:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 359111803B26; Mon, 10 Jul 2017 12:47:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl4q2024914 for ; Mon, 10 Jul 2017 08:47:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF5721797E; Mon, 10 Jul 2017 12:47:04 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 34BFE17A86 for ; Mon, 10 Jul 2017 12:47:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C3A15BC6BB Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C3A15BC6BB From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:48 +0200 Message-Id: <45c47fa8ccb981dc008d170d32c974cf7576f4aa.1499690733.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 09/11] qemu: introduce struct _virQEMUCapsCachePriv X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 10 Jul 2017 12:47:17 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This will store private data that will be used by following patches when switching to virFileCache. Signed-off-by: Pavel Hrdina Reviewed-by: Jiri Denemark --- src/qemu/qemu_capabilities.c | 51 ++++++++++++++++++++++++++--------------= ---- src/qemu/qemu_capspriv.h | 12 ++++++++--- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 1f3240b942..e190cfa8b1 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -3765,6 +3765,14 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps, } =20 =20 +static void +virQEMUCapsCachePrivFree(virQEMUCapsCachePrivPtr priv) +{ + VIR_FREE(priv->libDir); + VIR_FREE(priv); +} + + /* * Parsing a doc that looks like * @@ -4253,8 +4261,7 @@ virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, co= nst char *cacheDir) =20 static bool virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, - uid_t runUid, - gid_t runGid) + virQEMUCapsCachePrivPtr priv) { bool kvmUsable; struct stat sb; @@ -4291,7 +4298,7 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, } =20 kvmUsable =3D virFileAccessibleAs("/dev/kvm", R_OK | W_OK, - runUid, runGid) =3D=3D 0; + priv->runUid, priv->runGid) =3D=3D 0; =20 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) && virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KVM) && @@ -4319,8 +4326,7 @@ virQEMUCapsInitCached(virCapsPtr caps, virQEMUCapsPtr *qemuCaps, const char *binary, const char *cacheDir, - uid_t runUid, - gid_t runGid) + virQEMUCapsCachePrivPtr priv) { char *capsdir =3D NULL; char *capsfile =3D NULL; @@ -4371,7 +4377,7 @@ virQEMUCapsInitCached(virCapsPtr caps, goto discard; } =20 - if (!virQEMUCapsIsValid(qemuCapsNew, runUid, runGid)) + if (!virQEMUCapsIsValid(qemuCapsNew, priv)) goto discard; =20 VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=3D%d", @@ -5277,22 +5283,21 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps, static virQEMUCapsPtr virQEMUCapsNewForBinary(virCapsPtr caps, const char *binary, - const char *libDir, const char *cacheDir, - uid_t runUid, - gid_t runGid) + virQEMUCapsCachePrivPtr priv) { int rv; virQEMUCapsPtr qemuCaps =3D NULL; =20 - if ((rv =3D virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir, - runUid, runGid)) < 0) + if ((rv =3D virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir, p= riv)) < 0) goto error; =20 if (rv =3D=3D 0) { if (!(qemuCaps =3D virQEMUCapsNewForBinaryInternal(caps, binary, - libDir, runUid, - runGid, false))) { + priv->libDir, + priv->runUid, + priv->runGid, + false))) { goto error; } =20 @@ -5366,13 +5371,17 @@ virQEMUCapsCacheNew(const char *libDir, =20 if (!(cache->binaries =3D virHashCreate(10, virObjectFreeHashData))) goto error; - if (VIR_STRDUP(cache->libDir, libDir) < 0) - goto error; if (VIR_STRDUP(cache->cacheDir, cacheDir) < 0) goto error; =20 - cache->runUid =3D runUid; - cache->runGid =3D runGid; + if (VIR_ALLOC(cache->priv) < 0) + goto error; + + if (VIR_STRDUP(cache->priv->libDir, libDir) < 0) + goto error; + + cache->priv->runUid =3D runUid; + cache->priv->runGid =3D runGid; =20 return cache; =20 @@ -5389,7 +5398,7 @@ virQEMUCapsCacheValidate(virQEMUCapsCachePtr cache, virQEMUCapsPtr *qemuCaps) { if (*qemuCaps && - !virQEMUCapsIsValid(*qemuCaps, cache->runUid, cache->runGid)) { + !virQEMUCapsIsValid(*qemuCaps, cache->priv)) { VIR_DEBUG("Cached capabilities %p no longer valid for %s", *qemuCaps, binary); virHashRemoveEntry(cache->binaries, binary); @@ -5399,8 +5408,8 @@ virQEMUCapsCacheValidate(virQEMUCapsCachePtr cache, if (!*qemuCaps) { VIR_DEBUG("Creating capabilities for %s", binary); *qemuCaps =3D virQEMUCapsNewForBinary(caps, binary, - cache->libDir, cache->cacheDir, - cache->runUid, cache->runGid); + cache->cacheDir, + cache->priv); if (*qemuCaps) { VIR_DEBUG("Caching capabilities %p for %s", *qemuCaps, binary); if (virHashAddEntry(cache->binaries, binary, *qemuCaps) < 0) { @@ -5519,7 +5528,7 @@ virQEMUCapsCacheFree(virQEMUCapsCachePtr cache) if (!cache) return; =20 - VIR_FREE(cache->libDir); + virQEMUCapsCachePrivFree(cache->priv); VIR_FREE(cache->cacheDir); virHashFree(cache->binaries); virMutexDestroy(&cache->lock); diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h index 3458fc0700..61e7c34d12 100644 --- a/src/qemu/qemu_capspriv.h +++ b/src/qemu/qemu_capspriv.h @@ -28,13 +28,19 @@ #ifndef __QEMU_CAPSPRIV_H__ # define __QEMU_CAPSPRIV_H__ =20 +struct _virQEMUCapsCachePriv { + char *libDir; + uid_t runUid; + gid_t runGid; +}; +typedef struct _virQEMUCapsCachePriv virQEMUCapsCachePriv; +typedef virQEMUCapsCachePriv *virQEMUCapsCachePrivPtr; + struct _virQEMUCapsCache { virMutex lock; virHashTablePtr binaries; - char *libDir; char *cacheDir; - uid_t runUid; - gid_t runGid; + virQEMUCapsCachePrivPtr priv; }; =20 virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps); --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499690885400197.80696468676842; Mon, 10 Jul 2017 05:48:05 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 46B7F80F96; Mon, 10 Jul 2017 12:48:03 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1ABAF7D54A; Mon, 10 Jul 2017 12:48:03 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id AC8CA28A; Mon, 10 Jul 2017 12:47:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl5J4024927 for ; Mon, 10 Jul 2017 08:47:05 -0400 Received: by smtp.corp.redhat.com (Postfix) id 837A917A6B; Mon, 10 Jul 2017 12:47:05 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 097E21797E for ; Mon, 10 Jul 2017 12:47:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 46B7F80F96 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 46B7F80F96 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:49 +0200 Message-Id: <818da6129ee036cdaf750237264b2c8d5a240757.1499690733.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 10/11] qemu: switch QEMU capabilities to use virFileCache X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 10 Jul 2017 12:48:04 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The switch contains considerable amount of changes: virQEMUCapsRememberCached() is removed because this is now handled by virFileCacheSave(). virQEMUCapsInitCached() is removed because this is now handled by virFileCacheLoad(). virQEMUCapsNewForBinary() is split into two functions, virQEMUCapsNewData() which creates new data if there is nothing cached and virQEMUCapsLoadFile() which loads the cached data. This is now handled by virFileCacheNewData(). virQEMUCapsCacheValidate() is removed because this is now handled by virFileCacheValidate(). virQEMUCapsCacheFree() is removed because it's no longer required. Add virCapsPtr into virQEMUCapsCachePriv because for each call of virFileCacheLookup*() we need to use current virCapsPtr. Signed-off-by: Pavel Hrdina --- src/qemu/qemu_capabilities.c | 322 ++++++++++++---------------------------= ---- src/qemu/qemu_capabilities.h | 17 +-- src/qemu/qemu_capspriv.h | 8 +- src/qemu/qemu_conf.h | 3 +- src/qemu/qemu_driver.c | 2 +- tests/testutilsqemu.c | 9 +- tests/testutilsqemu.h | 3 +- 7 files changed, 102 insertions(+), 262 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index e190cfa8b1..c3e09616de 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -29,6 +29,7 @@ #include "virlog.h" #include "virerror.h" #include "virfile.h" +#include "virfilecache.h" #include "virpidfile.h" #include "virprocess.h" #include "cpu/cpu.h" @@ -953,7 +954,7 @@ virQEMUCapsFindBinaryForArch(virArch hostarch, =20 static int virQEMUCapsInitGuest(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, virArch hostarch, virArch guestarch) { @@ -1170,7 +1171,8 @@ virQEMUCapsProbeHostCPUForEmulator(virCapsPtr caps, } =20 =20 -virCapsPtr virQEMUCapsInit(virQEMUCapsCachePtr cache) +virCapsPtr +virQEMUCapsInit(virFileCachePtr cache) { virCapsPtr caps; size_t i; @@ -2163,7 +2165,7 @@ virQEMUCapsExtractDeviceStr(const char *qemu, =20 =20 int virQEMUCapsGetDefaultVersion(virCapsPtr caps, - virQEMUCapsCachePtr capsCache, + virFileCachePtr capsCache, unsigned int *version) { virQEMUCapsPtr qemucaps; @@ -3766,8 +3768,10 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps, =20 =20 static void -virQEMUCapsCachePrivFree(virQEMUCapsCachePrivPtr priv) +virQEMUCapsCachePrivFree(void *privData) { + virQEMUCapsCachePrivPtr priv =3D privData; + VIR_FREE(priv->libDir); VIR_FREE(priv); } @@ -4196,8 +4200,11 @@ virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps) =20 =20 static int -virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const char *filename) +virQEMUCapsSaveFile(void *data, + const char *filename, + void *privData ATTRIBUTE_UNUSED) { + virQEMUCapsPtr qemuCaps =3D data; char *xml =3D NULL; int ret =3D -1; =20 @@ -4221,48 +4228,13 @@ virQEMUCapsSaveCache(virQEMUCapsPtr qemuCaps, const= char *filename) return ret; } =20 -static int -virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, const char *cacheDir) -{ - char *capsdir =3D NULL; - char *capsfile =3D NULL; - int ret =3D -1; - char *binaryhash =3D NULL; - - if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0) - goto cleanup; - - if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, - qemuCaps->binary, - &binaryhash) < 0) - goto cleanup; - - if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0) - goto cleanup; - - if (virFileMakePath(capsdir) < 0) { - virReportSystemError(errno, - _("Unable to create directory '%s'"), - capsdir); - goto cleanup; - } - - if (virQEMUCapsSaveCache(qemuCaps, capsfile) < 0) - goto cleanup; - - ret =3D 0; - cleanup: - VIR_FREE(binaryhash); - VIR_FREE(capsfile); - VIR_FREE(capsdir); - return ret; -} - =20 static bool -virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, - virQEMUCapsCachePrivPtr priv) +virQEMUCapsIsValid(void *data, + void *privData) { + virQEMUCapsPtr qemuCaps =3D data; + virQEMUCapsCachePrivPtr priv =3D privData; bool kvmUsable; struct stat sb; =20 @@ -4321,87 +4293,6 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, } =20 =20 -static int -virQEMUCapsInitCached(virCapsPtr caps, - virQEMUCapsPtr *qemuCaps, - const char *binary, - const char *cacheDir, - virQEMUCapsCachePrivPtr priv) -{ - char *capsdir =3D NULL; - char *capsfile =3D NULL; - int ret =3D -1; - char *binaryhash =3D NULL; - struct stat sb; - virQEMUCapsPtr qemuCapsNew =3D NULL; - - if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0) - goto cleanup; - - if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, binary, &binaryhash) <= 0) - goto cleanup; - - if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0) - goto cleanup; - - if (virFileMakePath(capsdir) < 0) { - virReportSystemError(errno, - _("Unable to create directory '%s'"), - capsdir); - goto cleanup; - } - - if (stat(capsfile, &sb) < 0) { - if (errno =3D=3D ENOENT) { - VIR_DEBUG("No cached capabilities '%s' for '%s'", - capsfile, binary); - ret =3D 0; - goto cleanup; - } - virReportSystemError(errno, - _("Unable to access cache '%s' for '%s'"), - capsfile, binary); - goto cleanup; - } - - if (!(qemuCapsNew =3D virQEMUCapsNew())) - goto cleanup; - - if (VIR_STRDUP(qemuCapsNew->binary, binary) < 0) - goto discard; - - if (virQEMUCapsLoadCache(caps, qemuCapsNew, capsfile) < 0) { - VIR_WARN("Failed to load cached caps from '%s' for '%s': %s", - capsfile, qemuCapsNew->binary, virGetLastErrorMessage()); - virResetLastError(); - goto discard; - } - - if (!virQEMUCapsIsValid(qemuCapsNew, priv)) - goto discard; - - VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=3D%d", - capsfile, qemuCapsNew->binary, - (long long)qemuCapsNew->ctime, qemuCapsNew->usedQMP); - - ret =3D 1; - *qemuCaps =3D qemuCapsNew; - cleanup: - VIR_FREE(binaryhash); - VIR_FREE(capsfile); - VIR_FREE(capsdir); - return ret; - - discard: - VIR_DEBUG("Dropping cached capabilities '%s' for '%s'", - capsfile, qemuCapsNew->binary); - ignore_value(unlink(capsfile)); - virObjectUnref(qemuCapsNew); - ret =3D 0; - goto cleanup; -} - - #define QEMU_SYSTEM_PREFIX "qemu-system-" =20 static int @@ -5280,36 +5171,45 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps, goto cleanup; } =20 -static virQEMUCapsPtr -virQEMUCapsNewForBinary(virCapsPtr caps, - const char *binary, - const char *cacheDir, - virQEMUCapsCachePrivPtr priv) +static void * +virQEMUCapsNewData(const char *binary, + void *privData) { - int rv; - virQEMUCapsPtr qemuCaps =3D NULL; + virQEMUCapsCachePrivPtr priv =3D privData; =20 - if ((rv =3D virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir, p= riv)) < 0) - goto error; + return virQEMUCapsNewForBinaryInternal(priv->caps, + binary, + priv->libDir, + priv->runUid, + priv->runGid, + false); +} + + +static void * +virQEMUCapsLoadFile(const char *filename, + const char *binary, + void *privData) +{ + virQEMUCapsPtr qemuCaps =3D virQEMUCapsNew(); + virQEMUCapsCachePrivPtr priv =3D privData; =20 - if (rv =3D=3D 0) { - if (!(qemuCaps =3D virQEMUCapsNewForBinaryInternal(caps, binary, - priv->libDir, - priv->runUid, - priv->runGid, - false))) { - goto error; - } + if (!qemuCaps) + return NULL; =20 - if (virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0) - goto error; - } + if (VIR_STRDUP(qemuCaps->binary, binary) < 0) + goto error; + + if (virQEMUCapsLoadCache(priv->caps, qemuCaps, filename) < 0) + goto error; =20 + cleanup: return qemuCaps; =20 error: virObjectUnref(qemuCaps); - return NULL; + qemuCaps =3D NULL; + goto cleanup; } =20 =20 @@ -5351,90 +5251,63 @@ virQEMUCapsFilterByMachineType(virQEMUCapsPtr qemuC= aps, } =20 =20 -virQEMUCapsCachePtr +virFileCacheHandlers qemuCapsCacheHandlers =3D { + .isValid =3D virQEMUCapsIsValid, + .newData =3D virQEMUCapsNewData, + .loadFile =3D virQEMUCapsLoadFile, + .saveFile =3D virQEMUCapsSaveFile, + .privFree =3D virQEMUCapsCachePrivFree, +}; + + +virFileCachePtr virQEMUCapsCacheNew(const char *libDir, const char *cacheDir, uid_t runUid, gid_t runGid) { - virQEMUCapsCachePtr cache; + char *capsCacheDir =3D NULL; + virFileCachePtr cache =3D NULL; + virQEMUCapsCachePrivPtr priv =3D NULL; =20 - if (VIR_ALLOC(cache) < 0) - return NULL; - - if (virMutexInit(&cache->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - VIR_FREE(cache); - return NULL; - } - - if (!(cache->binaries =3D virHashCreate(10, virObjectFreeHashData))) + if (virAsprintf(&capsCacheDir, "%s/capabilities", cacheDir) < 0) goto error; - if (VIR_STRDUP(cache->cacheDir, cacheDir) < 0) + + if (!(cache =3D virFileCacheNew(capsCacheDir, qemuCapsCacheHandlers))) goto error; =20 - if (VIR_ALLOC(cache->priv) < 0) + if (VIR_ALLOC(priv) < 0) goto error; + virFileCacheSetPriv(cache, priv); =20 - if (VIR_STRDUP(cache->priv->libDir, libDir) < 0) + if (VIR_STRDUP(priv->libDir, libDir) < 0) goto error; =20 - cache->priv->runUid =3D runUid; - cache->priv->runGid =3D runGid; + priv->runUid =3D runUid; + priv->runGid =3D runGid; =20 + cleanup: + VIR_FREE(capsCacheDir); return cache; =20 error: - virQEMUCapsCacheFree(cache); - return NULL; -} - - -static void ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) -virQEMUCapsCacheValidate(virQEMUCapsCachePtr cache, - const char *binary, - virCapsPtr caps, - virQEMUCapsPtr *qemuCaps) -{ - if (*qemuCaps && - !virQEMUCapsIsValid(*qemuCaps, cache->priv)) { - VIR_DEBUG("Cached capabilities %p no longer valid for %s", - *qemuCaps, binary); - virHashRemoveEntry(cache->binaries, binary); - *qemuCaps =3D NULL; - } - - if (!*qemuCaps) { - VIR_DEBUG("Creating capabilities for %s", binary); - *qemuCaps =3D virQEMUCapsNewForBinary(caps, binary, - cache->cacheDir, - cache->priv); - if (*qemuCaps) { - VIR_DEBUG("Caching capabilities %p for %s", *qemuCaps, binary); - if (virHashAddEntry(cache->binaries, binary, *qemuCaps) < 0) { - virObjectUnref(*qemuCaps); - *qemuCaps =3D NULL; - } - } - } + virObjectUnref(cache); + cache =3D NULL; + goto cleanup; } =20 =20 virQEMUCapsPtr virQEMUCapsCacheLookup(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, const char *binary) { virQEMUCapsPtr ret =3D NULL; + virQEMUCapsCachePrivPtr priv =3D virFileCacheGetPriv(cache); =20 - virMutexLock(&cache->lock); - - ret =3D virHashLookup(cache->binaries, binary); - virQEMUCapsCacheValidate(cache, binary, caps, &ret); - virObjectRef(ret); - - virMutexUnlock(&cache->lock); + priv->caps =3D caps; + ret =3D virFileCacheLookup(cache, binary); + priv->caps =3D NULL; =20 VIR_DEBUG("Returning caps %p for %s", ret, binary); return ret; @@ -5443,7 +5316,7 @@ virQEMUCapsCacheLookup(virCapsPtr caps, =20 virQEMUCapsPtr virQEMUCapsCacheLookupCopy(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, const char *binary, const char *machineType) { @@ -5478,64 +5351,39 @@ virQEMUCapsCompareArch(const void *payload, =20 virQEMUCapsPtr virQEMUCapsCacheLookupByArch(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, virArch arch) { virQEMUCapsPtr ret =3D NULL; + virQEMUCapsCachePrivPtr priv =3D virFileCacheGetPriv(cache); virArch target; struct virQEMUCapsSearchData data =3D { .arch =3D arch }; =20 - virMutexLock(&cache->lock); - ret =3D virHashSearch(cache->binaries, virQEMUCapsCompareArch, &data, = NULL); + priv->caps =3D caps; + ret =3D virFileCacheLookupByFunc(cache, virQEMUCapsCompareArch, &data); if (!ret) { /* If the first attempt at finding capabilities has failed, try * again using the QEMU target as lookup key instead */ target =3D virQEMUCapsFindTarget(virArchFromHost(), data.arch); if (target !=3D data.arch) { data.arch =3D target; - ret =3D virHashSearch(cache->binaries, virQEMUCapsCompareArch, - &data, NULL); + ret =3D virFileCacheLookupByFunc(cache, virQEMUCapsCompareArch= , &data); } } + priv->caps =3D NULL; =20 - if (ret) { - char *binary; - - if (VIR_STRDUP(binary, ret->binary) < 0) { - ret =3D NULL; - } else { - virQEMUCapsCacheValidate(cache, binary, caps, &ret); - VIR_FREE(binary); - } - } else { + if (!ret) { virReportError(VIR_ERR_INVALID_ARG, _("unable to find any emulator to serve '%s' " "architecture"), virArchToString(arch)); } =20 - virObjectRef(ret); - virMutexUnlock(&cache->lock); - VIR_DEBUG("Returning caps %p for arch %s", ret, virArchToString(arch)); =20 return ret; } =20 =20 -void -virQEMUCapsCacheFree(virQEMUCapsCachePtr cache) -{ - if (!cache) - return; - - virQEMUCapsCachePrivFree(cache->priv); - VIR_FREE(cache->cacheDir); - virHashFree(cache->binaries); - virMutexDestroy(&cache->lock); - VIR_FREE(cache); -} - - bool virQEMUCapsSupportsChardev(const virDomainDef *def, virQEMUCapsPtr qemuCaps, diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 7f5382df62..77b519d939 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -30,6 +30,7 @@ # include "qemu_monitor.h" # include "domain_capabilities.h" # include "virfirmware.h" +# include "virfilecache.h" =20 /* * Internal flags to keep track of qemu command line capabilities @@ -423,9 +424,6 @@ typedef enum { typedef struct _virQEMUCaps virQEMUCaps; typedef virQEMUCaps *virQEMUCapsPtr; =20 -typedef struct _virQEMUCapsCache virQEMUCapsCache; -typedef virQEMUCapsCache *virQEMUCapsCachePtr; - virQEMUCapsPtr virQEMUCapsNew(void); =20 void virQEMUCapsSet(virQEMUCapsPtr qemuCaps, @@ -497,25 +495,24 @@ int virQEMUCapsGetMachineTypesCaps(virQEMUCapsPtr qem= uCaps, void virQEMUCapsFilterByMachineType(virQEMUCapsPtr qemuCaps, const char *machineType); =20 -virQEMUCapsCachePtr virQEMUCapsCacheNew(const char *libDir, +virFileCachePtr virQEMUCapsCacheNew(const char *libDir, const char *cacheDir, uid_t uid, gid_t gid); virQEMUCapsPtr virQEMUCapsCacheLookup(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, const char *binary); virQEMUCapsPtr virQEMUCapsCacheLookupCopy(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, const char *binary, const char *machineType); virQEMUCapsPtr virQEMUCapsCacheLookupByArch(virCapsPtr caps, - virQEMUCapsCachePtr cache, + virFileCachePtr cache, virArch arch); -void virQEMUCapsCacheFree(virQEMUCapsCachePtr cache); =20 -virCapsPtr virQEMUCapsInit(virQEMUCapsCachePtr cache); +virCapsPtr virQEMUCapsInit(virFileCachePtr cache); =20 int virQEMUCapsGetDefaultVersion(virCapsPtr caps, - virQEMUCapsCachePtr capsCache, + virFileCachePtr capsCache, unsigned int *version); =20 VIR_ENUM_DECL(virQEMUCaps); diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h index 61e7c34d12..77e41d753b 100644 --- a/src/qemu/qemu_capspriv.h +++ b/src/qemu/qemu_capspriv.h @@ -29,6 +29,7 @@ # define __QEMU_CAPSPRIV_H__ =20 struct _virQEMUCapsCachePriv { + virCapsPtr caps; char *libDir; uid_t runUid; gid_t runGid; @@ -36,13 +37,6 @@ struct _virQEMUCapsCachePriv { typedef struct _virQEMUCapsCachePriv virQEMUCapsCachePriv; typedef virQEMUCapsCachePriv *virQEMUCapsCachePrivPtr; =20 -struct _virQEMUCapsCache { - virMutex lock; - virHashTablePtr binaries; - char *cacheDir; - virQEMUCapsCachePrivPtr priv; -}; - virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps); =20 virQEMUCapsPtr diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 1407eef4d4..3013f24ed4 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -48,6 +48,7 @@ # include "virclosecallbacks.h" # include "virhostdev.h" # include "virfile.h" +# include "virfilecache.h" # include "virfirmware.h" =20 # ifdef CPU_SETSIZE /* Linux */ @@ -245,7 +246,7 @@ struct _virQEMUDriver { virDomainXMLOptionPtr xmlopt; =20 /* Immutable pointer, self-locking APIs */ - virQEMUCapsCachePtr qemuCapsCache; + virFileCachePtr qemuCapsCache; =20 /* Immutable pointer, self-locking APIs */ virObjectEventStatePtr domainEventState; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index cdb727b554..fd705645c4 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1078,7 +1078,7 @@ qemuStateCleanup(void) virObjectUnref(qemu_driver->hostdevMgr); virHashFree(qemu_driver->sharedDevices); virObjectUnref(qemu_driver->caps); - virQEMUCapsCacheFree(qemu_driver->qemuCapsCache); + virObjectUnref(qemu_driver->qemuCapsCache); =20 virObjectUnref(qemu_driver->domains); virObjectUnref(qemu_driver->remotePorts); diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index 88e11ba14a..710d12dc4c 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -11,6 +11,7 @@ # define __QEMU_CAPSPRIV_H_ALLOW__ # include "qemu/qemu_capspriv.h" # include "virstring.h" +# include "virfilecache.h" =20 # define VIR_FROM_THIS VIR_FROM_QEMU =20 @@ -587,14 +588,14 @@ void qemuTestDriverFree(virQEMUDriver *driver) virFileDeleteTree(driver->config->stateDir); virFileDeleteTree(driver->config->configDir); } - virQEMUCapsCacheFree(driver->qemuCapsCache); + virObjectUnref(driver->qemuCapsCache); virObjectUnref(driver->xmlopt); virObjectUnref(driver->caps); virObjectUnref(driver->config); virObjectUnref(driver->securityManager); } =20 -int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache, +int qemuTestCapsCacheInsert(virFileCachePtr cache, virQEMUCapsPtr caps) { size_t i; @@ -609,9 +610,7 @@ int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache, =20 for (i =3D 0; i < ARRAY_CARDINALITY(QEMUBinList); i++) { virObjectRef(tmpCaps); - if (virHashUpdateEntry(cache->binaries, - QEMUBinList[i], - tmpCaps) < 0) { + if (virFileCacheInsertData(cache, QEMUBinList[i], tmpCaps) < 0) { virObjectUnref(tmpCaps); return -1; } diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h index 05e5651ae8..f29c6e5d62 100644 --- a/tests/testutilsqemu.h +++ b/tests/testutilsqemu.h @@ -1,6 +1,7 @@ #ifdef WITH_QEMU =20 # include "capabilities.h" +# include "virfilecache.h" # include "domain_conf.h" # include "qemu/qemu_capabilities.h" # include "qemu/qemu_conf.h" @@ -30,7 +31,7 @@ void qemuTestSetHostCPU(virCapsPtr caps, =20 int qemuTestDriverInit(virQEMUDriver *driver); void qemuTestDriverFree(virQEMUDriver *driver); -int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache, +int qemuTestCapsCacheInsert(virFileCachePtr cache, virQEMUCapsPtr caps); =20 int testQemuCapsSetGIC(virQEMUCapsPtr qemuCaps, --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu May 2 22:06:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 14996908476761014.7184298610938; Mon, 10 Jul 2017 05:47:27 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F1FC7624A4; Mon, 10 Jul 2017 12:47:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AD6847D51B; Mon, 10 Jul 2017 12:47:23 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 114071853E31; Mon, 10 Jul 2017 12:47:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6ACl6NG024939 for ; Mon, 10 Jul 2017 08:47:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5647917A6B; Mon, 10 Jul 2017 12:47:06 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id D248B1797E for ; Mon, 10 Jul 2017 12:47:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F1FC7624A4 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com F1FC7624A4 From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 10 Jul 2017 14:46:50 +0200 Message-Id: <00d69517188f924630570881f0429375ec4f8f0e.1499690734.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 11/11] qemu: privatize _virQEMUCapsCachePriv struct X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 10 Jul 2017 12:47:24 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: Jiri Denemark --- src/qemu/qemu_capabilities.c | 10 ++++++++++ src/qemu/qemu_capspriv.h | 9 --------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index c3e09616de..0395a397ce 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -3767,6 +3767,16 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps, } =20 =20 +struct _virQEMUCapsCachePriv { + virCapsPtr caps; + char *libDir; + uid_t runUid; + gid_t runGid; +}; +typedef struct _virQEMUCapsCachePriv virQEMUCapsCachePriv; +typedef virQEMUCapsCachePriv *virQEMUCapsCachePrivPtr; + + static void virQEMUCapsCachePrivFree(void *privData) { diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h index 77e41d753b..9c360438db 100644 --- a/src/qemu/qemu_capspriv.h +++ b/src/qemu/qemu_capspriv.h @@ -28,15 +28,6 @@ #ifndef __QEMU_CAPSPRIV_H__ # define __QEMU_CAPSPRIV_H__ =20 -struct _virQEMUCapsCachePriv { - virCapsPtr caps; - char *libDir; - uid_t runUid; - gid_t runGid; -}; -typedef struct _virQEMUCapsCachePriv virQEMUCapsCachePriv; -typedef virQEMUCapsCachePriv *virQEMUCapsCachePrivPtr; - virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps); =20 virQEMUCapsPtr --=20 2.13.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list