[libvirt] [PATCH 0/4] Couple of virFileIsShared* fixes

This patch [1] got me looking into the code. I've found couple of issues which I'm fixing and also I'm introducing couple of new test cases. 1: https://www.redhat.com/archives/libvir-list/2018-October/msg00513.html Michal Prívozník (4): virfiletest: Fix test name prefix for virFileInData test virfiletst: Test virFileIsSharedFS virFileIsSharedFSType: Detect direct mount points virfile: Rework virFileIsSharedFixFUSE src/util/virfile.c | 66 ++++++--------- tests/Makefile.am | 7 +- tests/virfiledata/mounts3.txt | 35 ++++++++ tests/virfilemock.c | 154 ++++++++++++++++++++++++++++++++++ tests/virfiletest.c | 64 +++++++++++++- 5 files changed, 284 insertions(+), 42 deletions(-) create mode 100644 tests/virfiledata/mounts3.txt create mode 100644 tests/virfilemock.c -- 2.18.0

Because of lacking virTestCounterReset() call, the old test cases name was preserved. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/virfiletest.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/virfiletest.c b/tests/virfiletest.c index efe15485eb..19dd06e49e 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -388,6 +388,7 @@ mymain(void) } while (0) if (holesSupported()) { + virTestCounterReset("testFileInData "); DO_TEST_IN_DATA(true, 4, 4, 4); DO_TEST_IN_DATA(false, 4, 4, 4); DO_TEST_IN_DATA(true, 8, 8, 8); -- 2.18.0

On Tue, Oct 09, 2018 at 15:48:44 +0200, Michal Privoznik wrote:
Because of lacking virTestCounterReset() call, the old test cases name was preserved.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/virfiletest.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/tests/virfiletest.c b/tests/virfiletest.c index efe15485eb..19dd06e49e 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -388,6 +388,7 @@ mymain(void) } while (0)
if (holesSupported()) { + virTestCounterReset("testFileInData "); DO_TEST_IN_DATA(true, 4, 4, 4); DO_TEST_IN_DATA(false, 4, 4, 4); DO_TEST_IN_DATA(true, 8, 8, 8);
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>

Introduce some basic test cases for virFileIsSharedFS(). More will be added later. In order to achieve desired result, mocks for setmntent() and statfs() need to be invented because the first thing that virFileIsSharedFS() does is calling the latter. If it finds a FUSE mount it'll call the former. The mock might look a bit complicated, but in fact it's quite simple. The test sets LIBVIRT_MTAB env variable to hold the absolute path to a file containing mount table. Then, statfs() returns matching FS it finds, and setmntent() is there just to replace /proc/mounts with the file the test wants to load. Adding this test also exposed a bug we have - because we assume the given path points to a file we cut off what we assume is a file name to obtain directory path and only then we call statfs(). This is buggy because the passed path could be already a mount point. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/Makefile.am | 7 +- tests/virfiledata/mounts3.txt | 33 ++++++++ tests/virfilemock.c | 154 ++++++++++++++++++++++++++++++++++ tests/virfiletest.c | 62 +++++++++++++- 4 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 tests/virfiledata/mounts3.txt create mode 100644 tests/virfilemock.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 51be1509e1..d7ec7e3a6f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -242,6 +242,7 @@ test_libraries += virusbmock.la \ virnetdevbandwidthmock.la \ virnumamock.la \ virtestmock.la \ + virfilemock.la \ $(NULL) endif WITH_LINUX @@ -1170,9 +1171,13 @@ virresctrltest_SOURCES = \ virresctrltest.c testutils.h testutils.c virfilewrapper.h virfilewrapper.c virresctrltest_LDADD = $(LDADDS) +virfilemock_la_SOURCES = \ + virfilemock.c +virfilemock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS) +virfilemock_la_LIBADD = $(MOCKLIBS_LIBS) else ! WITH_LINUX EXTRA_DIST += vircaps2xmltest.c virnumamock.c virfilewrapper.c \ - virfilewrapper.h virresctrltest.c + virfilewrapper.h virresctrltest.c virfilemock.c endif ! WITH_LINUX if WITH_NSS diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt new file mode 100644 index 0000000000..226f67dc00 --- /dev/null +++ b/tests/virfiledata/mounts3.txt @@ -0,0 +1,33 @@ +/dev/root / xfs rw,noatime,attr2,inode64,noquota 0 0 +proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 +tmpfs /run tmpfs rw,nodev,relatime,size=3281436k,mode=755 0 0 +sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0 +dev /dev devtmpfs rw,nosuid,relatime,size=10240k,nr_inodes=4093060,mode=755 0 0 +securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0 +debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0 +mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0 +configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0 +devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0 +fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0 +shm /dev/shm tmpfs rw,nosuid,nodev,noexec,relatime 0 0 +cgroup_root /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,relatime,size=10240k,mode=755 0 0 +openrc /sys/fs/cgroup/openrc cgroup rw,nosuid,nodev,noexec,relatime,release_agent=/lib/rc/sh/cgroup-release-agent.sh,name=openrc 0 0 +none /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0 +cpuset /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0 +cpu /sys/fs/cgroup/cpu cgroup rw,nosuid,nodev,noexec,relatime,cpu 0 0 +cpuacct /sys/fs/cgroup/cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct 0 0 +blkio /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0 +memory /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0 +devices /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0 +freezer /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0 +net_cls /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0 +perf_event /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0 +net_prio /sys/fs/cgroup/net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_prio 0 0 +hugetlb /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0 +pids /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0 +rdma /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0 +binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0 +hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=1777,pagesize=2M 0 0 +none /run/user/1000 tmpfs rw,relatime,mode=700,uid=1000 0 0 +host:/nfs /nfs nfs4 rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp6,timeo=600,retrans=2,sec=sys,clientaddr=::,local_lock=none,addr=:: 0 0 +dev /nfs/blah devtmpfs rw,nosuid,relatime,size=10240k,nr_inodes=4093060,mode=755 0 0 diff --git a/tests/virfilemock.c b/tests/virfilemock.c new file mode 100644 index 0000000000..d458616899 --- /dev/null +++ b/tests/virfilemock.c @@ -0,0 +1,154 @@ +/* + * Copyright (C) 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 + * <http://www.gnu.org/licenses/>. + * + * Author: Michal Privoznik <mprivozn@redhat.com> + */ + +#include <config.h> + +#include <stdio.h> +#include <mntent.h> +#include <sys/vfs.h> +#if HAVE_LINUX_MAGIC_H +# include <linux/magic.h> +#endif + +#include "virmock.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +static FILE *(*real_setmntent)(const char *filename, const char *type); +static int (*real_statfs)(const char *path, struct statfs *buf); + + +static void +init_syms(void) +{ + if (real_setmntent) + return; + + VIR_MOCK_REAL_INIT(setmntent); + VIR_MOCK_REAL_INIT(statfs); +} + + +FILE * +setmntent(const char *filename, const char *type) +{ + const char *mtab; + + init_syms(); + + if ((mtab = getenv("LIBVIRT_MTAB"))) + filename = mtab; + + return real_setmntent(filename, type); +} + + +#ifndef NFS_SUPER_MAGIC +# define NFS_SUPER_MAGIC 0x6969 +#endif +#ifndef OCFS2_SUPER_MAGIC +# define OCFS2_SUPER_MAGIC 0x7461636f +#endif +#ifndef GFS2_MAGIC +# define GFS2_MAGIC 0x01161970 +#endif +#ifndef AFS_FS_MAGIC +# define AFS_FS_MAGIC 0x6B414653 +#endif +#ifndef SMB_SUPER_MAGIC +# define SMB_SUPER_MAGIC 0x517B +#endif +#ifndef CIFS_SUPER_MAGIC +# define CIFS_SUPER_MAGIC 0xFF534D42 +#endif +#ifndef HUGETLBFS_MAGIC +# define HUGETLBFS_MAGIC 0x958458f6 +#endif +#ifndef FUSE_SUPER_MAGIC +# define FUSE_SUPER_MAGIC 0x65735546 +#endif + + +static int +statfs_mock(const char *mtab, + const char *path, + struct statfs *buf) +{ + FILE *f; + struct mntent mb; + char mntbuf[1024]; + int ret = -1; + + if (!(f = real_setmntent(mtab, "r"))) { + fprintf(stderr, "Unable to open %s", mtab); + abort(); + } + + while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { + int ftype; + + if (STRNEQ(mb.mnt_dir, path)) + continue; + + if (STREQ(mb.mnt_type, "nfs") || + STREQ(mb.mnt_type, "nfs4")) { + ftype = NFS_SUPER_MAGIC; + } else if (STREQ(mb.mnt_type, "gfs2")|| + STREQ(mb.mnt_type, "gfs2meta")) { + ftype = GFS2_MAGIC; + } else if (STREQ(mb.mnt_type, "ocfs2")) { + ftype = OCFS2_SUPER_MAGIC; + } else if (STREQ(mb.mnt_type, "afs")) { + ftype = AFS_FS_MAGIC; + } else if (STREQ(mb.mnt_type, "smb3")) { + ftype = SMB_SUPER_MAGIC; + } else if (STREQ(mb.mnt_type, "cifs")) { + ftype = CIFS_SUPER_MAGIC; + } else if (STRPREFIX(mb.mnt_type, "fuse")) { + ftype = FUSE_SUPER_MAGIC; + } else { + /* Everything else is EXT4. We don't care really for other paths. */ + ftype = EXT4_SUPER_MAGIC; + } + + memset(buf, 0, sizeof(*buf)); + /* We only care about f_type so far. */ + buf->f_type = ftype; + ret = 0; + break; + } + + endmntent(f); + return ret; +} + + +int +statfs(const char *path, struct statfs *buf) +{ + const char *mtab; + + init_syms(); + + if ((mtab = getenv("LIBVIRT_MTAB"))) + return statfs_mock(mtab, path, buf); + + return real_statfs(path, buf); +} diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 19dd06e49e..22c163f0a0 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -30,6 +30,7 @@ # include <linux/falloc.h> #endif +#define VIR_FROM_THIS VIR_FROM_NONE #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R static int testFileCheckMounts(const char *prefix, @@ -309,6 +310,48 @@ testFileInData(const void *opaque) } +struct testFileIsSharedFSType { + const char *mtabFile; + const char *filename; + const bool expected; +}; + +static int +testFileIsSharedFSType(const void *opaque ATTRIBUTE_UNUSED) +{ +#ifndef __linux__ + return EXIT_AM_SKIP; +#else + const struct testFileIsSharedFSType *data = opaque; + char *mtabFile = NULL; + bool actual; + int ret = -1; + + if (virAsprintf(&mtabFile, abs_srcdir "/virfiledata/%s", data->mtabFile) < 0) + return -1; + + if (setenv("LIBVIRT_MTAB", mtabFile, 1) < 0) { + fprintf(stderr, "Unable to set env variable\n"); + goto cleanup; + } + + actual = virFileIsSharedFS(data->filename); + + if (actual != data->expected) { + fprintf(stderr, "Unexpected FS type. Expected %d got %d\n", + data->expected, actual); + goto cleanup; + } + + ret = 0; + cleanup: + VIR_FREE(mtabFile); + unsetenv("LIBVIRT_MTAB"); + return ret; +#endif +} + + static int mymain(void) { @@ -396,7 +439,24 @@ mymain(void) DO_TEST_IN_DATA(true, 8, 16, 32, 64, 128, 256, 512); DO_TEST_IN_DATA(false, 8, 16, 32, 64, 128, 256, 512); } + +#define DO_TEST_FILE_IS_SHARED_FS_TYPE(mtab, file, exp) \ + do { \ + struct testFileIsSharedFSType data = { \ + .mtabFile = mtab, .filename = file, .expected = exp \ + }; \ + if (virTestRun(virTestCounterNext(), testFileIsSharedFSType, &data) < 0) \ + ret = -1; \ + } while (0) + + virTestCounterReset("testFileIsSharedFSType "); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); + /* TODO Detect bind mounts */ + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", true); + return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilemock.so") -- 2.18.0

On Tue, Oct 09, 2018 at 15:48:45 +0200, Michal Privoznik wrote:
Introduce some basic test cases for virFileIsSharedFS(). More will be added later. In order to achieve desired result, mocks for setmntent() and statfs() need to be invented because the first thing that virFileIsSharedFS() does is calling the latter. If it finds a FUSE mount it'll call the former.
The mock might look a bit complicated, but in fact it's quite simple. The test sets LIBVIRT_MTAB env variable to hold the absolute path to a file containing mount table. Then, statfs() returns matching FS it finds, and setmntent() is there just to replace /proc/mounts with the file the test wants to load.
Adding this test also exposed a bug we have - because we assume the given path points to a file we cut off what we assume is a file name to obtain directory path and only then we call statfs(). This is buggy because the passed path could be already a mount point.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tests/Makefile.am | 7 +- tests/virfiledata/mounts3.txt | 33 ++++++++ tests/virfilemock.c | 154 ++++++++++++++++++++++++++++++++++ tests/virfiletest.c | 62 +++++++++++++- 4 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 tests/virfiledata/mounts3.txt create mode 100644 tests/virfilemock.c ... diff --git a/tests/virfilemock.c b/tests/virfilemock.c new file mode 100644 index 0000000000..d458616899 --- /dev/null +++ b/tests/virfilemock.c ... +static int +statfs_mock(const char *mtab, + const char *path, + struct statfs *buf) +{ + FILE *f; + struct mntent mb; + char mntbuf[1024]; + int ret = -1; + + if (!(f = real_setmntent(mtab, "r"))) { + fprintf(stderr, "Unable to open %s", mtab); + abort();
I think just returning -1 would be better. The caller will take care of reporting the error.
+ } + + while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { + int ftype; + + if (STRNEQ(mb.mnt_dir, path)) + continue;
I was wondering how this can work as we call statfs on an arbitrary path rather than on the exact mount point. But it works because virFileIsSharedFSType calls statfs in a loop checking smaller part of the path in each iteration. That said, the mocked function is not generic, but it's good enough for our use case. ... With the change suggested above Reviewed-by: Jiri Denemark <jdenemar@redhat.com>

If the given path is already a mount point (e.g. a bind mount of a file, or simply a direct mount point of a FS), then our code fails to detect that because the first thing it does is cutting off part after last slash '/'. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virfile.c | 8 ++++---- tests/virfiletest.c | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 2a7e87102a..666d703f99 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3534,15 +3534,16 @@ virFileIsSharedFSType(const char *path, int fstypes) { VIR_AUTOFREE(char *) dirpath = NULL; - char *p; + char *p = NULL; struct statfs sb; int statfs_ret; if (VIR_STRDUP(dirpath, path) < 0) return -1; - do { + statfs_ret = statfs(dirpath, &sb); + while ((statfs_ret < 0) && (p != dirpath)) { /* Try less and less of the path until we get to a * directory we can stat. Even if we don't have 'x' * permission on any directory in the path on the NFS @@ -3563,8 +3564,7 @@ virFileIsSharedFSType(const char *path, *p = '\0'; statfs_ret = statfs(dirpath, &sb); - - } while ((statfs_ret < 0) && (p != dirpath)); + } if (statfs_ret < 0) { virReportSystemError(errno, diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 22c163f0a0..42d918aecc 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -453,8 +453,7 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); - /* TODO Detect bind mounts */ - DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", true); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false); return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -- 2.18.0

On Tue, Oct 09, 2018 at 15:48:46 +0200, Michal Privoznik wrote:
If the given path is already a mount point (e.g. a bind mount of a file, or simply a direct mount point of a FS), then our code fails to detect that because the first thing it does is cutting off part after last slash '/'.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virfile.c | 8 ++++---- tests/virfiletest.c | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-)
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>

There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code. The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount. Reported-by: Han Han <hhan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virfile.c | 58 +++++++++++++---------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 666d703f99..19f504cc6d 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3468,18 +3468,14 @@ static int virFileIsSharedFixFUSE(const char *path, long *f_type) { - char *dirpath = NULL; - const char **mounts = NULL; - size_t nmounts = 0; - char *p; FILE *f = NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir = NULL; + char *mntType = NULL; + size_t maxMatching = 0; int ret = -1; - if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f = setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,33 @@ virFileIsSharedFixFUSE(const char *path, } while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) + const char *p; + size_t len = strlen(mb.mnt_dir); + + if (!(p = STRSKIP(path, mb.mnt_dir))) continue; - if (VIR_APPEND_ELEMENT_COPY(mounts, nmounts, mb.mnt_dir) < 0) - goto cleanup; - } - - /* Add NULL sentinel so that this is a virStringList */ - if (VIR_REALLOC_N(mounts, nmounts + 1) < 0) - goto cleanup; - mounts[nmounts] = NULL; - - do { - if ((p = strrchr(dirpath, '/')) == NULL) { - virReportSystemError(EINVAL, - _("Invalid relative path '%s'"), path); - goto cleanup; + if (len > maxMatching) { + len = maxMatching; + VIR_FREE(mntType); + VIR_FREE(mntDir); + if (VIR_STRDUP(mntDir, mb.mnt_dir) < 0 || + VIR_STRDUP(mntType, mb.mnt_type) < 0) + goto cleanup; } + } - if (p == dirpath) - *(p+1) = '\0'; - else - *p = '\0'; - - if (virStringListHasString(mounts, dirpath)) { - VIR_DEBUG("Found gluster FUSE mountpoint=%s for path=%s. " - "Fixing shared FS type", dirpath, path); - *f_type = GFS2_MAGIC; - break; - } - } while (p != dirpath); + if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { + VIR_DEBUG("Found gluster FUSE mountpoint=%s for path=%s. " + "Fixing shared FS type", mntDir, path); + *f_type = GFS2_MAGIC; + } ret = 0; cleanup: + VIR_FREE(mntType); + VIR_FREE(mntDir); endmntent(f); - VIR_FREE(mounts); - VIR_FREE(dirpath); return ret; } diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt index 226f67dc00..134c6e8f81 100644 --- a/tests/virfiledata/mounts3.txt +++ b/tests/virfiledata/mounts3.txt @@ -31,3 +31,5 @@ hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=1777,pagesize=2M 0 0 none /run/user/1000 tmpfs rw,relatime,mode=700,uid=1000 0 0 host:/nfs /nfs nfs4 rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp6,timeo=600,retrans=2,sec=sys,clientaddr=::,local_lock=none,addr=:: 0 0 dev /nfs/blah devtmpfs rw,nosuid,relatime,size=10240k,nr_inodes=4093060,mode=755 0 0 +host:/gv0 /gluster fuse.glusterfs rw 0 0 +root@host:/tmp/mkdir /gluster/sshfs fuse.sshfs rw 0 0 diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 42d918aecc..d5102b1cc4 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -454,6 +454,8 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/file", true); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/sshfs/file", false); return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -- 2.18.0

On Tue, Oct 09, 2018 at 15:48:47 +0200, Michal Privoznik wrote:
There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code.
The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount.
Reported-by: Han Han <hhan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virfile.c | 58 +++++++++++++---------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 26 insertions(+), 36 deletions(-)
diff --git a/src/util/virfile.c b/src/util/virfile.c index 666d703f99..19f504cc6d 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3468,18 +3468,14 @@ static int virFileIsSharedFixFUSE(const char *path, long *f_type) { - char *dirpath = NULL; - const char **mounts = NULL; - size_t nmounts = 0; - char *p; FILE *f = NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir = NULL; + char *mntType = NULL; + size_t maxMatching = 0; int ret = -1;
- if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f = setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,33 @@ virFileIsSharedFixFUSE(const char *path, }
while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) + const char *p; + size_t len = strlen(mb.mnt_dir); + + if (!(p = STRSKIP(path, mb.mnt_dir))) continue;
I think you wanted to do something clever with the p pointer here to avoid false positives on, e.g., /mnt/ble when searching for /mnt/bleak path. Otherwise you could have just use STRPREFIX(). The rest of the patch looks OK. Jirka

On 10/10/2018 01:08 PM, Jiri Denemark wrote:
On Tue, Oct 09, 2018 at 15:48:47 +0200, Michal Privoznik wrote:
There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code.
The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount.
Reported-by: Han Han <hhan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virfile.c | 58 +++++++++++++---------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 26 insertions(+), 36 deletions(-)
diff --git a/src/util/virfile.c b/src/util/virfile.c index 666d703f99..19f504cc6d 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3468,18 +3468,14 @@ static int virFileIsSharedFixFUSE(const char *path, long *f_type) { - char *dirpath = NULL; - const char **mounts = NULL; - size_t nmounts = 0; - char *p; FILE *f = NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir = NULL; + char *mntType = NULL; + size_t maxMatching = 0; int ret = -1;
- if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f = setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,33 @@ virFileIsSharedFixFUSE(const char *path, }
while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) + const char *p; + size_t len = strlen(mb.mnt_dir); + + if (!(p = STRSKIP(path, mb.mnt_dir))) continue;
I think you wanted to do something clever with the p pointer here to avoid false positives on, e.g., /mnt/ble when searching for /mnt/bleak path. Otherwise you could have just use STRPREFIX().
The rest of the patch looks OK.
Oh right. I wanted to do this: diff --git i/src/util/virfile.c w/src/util/virfile.c index 19f504cc6d..03e14c757f 100644 --- i/src/util/virfile.c +++ w/src/util/virfile.c @@ -3490,6 +3490,9 @@ virFileIsSharedFixFUSE(const char *path, if (!(p = STRSKIP(path, mb.mnt_dir))) continue; + if (*p != '/') + continue; + if (len > maxMatching) { len = maxMatching; VIR_FREE(mntType); Michal

There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code. The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount. Reported-by: Han Han <hhan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- diff to v1: - fixed while() loop - added more checks for @p - fixed @maxMatching assignment (facepalm) src/util/virfile.c | 59 ++++++++++++++--------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 666d703f99..f964806c3a 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3468,18 +3468,14 @@ static int virFileIsSharedFixFUSE(const char *path, long *f_type) { - char *dirpath = NULL; - const char **mounts = NULL; - size_t nmounts = 0; - char *p; FILE *f = NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir = NULL; + char *mntType = NULL; + size_t maxMatching = 0; int ret = -1; - if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f = setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,36 @@ virFileIsSharedFixFUSE(const char *path, } while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) - continue; + const char *p; + size_t len = strlen(mb.mnt_dir); - if (VIR_APPEND_ELEMENT_COPY(mounts, nmounts, mb.mnt_dir) < 0) - goto cleanup; - } + if (!(p = STRSKIP(path, mb.mnt_dir))) + continue; - /* Add NULL sentinel so that this is a virStringList */ - if (VIR_REALLOC_N(mounts, nmounts + 1) < 0) - goto cleanup; - mounts[nmounts] = NULL; + if (*(p - 1) != '/' && *p != '/' && *p != '\0') + continue; - do { - if ((p = strrchr(dirpath, '/')) == NULL) { - virReportSystemError(EINVAL, - _("Invalid relative path '%s'"), path); - goto cleanup; + if (len > maxMatching) { + maxMatching = len; + VIR_FREE(mntType); + VIR_FREE(mntDir); + if (VIR_STRDUP(mntDir, mb.mnt_dir) < 0 || + VIR_STRDUP(mntType, mb.mnt_type) < 0) + goto cleanup; } + } - if (p == dirpath) - *(p+1) = '\0'; - else - *p = '\0'; - - if (virStringListHasString(mounts, dirpath)) { - VIR_DEBUG("Found gluster FUSE mountpoint=%s for path=%s. " - "Fixing shared FS type", dirpath, path); - *f_type = GFS2_MAGIC; - break; - } - } while (p != dirpath); + if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { + VIR_DEBUG("Found gluster FUSE mountpoint=%s for path=%s. " + "Fixing shared FS type", mntDir, path); + *f_type = GFS2_MAGIC; + } ret = 0; cleanup: + VIR_FREE(mntType); + VIR_FREE(mntDir); endmntent(f); - VIR_FREE(mounts); - VIR_FREE(dirpath); return ret; } diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt index 226f67dc00..134c6e8f81 100644 --- a/tests/virfiledata/mounts3.txt +++ b/tests/virfiledata/mounts3.txt @@ -31,3 +31,5 @@ hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=1777,pagesize=2M 0 0 none /run/user/1000 tmpfs rw,relatime,mode=700,uid=1000 0 0 host:/nfs /nfs nfs4 rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp6,timeo=600,retrans=2,sec=sys,clientaddr=::,local_lock=none,addr=:: 0 0 dev /nfs/blah devtmpfs rw,nosuid,relatime,size=10240k,nr_inodes=4093060,mode=755 0 0 +host:/gv0 /gluster fuse.glusterfs rw 0 0 +root@host:/tmp/mkdir /gluster/sshfs fuse.sshfs rw 0 0 diff --git a/tests/virfiletest.c b/tests/virfiletest.c index 42d918aecc..d5102b1cc4 100644 --- a/tests/virfiletest.c +++ b/tests/virfiletest.c @@ -454,6 +454,8 @@ mymain(void) DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true); DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/file", true); + DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/sshfs/file", false); return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -- 2.18.0

On Wed, Oct 10, 2018 at 16:43:03 +0200, Michal Privoznik wrote:
There are couple of things wrong with the current implementation. The first one is that in the first loop the code tries to build a list of fuse.glusterfs mount points. Well, since the strings are allocated in a temporary buffer and are not duplicated this results in wrong decision made later in the code.
The second problem is that the code does not take into account subtree mounts. For instance, if there's a fuse.gluster mounted at /some/path and another FS mounted at /some/path/subdir the code would not recognize this subdir mount.
Reported-by: Han Han <hhan@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> ---
diff to v1:
- fixed while() loop - added more checks for @p - fixed @maxMatching assignment (facepalm)
src/util/virfile.c | 59 ++++++++++++++--------------------- tests/virfiledata/mounts3.txt | 2 ++ tests/virfiletest.c | 2 ++ 3 files changed, 28 insertions(+), 35 deletions(-)
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
participants (2)
-
Jiri Denemark
-
Michal Privoznik