From nobody Wed May 14 05:27:00 2025 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