Subject: bug#15356: [PATCH 03/19] libparted: use dm_udev_wait (#698121)

From: "Brian C. Lane" <[email-masked]>
Date: Wed, 11 Sep 2013 12:24:53 -0700

From: "Brian C. Lane" <[email-masked]>

This is based on Peter Rajnoha's patch to use dm_udev_wait to
synchronize with udev.

This requires libdevmapper v1.02.39 and higher.

mailing list thread:
https://lists.gnu.org/archive/html/bug-parted/2010-09/msg00007.html

* libparted/arch/linux.c (_dm_task_run_wait): New function
(_is_dm_major): Add cookie and change call to _dm_task_run_wait
(_is_dmraid_device): Same
(_dm_is_part): Same
(dm_canonical_path): Same
(_dm_remove_partition): Same
(_dm_get_partition_start_and_length): Same
(_dm_add_partition): Same
(linux_new): Enable udev sync support
---
 libparted/arch/linux.c | 52 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 2bb8774..1c5faa5 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -438,6 +438,17 @@ _is_virtblk_major (int major)
 
 #ifdef ENABLE_DEVICE_MAPPER
 static int
+_dm_task_run_wait (struct dm_task *task, uint32_t cookie)
+{
+    int rc = 0;
+
+    rc = dm_task_run (task);
+    dm_udev_wait (cookie);
+
+    return rc;
+}
+
+static int
 _is_dm_major (int major)
 {
         return _major_type_in_devices (major, "device-mapper");
@@ -466,7 +477,7 @@ _dm_maptype (PedDevice *dev)
 
         dm_task_no_open_count(dmt);
 
-        if (!dm_task_run(dmt))
+        if (!dm_task_run (dmt))
                 goto bad;
 
         dm_get_next_target(dmt, NULL, &start, &length, &target_type, &params);
@@ -519,7 +530,7 @@ _dm_is_part (const char *path)
                 return 0;
 
         dm_task_set_name(task, path);
-        if (!dm_task_run(task))
+        if (!dm_task_run (task))
                 goto err;
 
         struct dm_info *info = alloca (sizeof *info);
@@ -1398,6 +1409,10 @@ linux_new (const char* path)
         dev->dirty = 0;
         dev->boot_dirty = 0;
 
+#ifdef ENABLE_DEVICE_MAPPER
+        dm_udev_set_sync_support(1);
+#endif
+
         if (!_device_probe_type (dev))
                 goto error_free_arch_specific;
 
@@ -2316,7 +2331,7 @@ dm_canonical_path (PedDevice const *dev)
         if (!dm_task_set_major_minor (task, arch_specific->major,
                                       arch_specific->minor, 0))
                 goto err;
-        if (!dm_task_run(task))
+        if (!dm_task_run (task))
                 goto err;
         char *dev_name = zasprintf ("/dev/mapper/%s", dm_task_get_name (task));
         if (dev_name == NULL)
@@ -2637,31 +2652,29 @@ _device_get_partition_range(PedDevice const* dev)
 static int
 _dm_remove_partition(PedDisk* disk, int partno)
 {
-        int             rc;
+        int             rc = 0;
+        uint32_t        cookie = 0;
         char            *part_name = _device_get_part_path (disk->dev, partno);
 
         int fd = open (part_name, O_RDONLY | O_EXCL);
         if (fd == -1) {
                 if (errno == ENOENT)
                         errno = ENXIO; /* nothing to remove, device already doesn't exist */
-                free (part_name);
-                return 0;
+                goto err;
         }
         close (fd);
         struct dm_task *task = dm_task_create(DM_DEVICE_REMOVE);
-        if (!task) {
-                free (part_name);
-                return 0;
-        }
+        if (!task)
+                goto err;
         dm_task_set_name (task, part_name);
-        rc = dm_task_run(task);
+        if (!dm_task_set_cookie (task, &cookie, 0))
+                goto err;
+        rc = _dm_task_run_wait (task, cookie);
         dm_task_update_nodes();
         dm_task_destroy(task);
+err:
         free (part_name);
-        if (!rc)
-                return 0;
-
-        return 1;
+        return rc;
 }
 
 static bool
@@ -2677,7 +2690,7 @@ _dm_get_partition_start_and_length(PedPartition const *part,
         char *path = _device_get_part_path (part->disk->dev, part->num);
         PED_ASSERT(path);
         dm_task_set_name(task, path);
-        if (!dm_task_run(task))
+        if (!dm_task_run (task))
                 goto err;
 
         int major, minor;
@@ -2700,6 +2713,7 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
         LinuxSpecific*  arch_specific = LINUX_SPECIFIC (disk->dev);
         char *params = NULL;
         char *vol_name = NULL;
+        uint32_t cookie = 0;
 
         /* Get map name from devicemapper */
         struct dm_task *task = dm_task_create (DM_DEVICE_INFO);
@@ -2710,7 +2724,7 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
                                       arch_specific->minor, 0))
                 goto err;
 
-        if (!dm_task_run(task))
+        if (!dm_task_run (task))
                 goto err;
 
         const char *dev_name = dm_task_get_name (task);
@@ -2736,7 +2750,9 @@ _dm_add_partition (PedDisk* disk, const PedPartition* part)
         dm_task_set_name (task, vol_name);
         dm_task_add_target (task, 0, part->geom.length,
                 "linear", params);
-        if (dm_task_run (task)) {
+        if (!dm_task_set_cookie (task, &cookie, 0))
+                goto err;
+        if (_dm_task_run_wait (task, cookie)) {
                 dm_task_update_nodes ();
                 dm_task_destroy (task);
                 free (params);
-- 
1.8.3.1