From nobody Mon Sep 16 20:39:10 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 1539182597907621.1261753472821; Wed, 10 Oct 2018 07:43:17 -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 02CB6227D5; Wed, 10 Oct 2018 14:43: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 AE65783EBB; Wed, 10 Oct 2018 14:43:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 356C9180B5B7; Wed, 10 Oct 2018 14:43:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9AEhBUg001621 for ; Wed, 10 Oct 2018 10:43:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id 621B91057068; Wed, 10 Oct 2018 14:43:11 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id B13661001F58; Wed, 10 Oct 2018 14:43:07 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Wed, 10 Oct 2018 16:43:03 +0200 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: jdenemar@redhat.com Subject: [libvirt] [PATCH v2 4/4] virfile: Rework virFileIsSharedFixFUSE X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 10 Oct 2018 14:43:16 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- 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 =3D NULL; - const char **mounts =3D NULL; - size_t nmounts =3D 0; - char *p; FILE *f =3D NULL; struct mntent mb; char mntbuf[1024]; + char *mntDir =3D NULL; + char *mntType =3D NULL; + size_t maxMatching =3D 0; int ret =3D -1; =20 - if (VIR_STRDUP(dirpath, path) < 0) - return -1; - if (!(f =3D setmntent(PROC_MOUNTS, "r"))) { virReportSystemError(errno, _("Unable to open %s"), @@ -3488,43 +3484,36 @@ virFileIsSharedFixFUSE(const char *path, } =20 while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { - if (STRNEQ("fuse.glusterfs", mb.mnt_type)) - continue; + const char *p; + size_t len =3D strlen(mb.mnt_dir); =20 - if (VIR_APPEND_ELEMENT_COPY(mounts, nmounts, mb.mnt_dir) < 0) - goto cleanup; - } + if (!(p =3D STRSKIP(path, mb.mnt_dir))) + continue; =20 - /* Add NULL sentinel so that this is a virStringList */ - if (VIR_REALLOC_N(mounts, nmounts + 1) < 0) - goto cleanup; - mounts[nmounts] =3D NULL; + if (*(p - 1) !=3D '/' && *p !=3D '/' && *p !=3D '\0') + continue; =20 - do { - if ((p =3D strrchr(dirpath, '/')) =3D=3D NULL) { - virReportSystemError(EINVAL, - _("Invalid relative path '%s'"), path); - goto cleanup; + if (len > maxMatching) { + maxMatching =3D len; + VIR_FREE(mntType); + VIR_FREE(mntDir); + if (VIR_STRDUP(mntDir, mb.mnt_dir) < 0 || + VIR_STRDUP(mntType, mb.mnt_type) < 0) + goto cleanup; } + } =20 - if (p =3D=3D dirpath) - *(p+1) =3D '\0'; - else - *p =3D '\0'; - - if (virStringListHasString(mounts, dirpath)) { - VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " - "Fixing shared FS type", dirpath, path); - *f_type =3D GFS2_MAGIC; - break; - } - } while (p !=3D dirpath); + if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) { + VIR_DEBUG("Found gluster FUSE mountpoint=3D%s for path=3D%s. " + "Fixing shared FS type", mntDir, path); + *f_type =3D GFS2_MAGIC; + } =20 ret =3D 0; cleanup: + VIR_FREE(mntType); + VIR_FREE(mntDir); endmntent(f); - VIR_FREE(mounts); - VIR_FREE(dirpath); return ret; } =20 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=3D1777,= pagesize=3D2M 0 0 none /run/user/1000 tmpfs rw,relatime,mode=3D700,uid=3D1000 0 0 host:/nfs /nfs nfs4 rw,relatime,vers=3D4.1,rsize=3D1048576,wsize=3D1048576= ,namlen=3D255,hard,proto=3Dtcp6,timeo=3D600,retrans=3D2,sec=3Dsys,clientadd= r=3D::,local_lock=3Dnone,addr=3D:: 0 0 dev /nfs/blah devtmpfs rw,nosuid,relatime,size=3D10240k,nr_inodes=3D409306= 0,mode=3D755 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", f= alse); =20 return ret !=3D 0 ? EXIT_FAILURE : EXIT_SUCCESS; } --=20 2.18.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list