From: Petr Uzel <petr.uzel@suse.cz>
Date: Thu, 26 May 2016 09:28:21 +0200
Subject: libparted: open the device RO and lazily switch to RW only if
 necessary
References: bsc#979275
Patch-mainline: not yet

Avoid useless udev events triggered by opening the device RW
for purely read-only operations.

Author: Michael Matz <matz@suse.de>
[sparschauer: Use _flush_cache() also for reads, don't switch to RW
 in _flush_cache() to avoid a possible recursion (bnc#989751), open
 partitions in _flush_cache() O_RDONLY instead of O_WRONLY]
Signed-off-by: Sebastian Parschauer <sparschauer@suse.de>
---
 libparted/arch/linux.c  |   32 ++++++++++++++++++++++++++++++--
 libparted/arch/linux.h  |    1 +
 libparted/labels/dasd.c |    4 ++++
 3 files changed, 35 insertions(+), 2 deletions(-)

--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -296,6 +296,9 @@ static unsigned int _device_get_partitio
 static int _device_open (PedDevice* dev, int flags);
 static int _device_open_ro (PedDevice* dev);
 static int _device_close (PedDevice* dev);
+static int linux_close (PedDevice* dev);
+static void _flush_cache (PedDevice* dev);
+
 
 static int
 _read_fd (int fd, char **buf)
@@ -443,6 +446,22 @@ _is_virtblk_major (int major)
         return _major_type_in_devices (major, "virtblk");
 }
 
+static void
+_ensure_read_write (PedDevice *dev)
+{
+       PED_ASSERT (dev != NULL);
+       LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
+
+       if (arch_specific->rw)
+              return;
+
+       if (!linux_close(dev))
+              return;
+
+       if (!_device_open (dev, RW_MODE))
+	      return;
+}
+
 #ifdef ENABLE_DEVICE_MAPPER
 static int
 _dm_task_run_wait (struct dm_task *task, uint32_t cookie)
@@ -1608,7 +1627,7 @@ _flush_cache (PedDevice* dev)
                 if (!name)
                         break;
                 if (!_partition_is_mounted_by_path (name)) {
-                        fd = open (name, WR_MODE, 0);
+                        fd = open (name, RD_MODE, 0);
                         if (fd > 0) {
                                 ioctl (fd, BLKFLSBUF);
 retry:
@@ -1639,7 +1658,7 @@ _device_open_ro (PedDevice* dev)
 static int
 linux_open (PedDevice* dev)
 {
-    return _device_open (dev, RW_MODE);
+    return _device_open (dev, RD_MODE);
 }
 
 static int
@@ -1677,6 +1696,10 @@ retry:
                 }
         } else {
                 dev->read_only = 0;
+		if (flags == WR_MODE || flags == RW_MODE)
+			arch_specific->rw = 1;
+		else
+			arch_specific->rw = 0;
         }
 
         _flush_cache (dev);
@@ -1912,6 +1935,7 @@ _write_lastoddsector (PedDevice* dev, co
 
         PED_ASSERT(dev != NULL);
         PED_ASSERT(buffer != NULL);
+        _ensure_read_write (dev);
 
         arch_specific = LINUX_SPECIFIC (dev);
 
@@ -1960,6 +1984,9 @@ linux_write (PedDevice* dev, const void*
                         return 1;
         }
 
+        _ensure_read_write (dev);
+        if (count == 0)
+                return 1;  /* We just ensured the device is open RW (see call from dasd_write()) */
         if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
                 /* Kludge.  This is necessary to read/write the last
                    block of an odd-sized disk, until Linux 2.5.x kernel fixes.
@@ -2481,6 +2508,7 @@ _blkpg_part_command (PedDevice* dev, str
         LinuxSpecific*          arch_specific = LINUX_SPECIFIC (dev);
         struct blkpg_ioctl_arg  ioctl_arg;
 
+        _ensure_read_write (dev);
         ioctl_arg.op = op;
         ioctl_arg.flags = 0;
         ioctl_arg.datalen = sizeof (struct blkpg_partition);
--- a/libparted/arch/linux.h
+++ b/libparted/arch/linux.h
@@ -30,6 +30,7 @@ struct _LinuxSpecific {
 	int	fd;
 	int	major;
 	int	minor;
+	int	rw;
 	char*	dmtype;         /**< device map target type */
 #if defined __s390__ || defined __s390x__
 	unsigned int real_sector_size;
--- a/libparted/labels/dasd.c
+++ b/libparted/labels/dasd.c
@@ -635,6 +635,10 @@ dasd_write (const PedDisk* disk)
 	if (disk_specific->format_type == 1)
 		return 1;
 
+	/* Ensure the device is open RW */
+	char dummy[10]; /* to avoid assertion in ped_device_write */
+	ped_device_write(disk->dev, &dummy, 0, 0);
+
 	/* initialize the anchor */
 	fdasd_initialize_anchor(&anchor);
 	if (fdasd_get_geometry(disk->dev, &anchor, arch_specific->fd) == 0)
