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