7b551b
From bc406e0919e72b1f108ded76a380212b179c8572 Mon Sep 17 00:00:00 2001
7b551b
Message-Id: <bc406e0919e72b1f108ded76a380212b179c8572@dist-git>
7b551b
From: Michal Privoznik <mprivozn@redhat.com>
7b551b
Date: Tue, 25 Aug 2020 17:16:05 +0200
7b551b
Subject: [PATCH] virdevmapper: Don't cache device-mapper major
7b551b
7b551b
The device mapper major is needed in virIsDevMapperDevice() which
7b551b
determines whether given device is managed by device-mapper. This
7b551b
number is obtained by parsing /proc/devices and then stored in a
7b551b
global variable so that the file doesn't have to be parsed again.
7b551b
However, as it turns out this logic is flawed - the major number
7b551b
is not static and can change as it can be specified as a
7b551b
parameter when loading the dm-mod module.
7b551b
7b551b
Unfortunately, I was not able to come up with a good solution and
7b551b
thus the /proc/devices file is being parsed every time we need
7b551b
the device mapper major.
7b551b
7b551b
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
7b551b
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
7b551b
Reviewed-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
7b551b
Tested-by: Christian Ehrhardt <christian.ehrhardt@canonical.com>
7b551b
(cherry picked from commit 82bb167f0d15b733b23931205be3488b83cb9ec6)
7b551b
7b551b
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1860421
7b551b
7b551b
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
7b551b
Message-Id: <64b6d6124918bc8cfe220464752eac93cd6d6734.1598364552.git.mprivozn@redhat.com>
7b551b
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
7b551b
---
7b551b
 src/util/virdevmapper.c | 17 +++++------------
7b551b
 1 file changed, 5 insertions(+), 12 deletions(-)
7b551b
7b551b
diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c
7b551b
index a471504176..b43dbefa9a 100644
7b551b
--- a/src/util/virdevmapper.c
7b551b
+++ b/src/util/virdevmapper.c
7b551b
@@ -46,11 +46,9 @@
7b551b
 
7b551b
 G_STATIC_ASSERT(BUF_SIZE > sizeof(struct dm_ioctl));
7b551b
 
7b551b
-static unsigned int virDMMajor;
7b551b
-
7b551b
 
7b551b
 static int
7b551b
-virDevMapperOnceInit(void)
7b551b
+virDevMapperGetMajor(unsigned int *major)
7b551b
 {
7b551b
     g_autofree char *buf = NULL;
7b551b
     VIR_AUTOSTRINGLIST lines = NULL;
7b551b
@@ -69,7 +67,7 @@ virDevMapperOnceInit(void)
7b551b
 
7b551b
         if (sscanf(lines[i], "%u %ms\n", &maj, &dev) == 2 &&
7b551b
             STREQ(dev, DM_NAME)) {
7b551b
-            virDMMajor = maj;
7b551b
+            *major = maj;
7b551b
             break;
7b551b
         }
7b551b
     }
7b551b
@@ -85,9 +83,6 @@ virDevMapperOnceInit(void)
7b551b
 }
7b551b
 
7b551b
 
7b551b
-VIR_ONCE_GLOBAL_INIT(virDevMapper);
7b551b
-
7b551b
-
7b551b
 static void *
7b551b
 virDMIoctl(int controlFD, int cmd, struct dm_ioctl *dm, char **buf)
7b551b
 {
7b551b
@@ -305,9 +300,6 @@ virDevMapperGetTargets(const char *path,
7b551b
      * consist of devices or yet another targets. If that's the
7b551b
      * case, we have to stop recursion somewhere. */
7b551b
 
7b551b
-    if (virDevMapperInitialize() < 0)
7b551b
-        return -1;
7b551b
-
7b551b
     if ((controlFD = virDMOpen()) < 0)
7b551b
         return -1;
7b551b
 
7b551b
@@ -319,13 +311,14 @@ bool
7b551b
 virIsDevMapperDevice(const char *dev_name)
7b551b
 {
7b551b
     struct stat buf;
7b551b
+    unsigned int major;
7b551b
 
7b551b
-    if (virDevMapperInitialize() < 0)
7b551b
+    if (virDevMapperGetMajor(&major) < 0)
7b551b
         return false;
7b551b
 
7b551b
     if (!stat(dev_name, &buf) &&
7b551b
         S_ISBLK(buf.st_mode) &&
7b551b
-        major(buf.st_rdev) == virDMMajor)
7b551b
+        major(buf.st_rdev) == major)
7b551b
         return true;
7b551b
 
7b551b
     return false;
7b551b
-- 
7b551b
2.28.0
7b551b