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

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