Blame SOURCES/0019-libparted-Use-read-only-when-probing-devices-on-linu.patch

7fd79c
From d66b197b227e1fbd4a72f002cb8b8a7ee9461062 Mon Sep 17 00:00:00 2001
7fd79c
From: "Brian C. Lane" <bcl@redhat.com>
7fd79c
Date: Thu, 6 Aug 2015 07:17:14 -0700
7fd79c
Subject: [PATCH 19/20] libparted: Use read only when probing devices on linux
7fd79c
 (#1245144)
7fd79c
7fd79c
When a device is opened for RW closing it can trigger other actions,
7fd79c
like udev scanning it for partition changes. Use read only for the
7fd79c
init_* methods and RW for actual changes to the device.
7fd79c
7fd79c
This adds _device_open which takes mode flags as an argument and turns
7fd79c
linux_open into a wrapper for it with RW_MODE.
7fd79c
7fd79c
_device_open_ro is added to open the device with RD_MODE and increment
7fd79c
the open_counter. This is used in the init_* functions.
7fd79c
7fd79c
_device_close is a wrapper around linux_close that decrements the
7fd79c
open_counter and is used in the init_* functions.
7fd79c
7fd79c
All of these changes are self-contained with no external API changes.
7fd79c
The only visible change in behavior is that when a new PedDevice is
7fd79c
created the device is opened in RO_MODE instead of RW_MODE.
7fd79c
7fd79c
Resolves: rhbz#1245144
7fd79c
(cherry picked from commit 0e169215efcdb33d588ddc2267467593bbf717c9)
7fd79c
---
7fd79c
 libparted/arch/linux.c | 62 +++++++++++++++++++++++++++++++++++---------------
7fd79c
 1 file changed, 44 insertions(+), 18 deletions(-)
7fd79c
7fd79c
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
7fd79c
index b68130b..3934a5b 100644
7fd79c
--- a/libparted/arch/linux.c
7fd79c
+++ b/libparted/arch/linux.c
7fd79c
@@ -291,7 +291,9 @@ struct blkdev_ioctl_param {
7fd79c
 static char* _device_get_part_path (PedDevice const *dev, int num);
7fd79c
 static int _partition_is_mounted_by_path (const char* path);
7fd79c
 static unsigned int _device_get_partition_range(PedDevice const* dev);
7fd79c
-
7fd79c
+static int _device_open (PedDevice* dev, int flags);
7fd79c
+static int _device_open_ro (PedDevice* dev);
7fd79c
+static int _device_close (PedDevice* dev);
7fd79c
 
7fd79c
 static int
7fd79c
 _read_fd (int fd, char **buf)
7fd79c
@@ -910,7 +912,7 @@ init_ide (PedDevice* dev)
7fd79c
         if (!_device_stat (dev, &dev_stat))
7fd79c
                 goto error;
7fd79c
 
7fd79c
-        if (!ped_device_open (dev))
7fd79c
+        if (!_device_open_ro (dev))
7fd79c
                 goto error;
7fd79c
 
7fd79c
         if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) {
7fd79c
@@ -979,11 +981,11 @@ init_ide (PedDevice* dev)
7fd79c
         if (!_device_probe_geometry (dev))
7fd79c
                 goto error_close_dev;
7fd79c
 
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
         return 1;
7fd79c
 
7fd79c
 error_close_dev:
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
 error:
7fd79c
         return 0;
7fd79c
 }
7fd79c
@@ -1116,7 +1118,7 @@ init_scsi (PedDevice* dev)
7fd79c
         char* vendor;
7fd79c
         char* product;
7fd79c
 
7fd79c
-        if (!ped_device_open (dev))
7fd79c
+        if (!_device_open_ro (dev))
7fd79c
                 goto error;
7fd79c
 
7fd79c
         if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) {
7fd79c
@@ -1130,7 +1132,7 @@ init_scsi (PedDevice* dev)
7fd79c
                         goto error_close_dev;
7fd79c
                 if (!_device_probe_geometry (dev))
7fd79c
                         goto error_close_dev;
7fd79c
-                ped_device_close (dev);
7fd79c
+                _device_close (dev);
7fd79c
                 return 1;
7fd79c
         }
7fd79c
 
7fd79c
@@ -1152,11 +1154,11 @@ init_scsi (PedDevice* dev)
7fd79c
         if (!_device_probe_geometry (dev))
7fd79c
                 goto error_close_dev;
7fd79c
 
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
         return 1;
7fd79c
 
7fd79c
 error_close_dev:
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
 error:
7fd79c
         return 0;
7fd79c
 }
7fd79c
@@ -1168,7 +1170,7 @@ init_file (PedDevice* dev)
7fd79c
 
7fd79c
         if (!_device_stat (dev, &dev_stat))
7fd79c
                 goto error;
7fd79c
-        if (!ped_device_open (dev))
7fd79c
+        if (!_device_open_ro (dev))
7fd79c
                 goto error;
7fd79c
 
7fd79c
         dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
7fd79c
@@ -1195,7 +1197,7 @@ init_file (PedDevice* dev)
7fd79c
                 goto error_close_dev;
7fd79c
         }
7fd79c
 
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
 
7fd79c
         dev->bios_geom.cylinders = dev->length / 4 / 32;
7fd79c
         dev->bios_geom.heads = 4;
7fd79c
@@ -1206,7 +1208,7 @@ init_file (PedDevice* dev)
7fd79c
         return 1;
7fd79c
 
7fd79c
 error_close_dev:
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
 error:
7fd79c
         return 0;
7fd79c
 }
7fd79c
@@ -1222,7 +1224,7 @@ init_dasd (PedDevice* dev, const char* model_name)
7fd79c
         if (!_device_stat (dev, &dev_stat))
7fd79c
                 goto error;
7fd79c
 
7fd79c
-        if (!ped_device_open (dev))
7fd79c
+        if (!_device_open_ro (dev))
7fd79c
                 goto error;
7fd79c
 
7fd79c
         LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
7fd79c
@@ -1262,11 +1264,11 @@ init_dasd (PedDevice* dev, const char* model_name)
7fd79c
 
7fd79c
         dev->model = strdup (model_name);
7fd79c
 
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
         return 1;
7fd79c
 
7fd79c
 error_close_dev:
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
 error:
7fd79c
         return 0;
7fd79c
 }
7fd79c
@@ -1281,7 +1283,7 @@ init_generic (PedDevice* dev, const char* model_name)
7fd79c
         if (!_device_stat (dev, &dev_stat))
7fd79c
                 goto error;
7fd79c
 
7fd79c
-        if (!ped_device_open (dev))
7fd79c
+        if (!_device_open_ro (dev))
7fd79c
                 goto error;
7fd79c
 
7fd79c
         ped_exception_fetch_all ();
7fd79c
@@ -1329,11 +1331,11 @@ init_generic (PedDevice* dev, const char* model_name)
7fd79c
 
7fd79c
         dev->model = strdup (model_name);
7fd79c
 
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
         return 1;
7fd79c
 
7fd79c
 error_close_dev:
7fd79c
-        ped_device_close (dev);
7fd79c
+        _device_close (dev);
7fd79c
 error:
7fd79c
         return 0;
7fd79c
 }
7fd79c
@@ -1620,12 +1622,27 @@ retry:
7fd79c
 }
7fd79c
 
7fd79c
 static int
7fd79c
+_device_open_ro (PedDevice* dev)
7fd79c
+{
7fd79c
+    int rc = _device_open (dev, RD_MODE);
7fd79c
+    if (rc)
7fd79c
+        dev->open_count++;
7fd79c
+    return rc;
7fd79c
+}
7fd79c
+
7fd79c
+static int
7fd79c
 linux_open (PedDevice* dev)
7fd79c
 {
7fd79c
+    return _device_open (dev, RW_MODE);
7fd79c
+}
7fd79c
+
7fd79c
+static int
7fd79c
+_device_open (PedDevice* dev, int flags)
7fd79c
+{
7fd79c
         LinuxSpecific*  arch_specific = LINUX_SPECIFIC (dev);
7fd79c
 
7fd79c
 retry:
7fd79c
-        arch_specific->fd = open (dev->path, RW_MODE);
7fd79c
+        arch_specific->fd = open (dev->path, flags);
7fd79c
 
7fd79c
         if (arch_specific->fd == -1) {
7fd79c
                 char*   rw_error_msg = strerror (errno);
7fd79c
@@ -1694,6 +1711,15 @@ linux_refresh_close (PedDevice* dev)
7fd79c
         return 1;
7fd79c
 }
7fd79c
 
7fd79c
+static int
7fd79c
+_device_close (PedDevice* dev)
7fd79c
+{
7fd79c
+    int rc = linux_close (dev);
7fd79c
+    if (dev->open_count > 0)
7fd79c
+        dev->open_count--;
7fd79c
+    return rc;
7fd79c
+}
7fd79c
+
7fd79c
 #if SIZEOF_OFF_T < 8
7fd79c
 
7fd79c
 static _syscall5(int,_llseek,
7fd79c
-- 
7fd79c
2.4.3
7fd79c