3cf992
From fb8c44edabd188957fe3e3deb97a605f71c394b8 Mon Sep 17 00:00:00 2001
3cf992
Message-Id: <fb8c44edabd188957fe3e3deb97a605f71c394b8@dist-git>
3cf992
From: Michal Privoznik <mprivozn@redhat.com>
3cf992
Date: Mon, 8 Mar 2021 12:57:32 +0100
3cf992
Subject: [PATCH] virdevmapper: Don't cache device-mapper major
3cf992
MIME-Version: 1.0
3cf992
Content-Type: text/plain; charset=UTF-8
3cf992
Content-Transfer-Encoding: 8bit
3cf992
3cf992
The device mapper major is needed in virIsDevMapperDevice() which
3cf992
determines whether given device is managed by device-mapper. This
3cf992
number is obtained by parsing /proc/devices and then stored in a
3cf992
global variable so that the file doesn't have to be parsed again.
3cf992
However, as it turns out this logic is flawed - the major number
3cf992
is not static and can change as it can be specified as a
3cf992
parameter when loading the dm-mod module.
3cf992
3cf992
Unfortunately, I was not able to come up with a good solution and
3cf992
thus the /proc/devices file is being parsed every time we need
3cf992
the device mapper major.
3cf992
3cf992
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
3cf992
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
3cf992
Reviewed-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
3cf992
Tested-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
3cf992
(cherry picked from commit 82bb167f0d15b733b23931205be3488b83cb9ec6)
3cf992
3cf992
Conflicts:
3cf992
- src/util/virdevmapper.c: The VIR_ONCE_GLOBAL_INIT() call was
3cf992
  missing a semicolon at EOL which was added there in
3cf992
  af36f8a641809556ac18dcc076f996033cb2385c which isn't
3cf992
  backported.
3cf992
3cf992
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1933557
3cf992
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
3cf992
Message-Id: <206f7453debcfd7a013317989893be8a0688045c.1615203117.git.mprivozn@redhat.com>
3cf992
Reviewed-by: Ján Tomko <jtomko@redhat.com>
3cf992
---
3cf992
 src/util/virdevmapper.c | 17 +++++------------
3cf992
 1 file changed, 5 insertions(+), 12 deletions(-)
3cf992
3cf992
diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c
3cf992
index ebf446d966..725de736c1 100644
3cf992
--- a/src/util/virdevmapper.c
3cf992
+++ b/src/util/virdevmapper.c
3cf992
@@ -49,11 +49,9 @@
3cf992
 
3cf992
 verify(BUF_SIZE > sizeof(struct dm_ioctl));
3cf992
 
3cf992
-static unsigned int virDMMajor;
3cf992
-
3cf992
 
3cf992
 static int
3cf992
-virDevMapperOnceInit(void)
3cf992
+virDevMapperGetMajor(unsigned int *major)
3cf992
 {
3cf992
     VIR_AUTOFREE(char *) buf = NULL;
3cf992
     VIR_AUTOSTRINGLIST lines = NULL;
3cf992
@@ -72,7 +70,7 @@ virDevMapperOnceInit(void)
3cf992
 
3cf992
         if (sscanf(lines[i], "%u %ms\n", &maj, &dev) == 2 &&
3cf992
             STREQ(dev, DM_NAME)) {
3cf992
-            virDMMajor = maj;
3cf992
+            *major = maj;
3cf992
             break;
3cf992
         }
3cf992
     }
3cf992
@@ -88,9 +86,6 @@ virDevMapperOnceInit(void)
3cf992
 }
3cf992
 
3cf992
 
3cf992
-VIR_ONCE_GLOBAL_INIT(virDevMapper)
3cf992
-
3cf992
-
3cf992
 static void *
3cf992
 virDMIoctl(int controlFD, int cmd, struct dm_ioctl *dm, char **buf)
3cf992
 {
3cf992
@@ -318,9 +313,6 @@ virDevMapperGetTargets(const char *path,
3cf992
      * consist of devices or yet another targets. If that's the
3cf992
      * case, we have to stop recursion somewhere. */
3cf992
 
3cf992
-    if (virDevMapperInitialize() < 0)
3cf992
-        return -1;
3cf992
-
3cf992
     if ((controlFD = virDMOpen()) < 0)
3cf992
         return -1;
3cf992
 
3cf992
@@ -332,13 +324,14 @@ bool
3cf992
 virIsDevMapperDevice(const char *dev_name)
3cf992
 {
3cf992
     struct stat buf;
3cf992
+    unsigned int major;
3cf992
 
3cf992
-    if (virDevMapperInitialize() < 0)
3cf992
+    if (virDevMapperGetMajor(&major) < 0)
3cf992
         return false;
3cf992
 
3cf992
     if (!stat(dev_name, &buf) &&
3cf992
         S_ISBLK(buf.st_mode) &&
3cf992
-        major(buf.st_rdev) == virDMMajor)
3cf992
+        major(buf.st_rdev) == major)
3cf992
         return true;
3cf992
 
3cf992
     return false;
3cf992
-- 
3cf992
2.31.0
3cf992