From e37f7c0555efec88ae8d0279d8cfec48eb0b312f Mon Sep 17 00:00:00 2001
From: Vojtech Trefny <vtrefny@redhat.com>
Date: Mon, 16 Oct 2017 12:42:42 +0200
Subject: [PATCH] Do not try to create file watchers for RAIDs without
 redundancy

We are trying to watch 'md/degraded' and 'md/sync_action' sysfs
files for all RAIDs but these files exist only for RAIDs with
redundancy -- we shouldn't do this for raid0, containers and linear
RAIDs.

Resolves: rhbz#1400056
(cherry picked from commit 756571efc1b0d602bca2dd4ff761dca686dc08bd)

[tblume: ported to v2.6.5]
---
 src/udiskslinuxmdraid.c       |  2 +-
 src/udiskslinuxmdraid.h       |  2 ++
 src/udiskslinuxmdraidobject.c | 25 +++++++++++++++++++++++++
 src/udiskslinuxmdraidobject.h |  1 +
 4 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/udiskslinuxmdraid.c b/src/udiskslinuxmdraid.c
index eefaef7b..283223bb 100644
--- a/src/udiskslinuxmdraid.c
+++ b/src/udiskslinuxmdraid.c
@@ -124,7 +124,7 @@ udisks_linux_mdraid_new (void)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-static gchar *
+gchar *
 read_sysfs_attr (GUdevDevice *device,
                  const gchar *attr)
 {
diff --git a/src/udiskslinuxmdraid.h b/src/udiskslinuxmdraid.h
index e8909878..da62778e 100644
--- a/src/udiskslinuxmdraid.h
+++ b/src/udiskslinuxmdraid.h
@@ -33,6 +33,8 @@ GType         udisks_linux_mdraid_get_type  (void) G_GNUC_CONST;
 UDisksMDRaid *udisks_linux_mdraid_new       (void);
 gboolean      udisks_linux_mdraid_update    (UDisksLinuxMDRaid       *mdraid,
                                              UDisksLinuxMDRaidObject *object);
+gchar        *read_sysfs_attr               (GUdevDevice             *device,
+                                             const gchar             *attr);
 
 G_END_DECLS
 
diff --git a/src/udiskslinuxmdraidobject.c b/src/udiskslinuxmdraidobject.c
index 97787b03..2414eba8 100644
--- a/src/udiskslinuxmdraidobject.c
+++ b/src/udiskslinuxmdraidobject.c
@@ -552,9 +552,18 @@ static void
 raid_device_added (UDisksLinuxMDRaidObject *object,
                    UDisksLinuxDevice       *device)
 {
+  gchar *level = NULL;
+
   g_assert (object->sync_action_source == NULL);
   g_assert (object->degraded_source == NULL);
 
+  if (!UDISKS_IS_LINUX_DEVICE (device))
+    goto out;
+
+  level = read_sysfs_attr (device->udev_device, "md/level");
+  if (level == NULL || !mdraid_has_redundancy (level))
+    goto out;
+
   /* udisks_debug ("start watching %s", g_udev_device_get_sysfs_path (device->udev_device)); */
   object->sync_action_source = watch_attr (device,
                                            "md/sync_action",
@@ -564,6 +573,9 @@ raid_device_added (UDisksLinuxMDRaidObject *object,
                                         "md/degraded",
                                         (GSourceFunc) attr_changed,
                                         object);
+
+ out:
+  g_free (level);
 }
 
 static void
@@ -681,6 +693,12 @@ udisks_linux_mdraid_object_uevent (UDisksLinuxMDRaidObject *object,
                   g_clear_object (&object->raid_device);
                   object->raid_device = g_object_ref (device);
                 }
+              else if (object->sync_action_source == NULL && object->degraded_source == NULL)
+                {
+                  /* we don't have file watchers, adding them may failed because
+                     we were unable to get raid level, let's try again */
+                  raid_device_added (object, object->raid_device);
+                }
             }
         }
     }
@@ -805,4 +823,11 @@ udisks_linux_mdraid_object_get_uuid (UDisksLinuxMDRaidObject *object)
   return object->uuid;
 }
 
+gboolean
+mdraid_has_redundancy (const gchar *raid_level)
+{
+  return raid_level != NULL &&
+         g_str_has_prefix (raid_level, "raid") &&
+         g_strcmp0 (raid_level, "raid0") != 0;
+}
 
diff --git a/src/udiskslinuxmdraidobject.h b/src/udiskslinuxmdraidobject.h
index 14d99d2a..d4d3f87f 100644
--- a/src/udiskslinuxmdraidobject.h
+++ b/src/udiskslinuxmdraidobject.h
@@ -51,6 +51,7 @@ gboolean                   udisks_linux_mdraid_object_complete_sync_job (UDisksL
                                                                          gboolean                 success,
                                                                          const gchar             *message);
 gboolean                   udisks_linux_mdraid_object_has_sync_job      (UDisksLinuxMDRaidObject *object);
+gboolean                 mdraid_has_redundancy                    (const gchar *raid_level);
 
 G_END_DECLS
 
-- 
2.13.6

