8a28aa4
From 1edbfcdb4a973120048b5c04e0eb92b05ad3cbd2 Mon Sep 17 00:00:00 2001
8a28aa4
From: Vratislav Podzimek <vpodzime@redhat.com>
8a28aa4
Date: Wed, 26 Oct 2016 16:13:58 +0200
8a28aa4
Subject: [PATCH] Open the device file as RDWR when committing parts
8a28aa4
8a28aa4
We block udev from generating events for the device while we are between
8a28aa4
committing changes to disk and informing kernel about the changes. If we close
8a28aa4
the file descriptor later, no udev changes are generated either unless we open
8a28aa4
is as RDWR. Here's the difference:
8a28aa4
8a28aa4
RDONLY:
8a28aa4
  # udevadm monitor
8a28aa4
  monitor will print the received events for:
8a28aa4
  UDEV - the event which udev sends out after rule processing
8a28aa4
  KERNEL - the kernel uevent
8a28aa4
8a28aa4
  KERNEL[68221.911864] change   /devices/virtual/block/loop0 (block)
8a28aa4
  KERNEL[68221.913442] change   /devices/virtual/block/loop0 (block)
8a28aa4
  KERNEL[68221.913794] add      /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
  KERNEL[68221.915019] change   /devices/virtual/block/loop0 (block)
8a28aa4
  KERNEL[68221.915217] change   /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
  UDEV  [68221.941293] change   /devices/virtual/block/loop0 (block)
8a28aa4
  UDEV  [68221.954214] change   /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
8a28aa4
RDWR:
8a28aa4
  # udevadm monitor
8a28aa4
  monitor will print the received events for:
8a28aa4
  UDEV - the event which udev sends out after rule processing
8a28aa4
  KERNEL - the kernel uevent
8a28aa4
8a28aa4
  KERNEL[68161.533114] change   /devices/virtual/block/loop0 (block)
8a28aa4
  KERNEL[68161.533165] change   /devices/virtual/block/loop0 (block)
8a28aa4
  UDEV  [68161.533219] change   /devices/virtual/block/loop0 (block)
8a28aa4
  KERNEL[68161.533262] add      /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
  KERNEL[68161.533292] change   /devices/virtual/block/loop0 (block)
8a28aa4
  KERNEL[68161.533326] change   /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
  UDEV  [68161.539255] add      /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
  UDEV  [68161.560215] change   /devices/virtual/block/loop0 (block)
8a28aa4
  UDEV  [68161.572345] change   /devices/virtual/block/loop0/loop0p1 (block)
8a28aa4
8a28aa4
As seen above, there's no 'add' udev event in case we open the device file as
8a28aa4
RDONLY.
8a28aa4
8a28aa4
(cherry picked from commit 44fdb3953eb05ff4f43a576f15d508928dca3eb7)
8a28aa4
Signed-off-by: Vratislav Podzimek <vpodzime@redhat.com>
8a28aa4
---
8a28aa4
 src/plugins/part.c | 5 ++++-
8a28aa4
 1 file changed, 4 insertions(+), 1 deletion(-)
8a28aa4
8a28aa4
diff --git a/src/plugins/part.c b/src/plugins/part.c
8a28aa4
index d5239e3..0f8e678 100644
8a28aa4
--- a/src/plugins/part.c
8a28aa4
+++ b/src/plugins/part.c
8a28aa4
@@ -157,7 +157,7 @@ static gboolean disk_commit (PedDisk *disk, gchar *path, GError **error) {
8a28aa4
     /* XXX: try to grab an exclusive lock for the device so that udev doesn't
8a28aa4
        trigger events for it in between the two operations we need to perform
8a28aa4
        (see below) */
8a28aa4
-    dev_fd = open (disk->dev->path, O_RDONLY|O_CLOEXEC);
8a28aa4
+    dev_fd = open (disk->dev->path, O_RDWR|O_CLOEXEC);
8a28aa4
     if (dev_fd >= 0)
8a28aa4
         /* if this fails, we can do no better anyway, so just ignore the return
8a28aa4
            value */
8a28aa4
@@ -167,6 +167,7 @@ static gboolean disk_commit (PedDisk *disk, gchar *path, GError **error) {
8a28aa4
     if (ret == 0) {
8a28aa4
         set_parted_error (error, BD_PART_ERROR_FAIL);
8a28aa4
         g_prefix_error (error, "Failed to commit changes to device '%s'", path);
8a28aa4
+        flock (dev_fd, LOCK_UN);
8a28aa4
         close (dev_fd);
8a28aa4
         return FALSE;
8a28aa4
     }
8a28aa4
@@ -175,10 +176,12 @@ static gboolean disk_commit (PedDisk *disk, gchar *path, GError **error) {
8a28aa4
     if (ret == 0) {
8a28aa4
         set_parted_error (error, BD_PART_ERROR_FAIL);
8a28aa4
         g_prefix_error (error, "Failed to inform OS about changes on the '%s' device", path);
8a28aa4
+        flock (dev_fd, LOCK_UN);
8a28aa4
         close (dev_fd);
8a28aa4
         return FALSE;
8a28aa4
     }
8a28aa4
 
8a28aa4
+    flock (dev_fd, LOCK_UN);
8a28aa4
     close (dev_fd);
8a28aa4
     return TRUE;
8a28aa4
 }
8a28aa4
-- 
8a28aa4
2.7.4
8a28aa4