From ad0cbefb13a8a239e2b004348de57dfb76f32531 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Mon, 5 Sep 2022 14:05:55 +0200 Subject: [PATCH] util: virfile: Rewrite matching of FUSE-based shared filesystems MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 'virFileIsSharedFixFUSE' was used to update the 'f_type' field for certain shared filesystem types. This patch renames it to 'virFileIsSharedFsFUSE' and makes it directly return whether the FUSE filesystem is shared or not and simplifies additions to the list of shared FUSE filesystems. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/util/virfile.c | 54 ++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index a8443acf8d..33a3ffe8f7 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3324,17 +3324,27 @@ virFileRemoveLastComponent(char *path) # define PROC_MOUNTS "/proc/mounts" + +struct virFileSharedFsData { + const char *mnttype; + unsigned int fstype; +}; + +static const struct virFileSharedFsData virFileSharedFsFUSE[] = { + { .mnttype = "fuse.glusterfs", .fstype = VIR_FILE_SHFS_GFS2 }, + { .mnttype = "fuse.quobyte", .fstype = VIR_FILE_SHFS_QB }, +}; + static int -virFileIsSharedFixFUSE(const char *path, - long long *f_type) +virFileIsSharedFsFUSE(const char *path, + unsigned int fstypes) { FILE *f = NULL; struct mntent mb; char mntbuf[1024]; - char *mntDir = NULL; - char *mntType = NULL; g_autofree char *canonPath = NULL; size_t maxMatching = 0; + bool isShared = false; if (!(canonPath = virFileCanonicalizePath(path))) { virReportSystemError(errno, _("unable to canonicalize %s"), path); @@ -3359,28 +3369,30 @@ virFileIsSharedFixFUSE(const char *path, continue; if (len > maxMatching) { + size_t i; + bool found = false; + + for (i = 0; i < G_N_ELEMENTS(virFileSharedFsFUSE); i++) { + if (STREQ_NULLABLE(mb.mnt_type, virFileSharedFsFUSE[i].mnttype) && + (fstypes & virFileSharedFsFUSE[i].fstype) > 0) { + found = true; + break; + } + } + + VIR_DEBUG("Updating shared='%d' for mountpoint '%s' type '%s'", + found, p, mb.mnt_type); + + isShared = found; maxMatching = len; - VIR_FREE(mntType); - VIR_FREE(mntDir); - mntDir = g_strdup(mb.mnt_dir); - mntType = g_strdup(mb.mnt_type); } } endmntent(f); - if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { - VIR_DEBUG("Found gluster FUSE mountpoint=%s for path=%s. " - "Fixing shared FS type", mntDir, canonPath); - *f_type = GFS2_MAGIC; - } else if (STREQ_NULLABLE(mntType, "fuse.quobyte")) { - VIR_DEBUG("Found Quobyte FUSE mountpoint=%s for path=%s. " - "Fixing shared FS type", mntDir, canonPath); - *f_type = QB_MAGIC; - } + if (isShared) + return 1; - VIR_FREE(mntType); - VIR_FREE(mntDir); return 0; } @@ -3432,8 +3444,8 @@ virFileIsSharedFSType(const char *path, f_type = sb.f_type; if (f_type == FUSE_SUPER_MAGIC) { - VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path); - virFileIsSharedFixFUSE(path, &f_type); + VIR_DEBUG("Found FUSE mount for path=%s", path); + return virFileIsSharedFsFUSE(path, fstypes); } VIR_DEBUG("Check if path %s with FS magic %lld is shared", -- 2.47.2