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

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