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

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