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