From nobody Mon May 6 23:34:43 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524671839635116.48470491499654; Wed, 25 Apr 2018 08:57:19 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BDBAC8765C; Wed, 25 Apr 2018 15:57: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 1E6452010CD9; Wed, 25 Apr 2018 15:57: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 7070C18033EB; Wed, 25 Apr 2018 15:57:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFqjiw026731 for ; Wed, 25 Apr 2018 11:52:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id 95A86202323B; Wed, 25 Apr 2018 15:52:45 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE25A2023239; Wed, 25 Apr 2018 15:52:44 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 16:52:38 +0100 Message-Id: <20180425155243.23406-2-berrange@redhat.com> In-Reply-To: <20180425155243.23406-1-berrange@redhat.com> References: <20180425155243.23406-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 1/6] util: create new virmodule.{c, h} files for dlopen support code 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: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 25 Apr 2018 15:57:18 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 The driver.{c,h} files are primarily targetted at loading hypervisor drivers and some helper functions in that area. It also, however, contains a generically useful function for loading extension modules that is called by the storage driver. Split that functionality off into a new virmodule.{c,h} file to isolate it. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/driver.c | 133 +------------------------------- src/driver.h | 3 - src/libvirt_driver_modules.syms | 1 - src/libvirt_private.syms | 3 + src/storage/storage_backend.c | 3 +- src/util/Makefile.inc.am | 2 + src/util/virmodule.c | 163 ++++++++++++++++++++++++++++++++++++= ++++ src/util/virmodule.h | 29 +++++++ 8 files changed, 201 insertions(+), 136 deletions(-) create mode 100644 src/util/virmodule.c create mode 100644 src/util/virmodule.h diff --git a/src/driver.c b/src/driver.c index 447f61d554..8b5ade763f 100644 --- a/src/driver.c +++ b/src/driver.c @@ -28,6 +28,7 @@ #include "viralloc.h" #include "virfile.h" #include "virlog.h" +#include "virmodule.h" #include "virthread.h" #include "configmake.h" =20 @@ -38,136 +39,6 @@ VIR_LOG_INIT("driver"); /* XXX re-implement this for other OS, or use libtools helper lib ? */ #define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver" =20 -#ifdef HAVE_DLFCN_H -# include - - -static void * -virDriverLoadModuleFile(const char *file) -{ - void *handle =3D NULL; - int flags =3D RTLD_NOW | RTLD_GLOBAL; - -# ifdef RTLD_NODELETE - flags |=3D RTLD_NODELETE; -# endif - - VIR_DEBUG("Load module file '%s'", file); - - virUpdateSelfLastChanged(file); - - if (!(handle =3D dlopen(file, flags))) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to load module '%s': %s"), file, dlerror(= )); - return NULL; - } - - return handle; -} - - -static void * -virDriverLoadModuleFunc(void *handle, - const char *file, - const char *funcname) -{ - void *regsym; - - VIR_DEBUG("Lookup function '%s'", funcname); - - if (!(regsym =3D dlsym(handle, funcname))) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to find symbol '%s' in module '%s': %s"), - funcname, file, dlerror()); - return NULL; - } - - return regsym; -} - - -/** - * virDriverLoadModuleFull: - * @path: filename of module to load - * @regfunc: name of the function that registers the module - * - * Loads a loadable module named @path and calls the - * registration function @regfunc. The module will never - * be unloaded because unloading is not safe in a multi-threaded - * application. - * - * The module is automatically looked up in the appropriate place (git or - * installed directory). - * - * Returns 0 on success, 1 if the module was not found and -1 on any error. - */ -int -virDriverLoadModuleFull(const char *path, - const char *regfunc, - bool required) -{ - void *rethandle =3D NULL; - int (*regsym)(void); - int ret =3D -1; - - if (!virFileExists(path)) { - if (required) { - virReportSystemError(errno, - _("Failed to find module '%s'"), path); - return -1; - } else { - VIR_INFO("Module '%s' does not exist", path); - return 1; - } - } - - if (!(rethandle =3D virDriverLoadModuleFile(path))) - goto cleanup; - - if (!(regsym =3D virDriverLoadModuleFunc(rethandle, path, regfunc))) - goto cleanup; - - if ((*regsym)() < 0) { - /* regsym() should report an error itself, but lets - * just make sure */ - virErrorPtr err =3D virGetLastError(); - if (err =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to execute symbol '%s' in module '%s'= "), - regfunc, path); - } - goto cleanup; - } - - rethandle =3D NULL; - ret =3D 0; - - cleanup: - if (rethandle) - dlclose(rethandle); - return ret; -} - -#else /* ! HAVE_DLFCN_H */ -int -virDriverLoadModuleFull(const char *path ATTRIBUTE_UNUSED, - const char *regfunc ATTRIBUTE_UNUSED, - bool required) -{ - VIR_DEBUG("dlopen not available on this platform"); - if (required) { - virReportSystemError(ENOSYS, - _("Failed to find module '%s': %s"), path); - return -1; - } else { - /* Since we have no dlopen(), but definition we have no - * loadable modules on disk, so we can resaonably - * return '1' instead of an error. - */ - return 1; - } -} -#endif /* ! HAVE_DLFCN_H */ =20 =20 int @@ -188,7 +59,7 @@ virDriverLoadModule(const char *name, "LIBVIRT_DRIVER_DIR"))) return -1; =20 - ret =3D virDriverLoadModuleFull(modfile, regfunc, required); + ret =3D virModuleLoad(modfile, regfunc, required); =20 VIR_FREE(modfile); =20 diff --git a/src/driver.h b/src/driver.h index b4e50ab987..0b1f7a2269 100644 --- a/src/driver.h +++ b/src/driver.h @@ -110,9 +110,6 @@ int virSetSharedStorageDriver(virStorageDriverPtr drive= r) ATTRIBUTE_RETURN_CHECK int virDriverLoadModule(const char *name, const char *regfunc, bool required); -int virDriverLoadModuleFull(const char *path, - const char *regfunc, - bool required); =20 virConnectPtr virGetConnectInterface(void); virConnectPtr virGetConnectNetwork(void); diff --git a/src/libvirt_driver_modules.syms b/src/libvirt_driver_modules.s= yms index bd9bf1c315..f9d0ee9b97 100644 --- a/src/libvirt_driver_modules.syms +++ b/src/libvirt_driver_modules.syms @@ -4,7 +4,6 @@ =20 # driver.h virDriverLoadModule; -virDriverLoadModuleFull; =20 # Let emacs know we want case-insensitive sorting # Local Variables: diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d2728749fb..1051a105b8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2228,6 +2228,9 @@ virMediatedDeviceTypeFree; virMediatedDeviceTypeReadAttrs; =20 =20 +# util/virmodule.h +virModuleLoad; + =20 # util/virnetdev.h virNetDevAddMulti; diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index cb1bcc0944..7d226f3d3a 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -33,6 +33,7 @@ #include "virstoragefile.h" #include "storage_backend.h" #include "virlog.h" +#include "virmodule.h" #include "virfile.h" #include "configmake.h" =20 @@ -97,7 +98,7 @@ virStorageDriverLoadBackendModule(const char *name, "LIBVIRT_STORAGE_BACKEND_DIR")= )) return -1; =20 - ret =3D virDriverLoadModuleFull(modfile, regfunc, forceload); + ret =3D virModuleLoad(modfile, regfunc, forceload); =20 VIR_FREE(modfile); =20 diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am index 9624fb687c..ec8745da7e 100644 --- a/src/util/Makefile.inc.am +++ b/src/util/Makefile.inc.am @@ -100,6 +100,8 @@ UTIL_SOURCES =3D \ util/virmacaddr.h \ util/virmacmap.c \ util/virmacmap.h \ + util/virmodule.c \ + util/virmodule.h \ util/virnetdev.c \ util/virnetdev.h \ util/virnetdevbandwidth.c \ diff --git a/src/util/virmodule.c b/src/util/virmodule.c new file mode 100644 index 0000000000..ff8c22752e --- /dev/null +++ b/src/util/virmodule.c @@ -0,0 +1,163 @@ +/* + * virmodule.c: APIs for dlopen'ing extension modules + * + * Copyright (C) 2012-2018 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 "virmodule.h" +#include "virerror.h" +#include "virfile.h" +#include "virlog.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +VIR_LOG_INIT("util.module"); + +#ifdef HAVE_DLFCN_H +# include + +static void * +virModuleLoadFile(const char *file) +{ + void *handle =3D NULL; + int flags =3D RTLD_NOW | RTLD_GLOBAL; + +# ifdef RTLD_NODELETE + flags |=3D RTLD_NODELETE; +# endif + + VIR_DEBUG("Load module file '%s'", file); + + virUpdateSelfLastChanged(file); + + if (!(handle =3D dlopen(file, flags))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to load module '%s': %s"), file, dlerror(= )); + return NULL; + } + + return handle; +} + + +static void * +virModuleLoadFunc(void *handle, + const char *file, + const char *funcname) +{ + void *regsym; + + VIR_DEBUG("Lookup function '%s'", funcname); + + if (!(regsym =3D dlsym(handle, funcname))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to find symbol '%s' in module '%s': %s"), + funcname, file, dlerror()); + return NULL; + } + + return regsym; +} + + +/** + * virModuleLoad: + * @path: filename of module to load + * @regfunc: name of the function that registers the module + * @required: true if module must exist on disk, false to silently skip + * + * Loads a loadable module named @path and calls the + * registration function @regfunc. The module will never + * be unloaded because unloading is not safe in a multi-threaded + * application. + * + * The module is automatically looked up in the appropriate place (git or + * installed directory). + * + * Returns 0 on success, 1 if the module was not found and -1 on any error. + */ +int +virModuleLoad(const char *path, + const char *regfunc, + bool required) +{ + void *rethandle =3D NULL; + int (*regsym)(void); + int ret =3D -1; + + if (!virFileExists(path)) { + if (required) { + virReportSystemError(errno, + _("Failed to find module '%s'"), path); + return -1; + } else { + VIR_INFO("Module '%s' does not exist", path); + return 1; + } + } + + if (!(rethandle =3D virModuleLoadFile(path))) + goto cleanup; + + if (!(regsym =3D virModuleLoadFunc(rethandle, path, regfunc))) + goto cleanup; + + if ((*regsym)() < 0) { + /* regsym() should report an error itself, but lets + * just make sure */ + virErrorPtr err =3D virGetLastError(); + if (err =3D=3D NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to execute symbol '%s' in module '%s'= "), + regfunc, path); + } + goto cleanup; + } + + rethandle =3D NULL; + ret =3D 0; + + cleanup: + if (rethandle) + dlclose(rethandle); + return ret; +} + +#else /* ! HAVE_DLFCN_H */ +int +virModuleLoad(const char *path ATTRIBUTE_UNUSED, + const char *regfunc ATTRIBUTE_UNUSED, + bool required) +{ + VIR_DEBUG("dlopen not available on this platform"); + if (required) { + virReportSystemError(ENOSYS, + _("Failed to find module '%s': %s"), path); + return -1; + } else { + /* Since we have no dlopen(), but definition we have no + * loadable modules on disk, so we can resaonably + * return '1' instead of an error. + */ + return 1; + } +} +#endif /* ! HAVE_DLFCN_H */ diff --git a/src/util/virmodule.h b/src/util/virmodule.h new file mode 100644 index 0000000000..cccd836b41 --- /dev/null +++ b/src/util/virmodule.h @@ -0,0 +1,29 @@ +/* + * virmodule.h: APIs for dlopen'ing extension modules + * + * Copyright (C) 2012-2018 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_MODULE_H__ +# define __VIR_MODULE_H__ + +int virModuleLoad(const char *path, + const char *regfunc, + bool required); + +#endif /* __VIR_MODULE_H__ */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 23:34:43 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524671571216807.3207119552881; Wed, 25 Apr 2018 08:52:51 -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 8EB2FC04B94A; Wed, 25 Apr 2018 15:52:49 +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 08EDB6B8C3; Wed, 25 Apr 2018 15:52:49 +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 1061818033EB; Wed, 25 Apr 2018 15:52:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFqkBI026740 for ; Wed, 25 Apr 2018 11:52:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 87813202323A; Wed, 25 Apr 2018 15:52:46 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id CE2342023239; Wed, 25 Apr 2018 15:52:45 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 16:52:39 +0100 Message-Id: <20180425155243.23406-3-berrange@redhat.com> In-Reply-To: <20180425155243.23406-1-berrange@redhat.com> References: <20180425155243.23406-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 2/6] storage: split gluster storage file code from storage driver backend 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: , Content-Type: text/plain; charset="utf-8" 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.31]); Wed, 25 Apr 2018 15:52:50 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 The storage file code needs to be run in the hypervisor drivers, while the storage backend code needs to be run in the storage driver. Split the source code as a preparatory step for creating separate loadable modules. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/storage/Makefile.inc.am | 2 + src/storage/storage_backend_gluster.c | 305 +--------------------------- src/storage/storage_backend_gluster.h | 2 +- src/storage/storage_file_gluster.c | 366 ++++++++++++++++++++++++++++++= ++++ src/storage/storage_file_gluster.h | 27 +++ 5 files changed, 399 insertions(+), 303 deletions(-) create mode 100644 src/storage/storage_file_gluster.c create mode 100644 src/storage/storage_file_gluster.h diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am index ae9fdeb6a9..1e81249272 100644 --- a/src/storage/Makefile.inc.am +++ b/src/storage/Makefile.inc.am @@ -55,6 +55,8 @@ STORAGE_DRIVER_SHEEPDOG_SOURCES =3D \ STORAGE_DRIVER_GLUSTER_SOURCES =3D \ storage/storage_backend_gluster.h \ storage/storage_backend_gluster.c \ + storage/storage_file_gluster.h \ + storage/storage_file_gluster.c \ $(NULL) =20 STORAGE_DRIVER_ZFS_SOURCES =3D \ diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_ba= ckend_gluster.c index c6cc531e2f..aca772676c 100644 --- a/src/storage/storage_backend_gluster.c +++ b/src/storage/storage_backend_gluster.c @@ -1,7 +1,7 @@ /* * storage_backend_gluster.c: storage backend for Gluster handling * - * Copyright (C) 2013-2014 Red Hat, Inc. + * Copyright (C) 2013-2018 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 @@ -24,11 +24,11 @@ #include =20 #include "storage_backend_gluster.h" +#include "storage_file_gluster.h" #include "storage_conf.h" #include "viralloc.h" #include "virerror.h" #include "virlog.h" -#include "virstoragefilebackend.h" #include "virstring.h" #include "viruri.h" #include "storage_util.h" @@ -561,312 +561,13 @@ virStorageBackend virStorageBackendGluster =3D { }; =20 =20 -typedef struct _virStorageFileBackendGlusterPriv virStorageFileBackendGlus= terPriv; -typedef virStorageFileBackendGlusterPriv *virStorageFileBackendGlusterPriv= Ptr; - -struct _virStorageFileBackendGlusterPriv { - glfs_t *vol; - char *canonpath; -}; - - -static void -virStorageFileBackendGlusterDeinit(virStorageSourcePtr src) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - - VIR_DEBUG("deinitializing gluster storage file %p (gluster://%s:%u/%s%= s)", - src, src->hosts->name, src->hosts->port, src->volume, src->p= ath); - - if (priv->vol) - glfs_fini(priv->vol); - VIR_FREE(priv->canonpath); - - VIR_FREE(priv); - src->drv->priv =3D NULL; -} - -static int -virStorageFileBackendGlusterInitServer(virStorageFileBackendGlusterPrivPtr= priv, - virStorageNetHostDefPtr host) -{ - const char *transport =3D virStorageNetHostTransportTypeToString(host-= >transport); - const char *hoststr =3D NULL; - int port =3D 0; - - switch ((virStorageNetHostTransport) host->transport) { - case VIR_STORAGE_NET_HOST_TRANS_RDMA: - case VIR_STORAGE_NET_HOST_TRANS_TCP: - hoststr =3D host->name; - port =3D host->port; - break; - - case VIR_STORAGE_NET_HOST_TRANS_UNIX: - hoststr =3D host->socket; - break; - - case VIR_STORAGE_NET_HOST_TRANS_LAST: - break; - } - - VIR_DEBUG("adding gluster host for %p: transport=3D%s host=3D%s port= =3D%d", - priv, transport, hoststr, port); - - if (glfs_set_volfile_server(priv->vol, transport, hoststr, port) < 0) { - virReportSystemError(errno, - _("failed to set gluster volfile server '%s'"= ), - hoststr); - return -1; - } - - return 0; -} - - -static int -virStorageFileBackendGlusterInit(virStorageSourcePtr src) -{ - virStorageFileBackendGlusterPrivPtr priv =3D NULL; - size_t i; - - if (!src->volume) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("missing gluster volume name for path '%s'"), - src->path); - return -1; - } - - if (VIR_ALLOC(priv) < 0) - return -1; - - VIR_DEBUG("initializing gluster storage file %p " - "(priv=3D'%p' volume=3D'%s' path=3D'%s') as [%u:%u]", - src, priv, src->volume, src->path, - (unsigned int)src->drv->uid, (unsigned int)src->drv->gid); - - if (!(priv->vol =3D glfs_new(src->volume))) { - virReportOOMError(); - goto error; - } - - for (i =3D 0; i < src->nhosts; i++) { - if (virStorageFileBackendGlusterInitServer(priv, src->hosts + i) <= 0) - goto error; - } - - if (glfs_init(priv->vol) < 0) { - virReportSystemError(errno, - _("failed to initialize gluster connection " - "(src=3D%p priv=3D%p)"), src, priv); - goto error; - } - - src->drv->priv =3D priv; - - return 0; - - error: - if (priv->vol) - glfs_fini(priv->vol); - VIR_FREE(priv); - - return -1; -} - - -static int -virStorageFileBackendGlusterCreate(virStorageSourcePtr src) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - glfs_fd_t *fd =3D NULL; - mode_t mode =3D S_IRUSR; - - if (!src->readonly) - mode |=3D S_IWUSR; - - if (!(fd =3D glfs_creat(priv->vol, src->path, - O_CREAT | O_TRUNC | O_WRONLY, mode))) - return -1; - - ignore_value(glfs_close(fd)); - return 0; -} - - -static int -virStorageFileBackendGlusterUnlink(virStorageSourcePtr src) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - - return glfs_unlink(priv->vol, src->path); -} - - -static int -virStorageFileBackendGlusterStat(virStorageSourcePtr src, - struct stat *st) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - - return glfs_stat(priv->vol, src->path, st); -} - - -static ssize_t -virStorageFileBackendGlusterRead(virStorageSourcePtr src, - size_t offset, - size_t len, - char **buf) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - glfs_fd_t *fd =3D NULL; - ssize_t ret =3D -1; - - *buf =3D NULL; - - if (!(fd =3D glfs_open(priv->vol, src->path, O_RDONLY))) { - virReportSystemError(errno, _("Failed to open file '%s'"), - src->path); - return -1; - } - - if (offset > 0) { - if (glfs_lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { - virReportSystemError(errno, _("cannot seek into '%s'"), src->p= ath); - goto cleanup; - } - } - - ret =3D virStorageBackendGlusterRead(fd, src->path, len, buf); - - cleanup: - if (fd) - glfs_close(fd); - - return ret; -} - - -static int -virStorageFileBackendGlusterAccess(virStorageSourcePtr src, - int mode) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - - return glfs_access(priv->vol, src->path, mode); -} - -static int -virStorageFileBackendGlusterReadlinkCallback(const char *path, - char **linkpath, - void *data) -{ - virStorageFileBackendGlusterPrivPtr priv =3D data; - char *buf =3D NULL; - size_t bufsiz =3D 0; - ssize_t ret; - struct stat st; - - *linkpath =3D NULL; - - if (glfs_stat(priv->vol, path, &st) < 0) { - virReportSystemError(errno, - _("failed to stat gluster path '%s'"), - path); - return -1; - } - - if (!S_ISLNK(st.st_mode)) - return 1; - - realloc: - if (VIR_EXPAND_N(buf, bufsiz, 256) < 0) - goto error; - - if ((ret =3D glfs_readlink(priv->vol, path, buf, bufsiz)) < 0) { - virReportSystemError(errno, - _("failed to read link of gluster file '%s'"), - path); - goto error; - } - - if (ret =3D=3D bufsiz) - goto realloc; - - buf[ret] =3D '\0'; - - *linkpath =3D buf; - - return 0; - - error: - VIR_FREE(buf); - return -1; -} - - -static const char * -virStorageFileBackendGlusterGetUniqueIdentifier(virStorageSourcePtr src) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - char *filePath =3D NULL; - - if (priv->canonpath) - return priv->canonpath; - - if (!(filePath =3D virStorageFileCanonicalizePath(src->path, - virStorageFileBackendG= lusterReadlinkCallback, - priv))) - return NULL; - - ignore_value(virAsprintf(&priv->canonpath, "gluster://%s:%u/%s/%s", - src->hosts->name, - src->hosts->port, - src->volume, - filePath)); - - VIR_FREE(filePath); - - return priv->canonpath; -} - - -static int -virStorageFileBackendGlusterChown(const virStorageSource *src, - uid_t uid, - gid_t gid) -{ - virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; - - return glfs_chown(priv->vol, src->path, uid, gid); -} - - -virStorageFileBackend virStorageFileBackendGluster =3D { - .type =3D VIR_STORAGE_TYPE_NETWORK, - .protocol =3D VIR_STORAGE_NET_PROTOCOL_GLUSTER, - - .backendInit =3D virStorageFileBackendGlusterInit, - .backendDeinit =3D virStorageFileBackendGlusterDeinit, - - .storageFileCreate =3D virStorageFileBackendGlusterCreate, - .storageFileUnlink =3D virStorageFileBackendGlusterUnlink, - .storageFileStat =3D virStorageFileBackendGlusterStat, - .storageFileRead =3D virStorageFileBackendGlusterRead, - .storageFileAccess =3D virStorageFileBackendGlusterAccess, - .storageFileChown =3D virStorageFileBackendGlusterChown, - - .storageFileGetUniqueIdentifier =3D virStorageFileBackendGlusterGetUni= queIdentifier, -}; - - int virStorageBackendGlusterRegister(void) { if (virStorageBackendRegister(&virStorageBackendGluster) < 0) return -1; =20 - if (virStorageFileBackendRegister(&virStorageFileBackendGluster) < 0) + if (virStorageFileGlusterRegister() < 0) return -1; =20 return 0; diff --git a/src/storage/storage_backend_gluster.h b/src/storage/storage_ba= ckend_gluster.h index 91b8d8275d..12a1c04f8d 100644 --- a/src/storage/storage_backend_gluster.h +++ b/src/storage/storage_backend_gluster.h @@ -1,7 +1,7 @@ /* * storage_backend_gluster.h: storage backend for Gluster handling * - * Copyright (C) 2013 Red Hat, Inc. + * Copyright (C) 2013-2018 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 diff --git a/src/storage/storage_file_gluster.c b/src/storage/storage_file_= gluster.c new file mode 100644 index 0000000000..f8bbde8cfe --- /dev/null +++ b/src/storage/storage_file_gluster.c @@ -0,0 +1,366 @@ +/* + * storage_file_gluster.c: storage file backend for Gluster handling + * + * Copyright (C) 2013-2018 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 + +#include "storage_file_gluster.h" +#include "viralloc.h" +#include "virerror.h" +#include "virlog.h" +#include "virstoragefilebackend.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_STORAGE + +VIR_LOG_INIT("storage.storage_file_gluster"); + + +typedef struct _virStorageFileBackendGlusterPriv virStorageFileBackendGlus= terPriv; +typedef virStorageFileBackendGlusterPriv *virStorageFileBackendGlusterPriv= Ptr; + +struct _virStorageFileBackendGlusterPriv { + glfs_t *vol; + char *canonpath; +}; + +static void +virStorageFileBackendGlusterDeinit(virStorageSourcePtr src) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + + VIR_DEBUG("deinitializing gluster storage file %p (gluster://%s:%u/%s%= s)", + src, src->hosts->name, src->hosts->port, src->volume, src->p= ath); + + if (priv->vol) + glfs_fini(priv->vol); + VIR_FREE(priv->canonpath); + + VIR_FREE(priv); + src->drv->priv =3D NULL; +} + +static int +virStorageFileBackendGlusterInitServer(virStorageFileBackendGlusterPrivPtr= priv, + virStorageNetHostDefPtr host) +{ + const char *transport =3D virStorageNetHostTransportTypeToString(host-= >transport); + const char *hoststr =3D NULL; + int port =3D 0; + + switch ((virStorageNetHostTransport) host->transport) { + case VIR_STORAGE_NET_HOST_TRANS_RDMA: + case VIR_STORAGE_NET_HOST_TRANS_TCP: + hoststr =3D host->name; + port =3D host->port; + break; + + case VIR_STORAGE_NET_HOST_TRANS_UNIX: + hoststr =3D host->socket; + break; + + case VIR_STORAGE_NET_HOST_TRANS_LAST: + break; + } + + VIR_DEBUG("adding gluster host for %p: transport=3D%s host=3D%s port= =3D%d", + priv, transport, hoststr, port); + + if (glfs_set_volfile_server(priv->vol, transport, hoststr, port) < 0) { + virReportSystemError(errno, + _("failed to set gluster volfile server '%s'"= ), + hoststr); + return -1; + } + + return 0; +} + + +static int +virStorageFileBackendGlusterInit(virStorageSourcePtr src) +{ + virStorageFileBackendGlusterPrivPtr priv =3D NULL; + size_t i; + + if (!src->volume) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("missing gluster volume name for path '%s'"), + src->path); + return -1; + } + + if (VIR_ALLOC(priv) < 0) + return -1; + + VIR_DEBUG("initializing gluster storage file %p " + "(priv=3D'%p' volume=3D'%s' path=3D'%s') as [%u:%u]", + src, priv, src->volume, src->path, + (unsigned int)src->drv->uid, (unsigned int)src->drv->gid); + + if (!(priv->vol =3D glfs_new(src->volume))) { + virReportOOMError(); + goto error; + } + + for (i =3D 0; i < src->nhosts; i++) { + if (virStorageFileBackendGlusterInitServer(priv, src->hosts + i) <= 0) + goto error; + } + + if (glfs_init(priv->vol) < 0) { + virReportSystemError(errno, + _("failed to initialize gluster connection " + "(src=3D%p priv=3D%p)"), src, priv); + goto error; + } + + src->drv->priv =3D priv; + + return 0; + + error: + if (priv->vol) + glfs_fini(priv->vol); + VIR_FREE(priv); + + return -1; +} + + +static int +virStorageFileBackendGlusterCreate(virStorageSourcePtr src) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + glfs_fd_t *fd =3D NULL; + mode_t mode =3D S_IRUSR; + + if (!src->readonly) + mode |=3D S_IWUSR; + + if (!(fd =3D glfs_creat(priv->vol, src->path, + O_CREAT | O_TRUNC | O_WRONLY, mode))) + return -1; + + ignore_value(glfs_close(fd)); + return 0; +} + + +static int +virStorageFileBackendGlusterUnlink(virStorageSourcePtr src) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + + return glfs_unlink(priv->vol, src->path); +} + + +static int +virStorageFileBackendGlusterStat(virStorageSourcePtr src, + struct stat *st) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + + return glfs_stat(priv->vol, src->path, st); +} + + +static ssize_t +virStorageFileBackendGlusterRead(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + glfs_fd_t *fd =3D NULL; + ssize_t ret =3D -1; + char *s; + size_t nread =3D 0; + + *buf =3D NULL; + + if (!(fd =3D glfs_open(priv->vol, src->path, O_RDONLY))) { + virReportSystemError(errno, _("Failed to open file '%s'"), + src->path); + return -1; + } + + if (offset > 0) { + if (glfs_lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { + virReportSystemError(errno, _("cannot seek into '%s'"), src->p= ath); + goto cleanup; + } + } + + + if (VIR_ALLOC_N(*buf, len) < 0) + return -1; + + s =3D *buf; + while (len) { + ssize_t r =3D glfs_read(fd, s, len, 0); + if (r < 0 && errno =3D=3D EINTR) + continue; + if (r < 0) { + VIR_FREE(*buf); + virReportSystemError(errno, _("unable to read '%s'"), src->pat= h); + return r; + } + if (r =3D=3D 0) + return nread; + s +=3D r; + len -=3D r; + nread +=3D r; + } + + ret =3D nread; + + cleanup: + if (fd) + glfs_close(fd); + + return ret; +} + + +static int +virStorageFileBackendGlusterAccess(virStorageSourcePtr src, + int mode) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + + return glfs_access(priv->vol, src->path, mode); +} + +static int +virStorageFileBackendGlusterReadlinkCallback(const char *path, + char **linkpath, + void *data) +{ + virStorageFileBackendGlusterPrivPtr priv =3D data; + char *buf =3D NULL; + size_t bufsiz =3D 0; + ssize_t ret; + struct stat st; + + *linkpath =3D NULL; + + if (glfs_stat(priv->vol, path, &st) < 0) { + virReportSystemError(errno, + _("failed to stat gluster path '%s'"), + path); + return -1; + } + + if (!S_ISLNK(st.st_mode)) + return 1; + + realloc: + if (VIR_EXPAND_N(buf, bufsiz, 256) < 0) + goto error; + + if ((ret =3D glfs_readlink(priv->vol, path, buf, bufsiz)) < 0) { + virReportSystemError(errno, + _("failed to read link of gluster file '%s'"), + path); + goto error; + } + + if (ret =3D=3D bufsiz) + goto realloc; + + buf[ret] =3D '\0'; + + *linkpath =3D buf; + + return 0; + + error: + VIR_FREE(buf); + return -1; +} + + +static const char * +virStorageFileBackendGlusterGetUniqueIdentifier(virStorageSourcePtr src) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + char *filePath =3D NULL; + + if (priv->canonpath) + return priv->canonpath; + + if (!(filePath =3D virStorageFileCanonicalizePath(src->path, + virStorageFileBackendG= lusterReadlinkCallback, + priv))) + return NULL; + + ignore_value(virAsprintf(&priv->canonpath, "gluster://%s:%u/%s/%s", + src->hosts->name, + src->hosts->port, + src->volume, + filePath)); + + VIR_FREE(filePath); + + return priv->canonpath; +} + + +static int +virStorageFileBackendGlusterChown(const virStorageSource *src, + uid_t uid, + gid_t gid) +{ + virStorageFileBackendGlusterPrivPtr priv =3D src->drv->priv; + + return glfs_chown(priv->vol, src->path, uid, gid); +} + + +virStorageFileBackend virStorageFileBackendGluster =3D { + .type =3D VIR_STORAGE_TYPE_NETWORK, + .protocol =3D VIR_STORAGE_NET_PROTOCOL_GLUSTER, + + .backendInit =3D virStorageFileBackendGlusterInit, + .backendDeinit =3D virStorageFileBackendGlusterDeinit, + + .storageFileCreate =3D virStorageFileBackendGlusterCreate, + .storageFileUnlink =3D virStorageFileBackendGlusterUnlink, + .storageFileStat =3D virStorageFileBackendGlusterStat, + .storageFileRead =3D virStorageFileBackendGlusterRead, + .storageFileAccess =3D virStorageFileBackendGlusterAccess, + .storageFileChown =3D virStorageFileBackendGlusterChown, + + .storageFileGetUniqueIdentifier =3D virStorageFileBackendGlusterGetUni= queIdentifier, +}; + + +int +virStorageFileGlusterRegister(void) +{ + if (virStorageFileBackendRegister(&virStorageFileBackendGluster) < 0) + return -1; + + return 0; +} diff --git a/src/storage/storage_file_gluster.h b/src/storage/storage_file_= gluster.h new file mode 100644 index 0000000000..572254aedb --- /dev/null +++ b/src/storage/storage_file_gluster.h @@ -0,0 +1,27 @@ +/* + * storage_file_gluster.h: storage file backend for Gluster handling + * + * Copyright (C) 2013-2018 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_STORAGE_FILE_GLUSTER_H__ +# define __VIR_STORAGE_FILE_GLUSTER_H__ + +int virStorageFileGlusterRegister(void); + +#endif /* __VIR_STORAGE_FILE_GLUSTER_H__ */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 23:34:43 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524671576973941.1830379507714; Wed, 25 Apr 2018 08:52:56 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3834C3003A27; Wed, 25 Apr 2018 15:52:55 +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 B311B1001642; Wed, 25 Apr 2018 15:52:54 +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 9CDAD180596F; Wed, 25 Apr 2018 15:52:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFql9I026747 for ; Wed, 25 Apr 2018 11:52:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5D34A202323C; Wed, 25 Apr 2018 15:52:47 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id C04502023239; Wed, 25 Apr 2018 15:52:46 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 16:52:40 +0100 Message-Id: <20180425155243.23406-4-berrange@redhat.com> In-Reply-To: <20180425155243.23406-1-berrange@redhat.com> References: <20180425155243.23406-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 3/6] storage: split fs storage file code from storage driver backend 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: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Wed, 25 Apr 2018 15:52:56 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 The storage file code needs to be run in the hypervisor drivers, while the storage backend code needs to be run in the storage driver. Split the source code as a preparatory step for creating separate loadable modules. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/storage/Makefile.inc.am | 2 + src/storage/storage_backend_fs.c | 210 +------------------------------- src/storage/storage_file_fs.c | 251 +++++++++++++++++++++++++++++++++++= ++++ src/storage/storage_file_fs.h | 29 +++++ 4 files changed, 285 insertions(+), 207 deletions(-) create mode 100644 src/storage/storage_file_fs.c create mode 100644 src/storage/storage_file_fs.h diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am index 1e81249272..af2c97ab93 100644 --- a/src/storage/Makefile.inc.am +++ b/src/storage/Makefile.inc.am @@ -14,6 +14,8 @@ STORAGE_DRIVER_SOURCES =3D \ STORAGE_DRIVER_FS_SOURCES =3D \ storage/storage_backend_fs.h \ storage/storage_backend_fs.c \ + storage/storage_file_fs.h \ + storage/storage_file_fs.c \ $(NULL) =20 STORAGE_DRIVER_LVM_SOURCES =3D \ diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend= _fs.c index 9b0fcf92c5..bface86b43 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -1,7 +1,7 @@ /* * storage_backend_fs.c: storage backend for FS and directory handling * - * Copyright (C) 2007-2015 Red Hat, Inc. + * Copyright (C) 2007-2018 Red Hat, Inc. * Copyright (C) 2007-2008 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -28,21 +28,15 @@ #include #include #include -#include #include =20 -#include -#include -#include - #include "virerror.h" #include "storage_backend_fs.h" +#include "storage_file_fs.h" #include "storage_util.h" #include "storage_conf.h" -#include "virstoragefilebackend.h" #include "vircommand.h" #include "viralloc.h" -#include "virxml.h" #include "virfile.h" #include "virlog.h" #include "virstring.h" @@ -705,198 +699,6 @@ virStorageBackend virStorageBackendNetFileSystem =3D { #endif /* WITH_STORAGE_FS */ =20 =20 -typedef struct _virStorageFileBackendFsPriv virStorageFileBackendFsPriv; -typedef virStorageFileBackendFsPriv *virStorageFileBackendFsPrivPtr; - -struct _virStorageFileBackendFsPriv { - char *canonpath; /* unique file identifier (canonical path) */ -}; - - -static void -virStorageFileBackendFileDeinit(virStorageSourcePtr src) -{ - VIR_DEBUG("deinitializing FS storage file %p (%s:%s)", src, - virStorageTypeToString(virStorageSourceGetActualType(src)), - src->path); - - virStorageFileBackendFsPrivPtr priv =3D src->drv->priv; - - VIR_FREE(priv->canonpath); - VIR_FREE(priv); -} - - -static int -virStorageFileBackendFileInit(virStorageSourcePtr src) -{ - virStorageFileBackendFsPrivPtr priv =3D NULL; - - VIR_DEBUG("initializing FS storage file %p (%s:%s)[%u:%u]", src, - virStorageTypeToString(virStorageSourceGetActualType(src)), - src->path, - (unsigned int)src->drv->uid, (unsigned int)src->drv->gid); - - if (VIR_ALLOC(priv) < 0) - return -1; - - src->drv->priv =3D priv; - - return 0; -} - - -static int -virStorageFileBackendFileCreate(virStorageSourcePtr src) -{ - int fd =3D -1; - mode_t mode =3D S_IRUSR; - - if (!src->readonly) - mode |=3D S_IWUSR; - - if ((fd =3D virFileOpenAs(src->path, O_WRONLY | O_TRUNC | O_CREAT, mod= e, - src->drv->uid, src->drv->gid, 0)) < 0) { - errno =3D -fd; - return -1; - } - - VIR_FORCE_CLOSE(fd); - return 0; -} - - -static int -virStorageFileBackendFileUnlink(virStorageSourcePtr src) -{ - return unlink(src->path); -} - - -static int -virStorageFileBackendFileStat(virStorageSourcePtr src, - struct stat *st) -{ - return stat(src->path, st); -} - - -static ssize_t -virStorageFileBackendFileRead(virStorageSourcePtr src, - size_t offset, - size_t len, - char **buf) -{ - int fd =3D -1; - ssize_t ret =3D -1; - - if ((fd =3D virFileOpenAs(src->path, O_RDONLY, 0, - src->drv->uid, src->drv->gid, 0)) < 0) { - virReportSystemError(-fd, _("Failed to open file '%s'"), - src->path); - return -1; - } - - if (offset > 0) { - if (lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { - virReportSystemError(errno, _("cannot seek into '%s'"), src->p= ath); - goto cleanup; - } - } - - if ((ret =3D virFileReadHeaderFD(fd, len, buf)) < 0) { - virReportSystemError(errno, - _("cannot read header '%s'"), src->path); - goto cleanup; - } - - cleanup: - VIR_FORCE_CLOSE(fd); - - return ret; -} - - -static const char * -virStorageFileBackendFileGetUniqueIdentifier(virStorageSourcePtr src) -{ - virStorageFileBackendFsPrivPtr priv =3D src->drv->priv; - - if (!priv->canonpath) { - if (!(priv->canonpath =3D canonicalize_file_name(src->path))) { - virReportSystemError(errno, _("can't canonicalize path '%s'"), - src->path); - return NULL; - } - } - - return priv->canonpath; -} - - -static int -virStorageFileBackendFileAccess(virStorageSourcePtr src, - int mode) -{ - return virFileAccessibleAs(src->path, mode, - src->drv->uid, src->drv->gid); -} - - -static int -virStorageFileBackendFileChown(const virStorageSource *src, - uid_t uid, - gid_t gid) -{ - return chown(src->path, uid, gid); -} - - -virStorageFileBackend virStorageFileBackendFile =3D { - .type =3D VIR_STORAGE_TYPE_FILE, - - .backendInit =3D virStorageFileBackendFileInit, - .backendDeinit =3D virStorageFileBackendFileDeinit, - - .storageFileCreate =3D virStorageFileBackendFileCreate, - .storageFileUnlink =3D virStorageFileBackendFileUnlink, - .storageFileStat =3D virStorageFileBackendFileStat, - .storageFileRead =3D virStorageFileBackendFileRead, - .storageFileAccess =3D virStorageFileBackendFileAccess, - .storageFileChown =3D virStorageFileBackendFileChown, - - .storageFileGetUniqueIdentifier =3D virStorageFileBackendFileGetUnique= Identifier, -}; - - -virStorageFileBackend virStorageFileBackendBlock =3D { - .type =3D VIR_STORAGE_TYPE_BLOCK, - - .backendInit =3D virStorageFileBackendFileInit, - .backendDeinit =3D virStorageFileBackendFileDeinit, - - .storageFileStat =3D virStorageFileBackendFileStat, - .storageFileRead =3D virStorageFileBackendFileRead, - .storageFileAccess =3D virStorageFileBackendFileAccess, - .storageFileChown =3D virStorageFileBackendFileChown, - - .storageFileGetUniqueIdentifier =3D virStorageFileBackendFileGetUnique= Identifier, -}; - - -virStorageFileBackend virStorageFileBackendDir =3D { - .type =3D VIR_STORAGE_TYPE_DIR, - - .backendInit =3D virStorageFileBackendFileInit, - .backendDeinit =3D virStorageFileBackendFileDeinit, - - .storageFileAccess =3D virStorageFileBackendFileAccess, - .storageFileChown =3D virStorageFileBackendFileChown, - - .storageFileGetUniqueIdentifier =3D virStorageFileBackendFileGetUnique= Identifier, -}; - - int virStorageBackendFsRegister(void) { @@ -911,13 +713,7 @@ virStorageBackendFsRegister(void) return -1; #endif /* WITH_STORAGE_FS */ =20 - if (virStorageFileBackendRegister(&virStorageFileBackendFile) < 0) - return -1; - - if (virStorageFileBackendRegister(&virStorageFileBackendBlock) < 0) - return -1; - - if (virStorageFileBackendRegister(&virStorageFileBackendDir) < 0) + if (virStorageFileFsRegister() < 0) return -1; =20 return 0; diff --git a/src/storage/storage_file_fs.c b/src/storage/storage_file_fs.c new file mode 100644 index 0000000000..c8d87514eb --- /dev/null +++ b/src/storage/storage_file_fs.c @@ -0,0 +1,251 @@ +/* + * storage_file_fs.c: storage file code for FS and directory handling + * + * Copyright (C) 2007-2018 Red Hat, Inc. + * Copyright (C) 2007-2008 Daniel P. Berrange + * + * 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 + * . + * + * Author: Daniel P. Berrange + */ + +#include + +#include +#include +#include +#include + +#include "virerror.h" +#include "storage_file_fs.h" +#include "storage_util.h" +#include "virstoragefilebackend.h" +#include "vircommand.h" +#include "viralloc.h" +#include "virfile.h" +#include "virlog.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_STORAGE + +VIR_LOG_INIT("storage.storage_backend_fs"); + + +typedef struct _virStorageFileBackendFsPriv virStorageFileBackendFsPriv; +typedef virStorageFileBackendFsPriv *virStorageFileBackendFsPrivPtr; + +struct _virStorageFileBackendFsPriv { + char *canonpath; /* unique file identifier (canonical path) */ +}; + + +static void +virStorageFileBackendFileDeinit(virStorageSourcePtr src) +{ + VIR_DEBUG("deinitializing FS storage file %p (%s:%s)", src, + virStorageTypeToString(virStorageSourceGetActualType(src)), + src->path); + + virStorageFileBackendFsPrivPtr priv =3D src->drv->priv; + + VIR_FREE(priv->canonpath); + VIR_FREE(priv); +} + + +static int +virStorageFileBackendFileInit(virStorageSourcePtr src) +{ + virStorageFileBackendFsPrivPtr priv =3D NULL; + + VIR_DEBUG("initializing FS storage file %p (%s:%s)[%u:%u]", src, + virStorageTypeToString(virStorageSourceGetActualType(src)), + src->path, + (unsigned int)src->drv->uid, (unsigned int)src->drv->gid); + + if (VIR_ALLOC(priv) < 0) + return -1; + + src->drv->priv =3D priv; + + return 0; +} + + +static int +virStorageFileBackendFileCreate(virStorageSourcePtr src) +{ + int fd =3D -1; + mode_t mode =3D S_IRUSR; + + if (!src->readonly) + mode |=3D S_IWUSR; + + if ((fd =3D virFileOpenAs(src->path, O_WRONLY | O_TRUNC | O_CREAT, mod= e, + src->drv->uid, src->drv->gid, 0)) < 0) { + errno =3D -fd; + return -1; + } + + VIR_FORCE_CLOSE(fd); + return 0; +} + + +static int +virStorageFileBackendFileUnlink(virStorageSourcePtr src) +{ + return unlink(src->path); +} + + +static int +virStorageFileBackendFileStat(virStorageSourcePtr src, + struct stat *st) +{ + return stat(src->path, st); +} + + +static ssize_t +virStorageFileBackendFileRead(virStorageSourcePtr src, + size_t offset, + size_t len, + char **buf) +{ + int fd =3D -1; + ssize_t ret =3D -1; + + if ((fd =3D virFileOpenAs(src->path, O_RDONLY, 0, + src->drv->uid, src->drv->gid, 0)) < 0) { + virReportSystemError(-fd, _("Failed to open file '%s'"), + src->path); + return -1; + } + + if (offset > 0) { + if (lseek(fd, offset, SEEK_SET) =3D=3D (off_t) -1) { + virReportSystemError(errno, _("cannot seek into '%s'"), src->p= ath); + goto cleanup; + } + } + + if ((ret =3D virFileReadHeaderFD(fd, len, buf)) < 0) { + virReportSystemError(errno, + _("cannot read header '%s'"), src->path); + goto cleanup; + } + + cleanup: + VIR_FORCE_CLOSE(fd); + + return ret; +} + + +static const char * +virStorageFileBackendFileGetUniqueIdentifier(virStorageSourcePtr src) +{ + virStorageFileBackendFsPrivPtr priv =3D src->drv->priv; + + if (!priv->canonpath) { + if (!(priv->canonpath =3D canonicalize_file_name(src->path))) { + virReportSystemError(errno, _("can't canonicalize path '%s'"), + src->path); + return NULL; + } + } + + return priv->canonpath; +} + + +static int +virStorageFileBackendFileAccess(virStorageSourcePtr src, + int mode) +{ + return virFileAccessibleAs(src->path, mode, + src->drv->uid, src->drv->gid); +} + + +static int +virStorageFileBackendFileChown(const virStorageSource *src, + uid_t uid, + gid_t gid) +{ + return chown(src->path, uid, gid); +} + + +virStorageFileBackend virStorageFileBackendFile =3D { + .type =3D VIR_STORAGE_TYPE_FILE, + + .backendInit =3D virStorageFileBackendFileInit, + .backendDeinit =3D virStorageFileBackendFileDeinit, + + .storageFileCreate =3D virStorageFileBackendFileCreate, + .storageFileUnlink =3D virStorageFileBackendFileUnlink, + .storageFileStat =3D virStorageFileBackendFileStat, + .storageFileRead =3D virStorageFileBackendFileRead, + .storageFileAccess =3D virStorageFileBackendFileAccess, + .storageFileChown =3D virStorageFileBackendFileChown, + + .storageFileGetUniqueIdentifier =3D virStorageFileBackendFileGetUnique= Identifier, +}; + + +virStorageFileBackend virStorageFileBackendBlock =3D { + .type =3D VIR_STORAGE_TYPE_BLOCK, + + .backendInit =3D virStorageFileBackendFileInit, + .backendDeinit =3D virStorageFileBackendFileDeinit, + + .storageFileStat =3D virStorageFileBackendFileStat, + .storageFileRead =3D virStorageFileBackendFileRead, + .storageFileAccess =3D virStorageFileBackendFileAccess, + .storageFileChown =3D virStorageFileBackendFileChown, + + .storageFileGetUniqueIdentifier =3D virStorageFileBackendFileGetUnique= Identifier, +}; + + +virStorageFileBackend virStorageFileBackendDir =3D { + .type =3D VIR_STORAGE_TYPE_DIR, + + .backendInit =3D virStorageFileBackendFileInit, + .backendDeinit =3D virStorageFileBackendFileDeinit, + + .storageFileAccess =3D virStorageFileBackendFileAccess, + .storageFileChown =3D virStorageFileBackendFileChown, + + .storageFileGetUniqueIdentifier =3D virStorageFileBackendFileGetUnique= Identifier, +}; + + +int +virStorageFileFsRegister(void) +{ + if (virStorageFileBackendRegister(&virStorageFileBackendFile) < 0) + return -1; + + if (virStorageFileBackendRegister(&virStorageFileBackendBlock) < 0) + return -1; + + if (virStorageFileBackendRegister(&virStorageFileBackendDir) < 0) + return -1; + + return 0; +} diff --git a/src/storage/storage_file_fs.h b/src/storage/storage_file_fs.h new file mode 100644 index 0000000000..c5d748c64d --- /dev/null +++ b/src/storage/storage_file_fs.h @@ -0,0 +1,29 @@ +/* + * storage_file_fs.h: storage file code for FS and directory handling + * + * Copyright (C) 2007-2018 Red Hat, Inc. + * Copyright (C) 2007-2008 Daniel P. Berrange + * + * 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 + * . + * + * Author: Daniel P. Berrange + */ + +#ifndef __VIR_STORAGE_FILE_FS_H__ +# define __VIR_STORAGE_FILE_FS_H__ + +int virStorageFileFsRegister(void); + +#endif /* __VIR_STORAGE_FILE_FS_H__ */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 23:34:43 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524671590460631.1124530976401; Wed, 25 Apr 2018 08:53:10 -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 A66AB356DA; Wed, 25 Apr 2018 15:53:08 +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 D44665E697; Wed, 25 Apr 2018 15:53:07 +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 A73544CA9F; Wed, 25 Apr 2018 15:53:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFqm5e026753 for ; Wed, 25 Apr 2018 11:52:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2FDA1202323B; Wed, 25 Apr 2018 15:52:48 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9498D2023239; Wed, 25 Apr 2018 15:52:47 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 16:52:41 +0100 Message-Id: <20180425155243.23406-5-berrange@redhat.com> In-Reply-To: <20180425155243.23406-1-berrange@redhat.com> References: <20180425155243.23406-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 4/6] storage: fix virStorageFileGetBackingStoreStr error handling 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: , Content-Type: text/plain; charset="utf-8" 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]); Wed, 25 Apr 2018 15:53:09 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 The virStorageFileGetBackingStoreStr method has overloaded the NULL return value to indicate both no backing available and a fatal error dealing with it. The caller is thus not able to correctly propagate the error messages. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/qemu/qemu_driver.c | 15 +++++++++------ src/util/virstoragefile.c | 49 +++++++++++++++++++++++++++++++------------= ---- src/util/virstoragefile.h | 5 +++-- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 7484b00e23..672c5372eb 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -14608,12 +14608,15 @@ qemuDomainSnapshotDiskDataCollect(virQEMUDriverPt= r driver, =20 /* relative backing store paths need to be updated so that relative * block commit still works */ - if (reuse && - (backingStoreStr =3D virStorageFileGetBackingStoreStr(dd->src)= )) { - if (virStorageIsRelative(backingStoreStr)) - VIR_STEAL_PTR(dd->relPath, backingStoreStr); - else - VIR_FREE(backingStoreStr); + if (reuse) { + if (virStorageFileGetBackingStoreStr(dd->src, &backingStoreStr= ) < 0) + goto error; + if (backingStoreStr !=3D NULL) { + if (virStorageIsRelative(backingStoreStr)) + VIR_STEAL_PTR(dd->relPath, backingStoreStr); + else + VIR_FREE(backingStoreStr); + } } =20 /* Note that it's unsafe to assume that the disks in the persistent diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 531540ac91..f09035cd4a 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -4357,14 +4357,8 @@ virStorageFileRead(virStorageSourcePtr src, return -1; } =20 - if (!src->drv->backend->storageFileRead) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("storage file reading is not supported for " - "storage type %s (protocol: %s)"), - virStorageTypeToString(src->type), - virStorageNetProtocolTypeToString(src->protocol)); + if (!src->drv->backend->storageFileRead) return -2; - } =20 ret =3D src->drv->backend->storageFileRead(src, offset, len, buf); =20 @@ -4551,8 +4545,15 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr= src, goto cleanup; =20 if ((headerLen =3D virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER, - &buf)) < 0) + &buf)) < 0) { + if (headerLen =3D=3D -2) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("storage file reading is not supported for " + "storage type %s (protocol: %s)"), + virStorageTypeToString(src->type), + virStorageNetProtocolTypeToString(src->protocol= )); goto cleanup; + } =20 if (virStorageFileGetMetadataInternal(src, buf, headerLen, &backingFormat) < 0) @@ -4664,24 +4665,36 @@ virStorageFileGetMetadata(virStorageSourcePtr src, * In case when the string can't be retrieved or does not exist NULL is * returned. */ -char * -virStorageFileGetBackingStoreStr(virStorageSourcePtr src) +int +virStorageFileGetBackingStoreStr(virStorageSourcePtr src, + char **backing) { virStorageSourcePtr tmp =3D NULL; char *buf =3D NULL; ssize_t headerLen; - char *ret =3D NULL; + int ret =3D -1; + int rv; + + *backing =3D NULL; =20 /* exit if we can't load information about the current image */ if (!virStorageFileSupportsBackingChainTraversal(src)) - return NULL; + return 0; =20 - if (virStorageFileAccess(src, F_OK) < 0) - return NULL; + rv =3D virStorageFileAccess(src, F_OK); + if (rv =3D=3D -2) + return 0; + if (rv < 0) { + virStorageFileReportBrokenChain(errno, src, src); + return -1; + } =20 if ((headerLen =3D virStorageFileRead(src, 0, VIR_STORAGE_MAX_HEADER, - &buf)) < 0) - return NULL; + &buf)) < 0) { + if (headerLen =3D=3D -2) + return 0; + return -1; + } =20 if (!(tmp =3D virStorageSourceCopy(src, false))) goto cleanup; @@ -4689,7 +4702,9 @@ virStorageFileGetBackingStoreStr(virStorageSourcePtr = src) if (virStorageFileGetMetadataInternal(tmp, buf, headerLen, NULL) < 0) goto cleanup; =20 - VIR_STEAL_PTR(ret, tmp->backingStoreRaw); + VIR_STEAL_PTR(*backing, tmp->backingStoreRaw); + + ret =3D 0; =20 cleanup: VIR_FREE(buf); diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index d129e81978..b92c1c47dd 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -474,8 +474,9 @@ int virStorageFileGetMetadata(virStorageSourcePtr src, bool report_broken) ATTRIBUTE_NONNULL(1); =20 -char *virStorageFileGetBackingStoreStr(virStorageSourcePtr src) - ATTRIBUTE_NONNULL(1); +int virStorageFileGetBackingStoreStr(virStorageSourcePtr src, + char **backing) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 void virStorageFileReportBrokenChain(int errcode, virStorageSourcePtr src, --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 23:34:43 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524671861975315.6412566092298; Wed, 25 Apr 2018 08:57:41 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B7B30804E3; Wed, 25 Apr 2018 15:57:38 +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 0F4DC7A236; Wed, 25 Apr 2018 15:57:38 +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 A76284CAA0; Wed, 25 Apr 2018 15:57:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFqnfr026772 for ; Wed, 25 Apr 2018 11:52:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0356E202323C; Wed, 25 Apr 2018 15:52:49 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id 684192023239; Wed, 25 Apr 2018 15:52:48 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 16:52:42 +0100 Message-Id: <20180425155243.23406-6-berrange@redhat.com> In-Reply-To: <20180425155243.23406-1-berrange@redhat.com> References: <20180425155243.23406-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 5/6] util: refactor storage file checks to allow error reporting 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: , Content-Type: text/plain; charset="utf-8" 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 25 Apr 2018 15:57:40 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 The virStorageFileSupportsSecurityDriver and virStorageFileSupportsAccess currently just return a boolean value. This is ok because they don't have any failure scenarios but a subsequent patch is going to introduce potential failure scenario. This changes their return type from a boolean to an int with values -1, 0, 1. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/qemu/qemu_domain.c | 21 +++++++++------ src/qemu/qemu_driver.c | 6 +++-- src/util/virstoragefile.c | 66 +++++++++++++++++++++++++++++++------------= ---- src/util/virstoragefile.h | 4 +-- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 326c939c85..542e20c5e4 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -7514,19 +7514,24 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr drive= r, =20 /* skip to the end of the chain if there is any */ while (virStorageSourceHasBacking(src)) { - if (report_broken && - virStorageFileSupportsAccess(src)) { + if (report_broken) { + int rv =3D virStorageFileSupportsAccess(src); =20 - if (qemuDomainStorageFileInit(driver, vm, src, disk->src) < 0) + if (rv < 0) goto cleanup; =20 - if (virStorageFileAccess(src, F_OK) < 0) { - virStorageFileReportBrokenChain(errno, src, disk->src); + if (rv > 0) { + if (qemuDomainStorageFileInit(driver, vm, src, disk->src) = < 0) + goto cleanup; + + if (virStorageFileAccess(src, F_OK) < 0) { + virStorageFileReportBrokenChain(errno, src, disk->src); + virStorageFileDeinit(src); + goto cleanup; + } + virStorageFileDeinit(src); - goto cleanup; } - - virStorageFileDeinit(src); } src =3D src->backingStore; } diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 672c5372eb..168a7c9ff3 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -308,9 +308,11 @@ qemuSecurityChownCallback(const virStorageSource *src, struct stat sb; int save_errno =3D 0; int ret =3D -1; + int rv; =20 - if (!virStorageFileSupportsSecurityDriver(src)) - return 0; + rv =3D virStorageFileSupportsSecurityDriver(src); + if (rv <=3D 0) + return rv; =20 if (virStorageSourceIsLocalStorage(src)) { /* use direct chmod for local files so that the file doesn't diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index f09035cd4a..da13d51d32 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -4098,34 +4098,46 @@ virStorageFileIsInitialized(const virStorageSource = *src) } =20 =20 -static virStorageFileBackendPtr -virStorageFileGetBackendForSupportCheck(const virStorageSource *src) +static int +virStorageFileGetBackendForSupportCheck(const virStorageSource *src, + virStorageFileBackendPtr *backend) { int actualType; =20 - if (!src) - return NULL; =20 - if (src->drv) - return src->drv->backend; + if (!src) { + *backend =3D NULL; + return 0; + } + + if (src->drv) { + *backend =3D src->drv->backend; + return 0; + } =20 actualType =3D virStorageSourceGetActualType(src); =20 - return virStorageFileBackendForTypeInternal(actualType, src->protocol,= false); + *backend =3D virStorageFileBackendForTypeInternal(actualType, src->pro= tocol, false); + return 0; } =20 =20 -static bool +static int virStorageFileSupportsBackingChainTraversal(virStorageSourcePtr src) { virStorageFileBackendPtr backend; + int ret; =20 - if (!(backend =3D virStorageFileGetBackendForSupportCheck(src))) - return false; + ret =3D virStorageFileGetBackendForSupportCheck(src, &backend); + if (ret < 0) + return -1; + + if (!backend) + return 0; =20 return backend->storageFileGetUniqueIdentifier && - backend->storageFileRead && - backend->storageFileAccess; + backend->storageFileRead && + backend->storageFileAccess ? 1 : 0; } =20 =20 @@ -4137,15 +4149,19 @@ virStorageFileSupportsBackingChainTraversal(virStor= ageSourcePtr src) * Check if a storage file supports operations needed by the security * driver to perform labelling */ -bool +int virStorageFileSupportsSecurityDriver(const virStorageSource *src) { virStorageFileBackendPtr backend; + int ret; =20 - if (!(backend =3D virStorageFileGetBackendForSupportCheck(src))) - return false; + ret =3D virStorageFileGetBackendForSupportCheck(src, &backend); + if (ret < 0) + return -1; + if (backend =3D=3D NULL) + return 0; =20 - return !!backend->storageFileChown; + return backend->storageFileChown ? 1 : 0; } =20 =20 @@ -4157,15 +4173,19 @@ virStorageFileSupportsSecurityDriver(const virStora= geSource *src) * Check if a storage file supports checking if the storage source is acce= ssible * for the given vm. */ -bool +int virStorageFileSupportsAccess(const virStorageSource *src) { virStorageFileBackendPtr backend; + int ret; =20 - if (!(backend =3D virStorageFileGetBackendForSupportCheck(src))) - return false; + ret =3D virStorageFileGetBackendForSupportCheck(src, &backend); + if (ret < 0) + return -1; + if (backend =3D=3D NULL) + return 0; =20 - return !!backend->storageFileAccess; + return backend->storageFileAccess ? 1 : 0; } =20 =20 @@ -4514,14 +4534,16 @@ virStorageFileGetMetadataRecurse(virStorageSourcePt= r src, ssize_t headerLen; virStorageSourcePtr backingStore =3D NULL; int backingFormat; + int rv; =20 VIR_DEBUG("path=3D%s format=3D%d uid=3D%u gid=3D%u probe=3D%d", src->path, src->format, (unsigned int)uid, (unsigned int)gid, allow_probe); =20 /* exit if we can't load information about the current image */ - if (!virStorageFileSupportsBackingChainTraversal(src)) - return 0; + rv =3D virStorageFileSupportsBackingChainTraversal(src); + if (rv <=3D 0) + return rv; =20 if (virStorageFileInitAs(src, uid, gid) < 0) return -1; diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index b92c1c47dd..0909fe212c 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -465,8 +465,8 @@ const char *virStorageFileGetUniqueIdentifier(virStorag= eSourcePtr src); int virStorageFileAccess(virStorageSourcePtr src, int mode); int virStorageFileChown(const virStorageSource *src, uid_t uid, gid_t gid); =20 -bool virStorageFileSupportsSecurityDriver(const virStorageSource *src); -bool virStorageFileSupportsAccess(const virStorageSource *src); +int virStorageFileSupportsSecurityDriver(const virStorageSource *src); +int virStorageFileSupportsAccess(const virStorageSource *src); =20 int virStorageFileGetMetadata(virStorageSourcePtr src, uid_t uid, gid_t gid, --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon May 6 23:34:43 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524671873417414.0962925286658; Wed, 25 Apr 2018 08:57:53 -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 AA2BD30BC123; Wed, 25 Apr 2018 15:57:51 +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 25179611AA; Wed, 25 Apr 2018 15:57:51 +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 F18534CAA5; Wed, 25 Apr 2018 15:57:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3PFqnJd026783 for ; Wed, 25 Apr 2018 11:52:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id CF8D2202323A; Wed, 25 Apr 2018 15:52:49 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.52]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3C9492023239; Wed, 25 Apr 2018 15:52:49 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Wed, 25 Apr 2018 16:52:43 +0100 Message-Id: <20180425155243.23406-7-berrange@redhat.com> In-Reply-To: <20180425155243.23406-1-berrange@redhat.com> References: <20180425155243.23406-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 6/6] storage: create separate loadable modules for storage file drivers 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: , Content-Type: text/plain; charset="utf-8" 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.42]); Wed, 25 Apr 2018 15:57:52 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 The storage file drivers are currently loaded as a side effect of loading the storage driver. This is a bogus dependancy because the storage file code has no interaction with the storage drivers, and even ultimately be running in a completely separate daemon. Signed-off-by: Daniel P. Berrang=C3=A9 --- libvirt.spec.in | 4 ++ src/storage/Makefile.inc.am | 40 +++++++++++++++++++ src/storage/storage_backend_fs.c | 4 -- src/storage/storage_backend_gluster.c | 4 -- src/util/virstoragefile.c | 10 +++-- src/util/virstoragefilebackend.c | 72 +++++++++++++++++++++++++++----= ---- src/util/virstoragefilebackend.h | 9 ++--- 7 files changed, 111 insertions(+), 32 deletions(-) diff --git a/libvirt.spec.in b/libvirt.spec.in index 5090dfa2aa..e8705bbb48 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1384,6 +1384,8 @@ rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/connection-dr= iver/*.la rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/connection-driver/*.a rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-backend/*.la rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-backend/*.a +rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-file/*.la +rm -f $RPM_BUILD_ROOT%{_libdir}/libvirt/storage-file/*.a %if %{with_wireshark} rm -f $RPM_BUILD_ROOT%{_libdir}/wireshark/plugins/libvirt.la %endif @@ -1886,6 +1888,7 @@ exit 0 %attr(0755, root, root) %{_libexecdir}/libvirt_parthelper %{_libdir}/%{name}/connection-driver/libvirt_driver_storage.so %{_libdir}/%{name}/storage-backend/libvirt_storage_backend_fs.so +%{_libdir}/%{name}/storage-file/libvirt_storage_file_fs.so =20 %files daemon-driver-storage-disk %{_libdir}/%{name}/storage-backend/libvirt_storage_backend_disk.so @@ -1905,6 +1908,7 @@ exit 0 %if %{with_storage_gluster} %files daemon-driver-storage-gluster %{_libdir}/%{name}/storage-backend/libvirt_storage_backend_gluster.so +%{_libdir}/%{name}/storage-file/libvirt_storage_file_gluster.so %endif =20 %if %{with_storage_rbd} diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am index af2c97ab93..ea98c0ee52 100644 --- a/src/storage/Makefile.inc.am +++ b/src/storage/Makefile.inc.am @@ -14,6 +14,9 @@ STORAGE_DRIVER_SOURCES =3D \ STORAGE_DRIVER_FS_SOURCES =3D \ storage/storage_backend_fs.h \ storage/storage_backend_fs.c \ + $(NULL) + +STORAGE_FILE_FS_SOURCES =3D \ storage/storage_file_fs.h \ storage/storage_file_fs.c \ $(NULL) @@ -57,6 +60,9 @@ STORAGE_DRIVER_SHEEPDOG_SOURCES =3D \ STORAGE_DRIVER_GLUSTER_SOURCES =3D \ storage/storage_backend_gluster.h \ storage/storage_backend_gluster.c \ + $(NULL) + +STORAGE_FILE_GLUSTER_SOURCES =3D \ storage/storage_file_gluster.h \ storage/storage_file_gluster.c \ $(NULL) @@ -80,6 +86,7 @@ STATEFUL_DRIVER_SOURCE_FILES +=3D $(STORAGE_DRIVER_SOURCE= S) EXTRA_DIST +=3D \ $(STORAGE_DRIVER_SOURCES) \ $(STORAGE_DRIVER_FS_SOURCES) \ + $(STORAGE_FILE_FS_SOURCES) \ $(STORAGE_DRIVER_LVM_SOURCES) \ $(STORAGE_DRIVER_ISCSI_SOURCES) \ $(STORAGE_DRIVER_SCSI_SOURCES) \ @@ -88,6 +95,7 @@ EXTRA_DIST +=3D \ $(STORAGE_DRIVER_RBD_SOURCES) \ $(STORAGE_DRIVER_SHEEPDOG_SOURCES) \ $(STORAGE_DRIVER_GLUSTER_SOURCES) \ + $(STORAGE_FILE_GLUSTER_SOURCES) \ $(STORAGE_DRIVER_ZFS_SOURCES) \ $(STORAGE_DRIVER_VSTORAGE_SOURCES) \ $(STORAGE_HELPER_DISK_SOURCES) \ @@ -96,6 +104,9 @@ EXTRA_DIST +=3D \ storagebackenddir =3D $(libdir)/libvirt/storage-backend storagebackend_LTLIBRARIES =3D =20 +storagefiledir =3D $(libdir)/libvirt/storage-file +storagefile_LTLIBRARIES =3D + # Needed to keep automake quiet about conditionals libvirt_driver_storage_impl_la_SOURCES =3D libvirt_driver_storage_impl_la_CFLAGS =3D \ @@ -136,6 +147,19 @@ libvirt_storage_backend_fs_la_LIBADD =3D \ libvirt.la \ ../gnulib/lib/libgnu.la \ $(NULL) + +libvirt_storage_file_fs_la_SOURCES =3D $(STORAGE_FILE_FS_SOURCES) +libvirt_storage_file_fs_la_CFLAGS =3D \ + -I$(srcdir)/conf \ + $(AM_CFLAGS) \ + $(NULL) + +storagefile_LTLIBRARIES +=3D libvirt_storage_file_fs.la +libvirt_storage_file_fs_la_LDFLAGS =3D $(AM_LDFLAGS_MOD) +libvirt_storage_file_fs_la_LIBADD =3D \ + libvirt.la \ + ../gnulib/lib/libgnu.la \ + $(NULL) endif WITH_STORAGE =20 if WITH_STORAGE_LVM @@ -270,6 +294,22 @@ libvirt_storage_backend_gluster_la_CFLAGS =3D \ =20 storagebackend_LTLIBRARIES +=3D libvirt_storage_backend_gluster.la libvirt_storage_backend_gluster_la_LDFLAGS =3D $(AM_LDFLAGS_MOD) + + +libvirt_storage_file_gluster_la_SOURCES =3D $(STORAGE_FILE_GLUSTER_SOURCES) +libvirt_storage_file_gluster_la_LIBADD =3D \ + libvirt.la \ + $(GLUSTERFS_LIBS) \ + ../gnulib/lib/libgnu.la \ + $(NULL) +libvirt_storage_file_gluster_la_CFLAGS =3D \ + -I$(srcdir)/conf \ + $(GLUSTERFS_CFLAGS) \ + $(AM_CFLAGS) \ + $(NULL) + +storagefile_LTLIBRARIES +=3D libvirt_storage_file_gluster.la +libvirt_storage_file_gluster_la_LDFLAGS =3D $(AM_LDFLAGS_MOD) endif WITH_STORAGE_GLUSTER =20 if WITH_STORAGE_ZFS diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend= _fs.c index bface86b43..37fea8c941 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -32,7 +32,6 @@ =20 #include "virerror.h" #include "storage_backend_fs.h" -#include "storage_file_fs.h" #include "storage_util.h" #include "storage_conf.h" #include "vircommand.h" @@ -713,8 +712,5 @@ virStorageBackendFsRegister(void) return -1; #endif /* WITH_STORAGE_FS */ =20 - if (virStorageFileFsRegister() < 0) - return -1; - return 0; } diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_ba= ckend_gluster.c index aca772676c..5d4b920a60 100644 --- a/src/storage/storage_backend_gluster.c +++ b/src/storage/storage_backend_gluster.c @@ -24,7 +24,6 @@ #include =20 #include "storage_backend_gluster.h" -#include "storage_file_gluster.h" #include "storage_conf.h" #include "viralloc.h" #include "virerror.h" @@ -567,8 +566,5 @@ virStorageBackendGlusterRegister(void) if (virStorageBackendRegister(&virStorageBackendGluster) < 0) return -1; =20 - if (virStorageFileGlusterRegister() < 0) - return -1; - return 0; } diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index da13d51d32..73937b527a 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -4117,7 +4117,9 @@ virStorageFileGetBackendForSupportCheck(const virStor= ageSource *src, =20 actualType =3D virStorageSourceGetActualType(src); =20 - *backend =3D virStorageFileBackendForTypeInternal(actualType, src->pro= tocol, false); + if (virStorageFileBackendForType(actualType, src->protocol, false, bac= kend) < 0) + return -1; + return 0; } =20 @@ -4234,8 +4236,10 @@ virStorageFileInitAs(virStorageSourcePtr src, else src->drv->gid =3D gid; =20 - if (!(src->drv->backend =3D virStorageFileBackendForType(actualType, - src->protocol))) + if (virStorageFileBackendForType(actualType, + src->protocol, + true, + &src->drv->backend) < 0) goto error; =20 if (src->drv->backend->backendInit && diff --git a/src/util/virstoragefilebackend.c b/src/util/virstoragefileback= end.c index df86ee39c2..ac087dabac 100644 --- a/src/util/virstoragefilebackend.c +++ b/src/util/virstoragefilebackend.c @@ -32,6 +32,7 @@ #include "internal.h" #include "virstoragefilebackend.h" #include "virlog.h" +#include "virmodule.h" #include "virfile.h" #include "configmake.h" =20 @@ -44,6 +45,46 @@ VIR_LOG_INIT("storage.storage_source_backend"); static virStorageFileBackendPtr virStorageFileBackends[VIR_STORAGE_BACKEND= S_MAX]; static size_t virStorageFileBackendsCount; =20 +#define STORAGE_FILE_MODULE_DIR LIBDIR "/libvirt/storage-file" + +static int +virStorageFileLoadBackendModule(const char *name, + const char *regfunc, + bool forceload) +{ + char *modfile =3D NULL; + int ret; + + if (!(modfile =3D virFileFindResourceFull(name, + "libvirt_storage_file_", + ".so", + abs_topbuilddir "/src/.libs", + STORAGE_FILE_MODULE_DIR, + "LIBVIRT_STORAGE_FILE_DIR"))) + return -1; + + ret =3D virModuleLoad(modfile, regfunc, forceload); + + VIR_FREE(modfile); + + return ret; +} + + +static int virStorageFileBackendOnceInit(void) +{ +#if WITH_STORAGE_DIR || WITH_STORAGE_FS + if (virStorageFileLoadBackendModule("fs", "virStorageFileFsRegister", = false) < 0) + return -1; +#endif /* WITH_STORAGE_DIR || WITH_STORAGE_FS */ +#if WITH_STORAGE_GLUSTER + if (virStorageFileLoadBackendModule("gluster", "virStorageFileGlusterR= egister", false) < 0) + return -1; +#endif /* WITH_STORAGE_GLUSTER */ + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virStorageFileBackend) =20 int virStorageFileBackendRegister(virStorageFileBackendPtr backend) @@ -65,25 +106,32 @@ virStorageFileBackendRegister(virStorageFileBackendPtr= backend) return 0; } =20 -virStorageFileBackendPtr -virStorageFileBackendForTypeInternal(int type, - int protocol, - bool report) +int +virStorageFileBackendForType(int type, + int protocol, + bool required, + virStorageFileBackendPtr *backend) { size_t i; =20 + *backend =3D NULL; + + if (virStorageFileBackendInitialize() < 0) + return -1; + for (i =3D 0; i < virStorageFileBackendsCount; i++) { if (virStorageFileBackends[i]->type =3D=3D type) { if (type =3D=3D VIR_STORAGE_TYPE_NETWORK && virStorageFileBackends[i]->protocol !=3D protocol) continue; =20 - return virStorageFileBackends[i]; + *backend =3D virStorageFileBackends[i]; + return 0; } } =20 - if (!report) - return NULL; + if (!required) + return 0; =20 if (type =3D=3D VIR_STORAGE_TYPE_NETWORK) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -96,13 +144,5 @@ virStorageFileBackendForTypeInternal(int type, virStorageTypeToString(type)); } =20 - return NULL; -} - - -virStorageFileBackendPtr -virStorageFileBackendForType(int type, - int protocol) -{ - return virStorageFileBackendForTypeInternal(type, protocol, true); + return -1; } diff --git a/src/util/virstoragefilebackend.h b/src/util/virstoragefileback= end.h index 6cd51750ee..6686864367 100644 --- a/src/util/virstoragefilebackend.h +++ b/src/util/virstoragefilebackend.h @@ -71,11 +71,10 @@ typedef int uid_t uid, gid_t gid); =20 -virStorageFileBackendPtr virStorageFileBackendForType(int type, int protoc= ol); -virStorageFileBackendPtr virStorageFileBackendForTypeInternal(int type, - int protocol, - bool report); - +int virStorageFileBackendForType(int type, + int protocol, + bool required, + virStorageFileBackendPtr *backend); =20 struct _virStorageFileBackend { int type; --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list