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

fc4a62
From 468d537b07c3c1ad72e1ee1e9651a17c72038837 Mon Sep 17 00:00:00 2001
fc4a62
From: Fedora Ninjas <parted-owner@fedoraproject.org>
fc4a62
Date: Thu, 6 Aug 2015 07:17:14 -0700
fc4a62
Subject: [PATCH 33/35] libparted: Use read only when probing devices on linux
fc4a62
 (#1245144)
fc4a62
fc4a62
When a device is opened for RW closing it can trigger other actions,
fc4a62
like udev scanning it for partition changes. Use read only for the
fc4a62
init_* methods and RW for actual changes to the device.
fc4a62
fc4a62
This adds _device_open which takes mode flags as an argument and turns
fc4a62
linux_open into a wrapper for it with RW_MODE.
fc4a62
fc4a62
_device_open_ro is added to open the device with RD_MODE and increment
fc4a62
the open_counter. This is used in the init_* functions.
fc4a62
fc4a62
_device_close is a wrapper around linux_close that decrements the
fc4a62
open_counter and is used in the init_* functions.
fc4a62
fc4a62
All of these changes are self-contained with no external API changes.
fc4a62
The only visible change in behavior is that when a new PedDevice is
fc4a62
created the device is opened in RO_MODE instead of RW_MODE.
fc4a62
fc4a62
Resolves: rhbz#1245144
fc4a62
---
fc4a62
 libparted/arch/linux.c | 61 ++++++++++++++++++++++++++++++++++++--------------
fc4a62
 1 file changed, 44 insertions(+), 17 deletions(-)
fc4a62
fc4a62
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
fc4a62
index 1d11f58..341bbbb 100644
fc4a62
--- a/libparted/arch/linux.c
fc4a62
+++ b/libparted/arch/linux.c
fc4a62
@@ -287,6 +287,9 @@ struct blkdev_ioctl_param {
fc4a62
 
fc4a62
 static char* _device_get_part_path (PedDevice* dev, int num);
fc4a62
 static int _partition_is_mounted_by_path (const char* path);
fc4a62
+static int _device_open (PedDevice* dev, int flags);
fc4a62
+static int _device_open_ro (PedDevice* dev);
fc4a62
+static int _device_close (PedDevice* dev);
fc4a62
 
fc4a62
 static int
fc4a62
 _read_fd (int fd, char **buf)
fc4a62
@@ -830,7 +833,7 @@ init_ide (PedDevice* dev)
fc4a62
         if (!_device_stat (dev, &dev_stat))
fc4a62
                 goto error;
fc4a62
 
fc4a62
-        if (!ped_device_open (dev))
fc4a62
+        if (!_device_open_ro (dev))
fc4a62
                 goto error;
fc4a62
 
fc4a62
         if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) {
fc4a62
@@ -899,11 +902,11 @@ init_ide (PedDevice* dev)
fc4a62
         if (!_device_probe_geometry (dev))
fc4a62
                 goto error_close_dev;
fc4a62
 
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
         return 1;
fc4a62
 
fc4a62
 error_close_dev:
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
 error:
fc4a62
         return 0;
fc4a62
 }
fc4a62
@@ -1036,7 +1039,7 @@ init_scsi (PedDevice* dev)
fc4a62
         char* vendor;
fc4a62
         char* product;
fc4a62
 
fc4a62
-        if (!ped_device_open (dev))
fc4a62
+        if (!_device_open_ro (dev))
fc4a62
                 goto error;
fc4a62
 
fc4a62
         if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) {
fc4a62
@@ -1050,7 +1053,7 @@ init_scsi (PedDevice* dev)
fc4a62
                         goto error_close_dev;
fc4a62
                 if (!_device_probe_geometry (dev))
fc4a62
                         goto error_close_dev;
fc4a62
-                ped_device_close (dev);
fc4a62
+                _device_close (dev);
fc4a62
                 return 1;
fc4a62
         }
fc4a62
 
fc4a62
@@ -1072,11 +1075,11 @@ init_scsi (PedDevice* dev)
fc4a62
         if (!_device_probe_geometry (dev))
fc4a62
                 goto error_close_dev;
fc4a62
 
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
         return 1;
fc4a62
 
fc4a62
 error_close_dev:
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
 error:
fc4a62
         return 0;
fc4a62
 }
fc4a62
@@ -1088,7 +1091,7 @@ init_file (PedDevice* dev)
fc4a62
 
fc4a62
         if (!_device_stat (dev, &dev_stat))
fc4a62
                 goto error;
fc4a62
-        if (!ped_device_open (dev))
fc4a62
+        if (!_device_open_ro (dev))
fc4a62
                 goto error;
fc4a62
 
fc4a62
         dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
fc4a62
@@ -1115,7 +1118,7 @@ init_file (PedDevice* dev)
fc4a62
                 goto error_close_dev;
fc4a62
         }
fc4a62
 
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
 
fc4a62
         dev->bios_geom.cylinders = dev->length / 4 / 32;
fc4a62
         dev->bios_geom.heads = 4;
fc4a62
@@ -1126,7 +1129,7 @@ init_file (PedDevice* dev)
fc4a62
         return 1;
fc4a62
 
fc4a62
 error_close_dev:
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
 error:
fc4a62
         return 0;
fc4a62
 }
fc4a62
@@ -1142,7 +1145,7 @@ init_dasd (PedDevice* dev, const char* model_name)
fc4a62
         if (!_device_stat (dev, &dev_stat))
fc4a62
                 goto error;
fc4a62
 
fc4a62
-        if (!ped_device_open (dev))
fc4a62
+        if (!_device_open_ro (dev))
fc4a62
                 goto error;
fc4a62
 
fc4a62
         LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
fc4a62
@@ -1182,11 +1185,11 @@ init_dasd (PedDevice* dev, const char* model_name)
fc4a62
 
fc4a62
         dev->model = strdup (model_name);
fc4a62
 
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
         return 1;
fc4a62
 
fc4a62
 error_close_dev:
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
 error:
fc4a62
         return 0;
fc4a62
 }
fc4a62
@@ -1201,7 +1204,7 @@ init_generic (PedDevice* dev, const char* model_name)
fc4a62
         if (!_device_stat (dev, &dev_stat))
fc4a62
                 goto error;
fc4a62
 
fc4a62
-        if (!ped_device_open (dev))
fc4a62
+        if (!_device_open_ro (dev))
fc4a62
                 goto error;
fc4a62
 
fc4a62
         ped_exception_fetch_all ();
fc4a62
@@ -1249,11 +1252,11 @@ init_generic (PedDevice* dev, const char* model_name)
fc4a62
 
fc4a62
         dev->model = strdup (model_name);
fc4a62
 
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
         return 1;
fc4a62
 
fc4a62
 error_close_dev:
fc4a62
-        ped_device_close (dev);
fc4a62
+        _device_close (dev);
fc4a62
 error:
fc4a62
         return 0;
fc4a62
 }
fc4a62
@@ -1543,12 +1546,27 @@ retry:
fc4a62
 }
fc4a62
 
fc4a62
 static int
fc4a62
+_device_open_ro (PedDevice* dev)
fc4a62
+{
fc4a62
+    int rc = _device_open (dev, RD_MODE);
fc4a62
+    if (rc)
fc4a62
+        dev->open_count++;
fc4a62
+    return rc;
fc4a62
+}
fc4a62
+
fc4a62
+static int
fc4a62
 linux_open (PedDevice* dev)
fc4a62
 {
fc4a62
+    return _device_open (dev, RW_MODE);
fc4a62
+}
fc4a62
+
fc4a62
+static int
fc4a62
+_device_open (PedDevice* dev, int flags)
fc4a62
+{
fc4a62
         LinuxSpecific*  arch_specific = LINUX_SPECIFIC (dev);
fc4a62
 
fc4a62
 retry:
fc4a62
-        arch_specific->fd = open (dev->path, RW_MODE);
fc4a62
+        arch_specific->fd = open (dev->path, flags);
fc4a62
 
fc4a62
         if (arch_specific->fd == -1) {
fc4a62
                 char*   rw_error_msg = strerror (errno);
fc4a62
@@ -1617,6 +1635,15 @@ linux_refresh_close (PedDevice* dev)
fc4a62
         return 1;
fc4a62
 }
fc4a62
 
fc4a62
+static int
fc4a62
+_device_close (PedDevice* dev)
fc4a62
+{
fc4a62
+    int rc = linux_close (dev);
fc4a62
+    if (dev->open_count > 0)
fc4a62
+        dev->open_count--;
fc4a62
+    return rc;
fc4a62
+}
fc4a62
+
fc4a62
 #if SIZEOF_OFF_T < 8
fc4a62
 
fc4a62
 static _syscall5(int,_llseek,
fc4a62
-- 
fc4a62
2.4.3
fc4a62