render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From fb8c44edabd188957fe3e3deb97a605f71c394b8 Mon Sep 17 00:00:00 2001
Message-Id: <fb8c44edabd188957fe3e3deb97a605f71c394b8@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Mon, 8 Mar 2021 12:57:32 +0100
Subject: [PATCH] virdevmapper: Don't cache device-mapper major
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The device mapper major is needed in virIsDevMapperDevice() which
determines whether given device is managed by device-mapper. This
number is obtained by parsing /proc/devices and then stored in a
global variable so that the file doesn't have to be parsed again.
However, as it turns out this logic is flawed - the major number
is not static and can change as it can be specified as a
parameter when loading the dm-mod module.

Unfortunately, I was not able to come up with a good solution and
thus the /proc/devices file is being parsed every time we need
the device mapper major.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
Tested-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
(cherry picked from commit 82bb167f0d15b733b23931205be3488b83cb9ec6)

Conflicts:
- src/util/virdevmapper.c: The VIR_ONCE_GLOBAL_INIT() call was
  missing a semicolon at EOL which was added there in
  af36f8a641809556ac18dcc076f996033cb2385c which isn't
  backported.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1933557
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Message-Id: <206f7453debcfd7a013317989893be8a0688045c.1615203117.git.mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/virdevmapper.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c
index ebf446d966..725de736c1 100644
--- a/src/util/virdevmapper.c
+++ b/src/util/virdevmapper.c
@@ -49,11 +49,9 @@
 
 verify(BUF_SIZE > sizeof(struct dm_ioctl));
 
-static unsigned int virDMMajor;
-
 
 static int
-virDevMapperOnceInit(void)
+virDevMapperGetMajor(unsigned int *major)
 {
     VIR_AUTOFREE(char *) buf = NULL;
     VIR_AUTOSTRINGLIST lines = NULL;
@@ -72,7 +70,7 @@ virDevMapperOnceInit(void)
 
         if (sscanf(lines[i], "%u %ms\n", &maj, &dev) == 2 &&
             STREQ(dev, DM_NAME)) {
-            virDMMajor = maj;
+            *major = maj;
             break;
         }
     }
@@ -88,9 +86,6 @@ virDevMapperOnceInit(void)
 }
 
 
-VIR_ONCE_GLOBAL_INIT(virDevMapper)
-
-
 static void *
 virDMIoctl(int controlFD, int cmd, struct dm_ioctl *dm, char **buf)
 {
@@ -318,9 +313,6 @@ virDevMapperGetTargets(const char *path,
      * consist of devices or yet another targets. If that's the
      * case, we have to stop recursion somewhere. */
 
-    if (virDevMapperInitialize() < 0)
-        return -1;
-
     if ((controlFD = virDMOpen()) < 0)
         return -1;
 
@@ -332,13 +324,14 @@ bool
 virIsDevMapperDevice(const char *dev_name)
 {
     struct stat buf;
+    unsigned int major;
 
-    if (virDevMapperInitialize() < 0)
+    if (virDevMapperGetMajor(&major) < 0)
         return false;
 
     if (!stat(dev_name, &buf) &&
         S_ISBLK(buf.st_mode) &&
-        major(buf.st_rdev) == virDMMajor)
+        major(buf.st_rdev) == major)
         return true;
 
     return false;
-- 
2.31.0