All devices that we use to derive the target node name from are
parents of the scsi_target device. This allows some simplification
in sysfs_get_target_nodename().

This changes the sequence of libudev calls in some of the tests, which
need to be adapted.

Signed-off-by: Martin Wilck <[email protected]>
Reviewed-by: Benjamin Marzinski <[email protected]>
---
 libmultipath/discovery.c | 36 ++++++++++++++++++++----------------
 tests/test-lib.c         |  5 +++--
 2 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/libmultipath/discovery.c b/libmultipath/discovery.c
index 31db875..85ea1aa 100644
--- a/libmultipath/discovery.c
+++ b/libmultipath/discovery.c
@@ -325,19 +325,23 @@ static int
 sysfs_get_tgt_nodename(struct path *pp, char *node)
 {
        const char *tgtname, *value;
-       struct udev_device *parent, *tgtdev;
+       struct udev_device *parent, *tgtdev, *device, *target;
        int host, channel, tgtid = -1;
 
        if (!pp->udev)
                return 1;
-       parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
-                                                        "scsi", "scsi_device");
-       if (!parent)
+       device = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi",
+                                                              "scsi_device");
+       if (!device)
                return 1;
+       target = udev_device_get_parent_with_subsystem_devtype(device, "scsi",
+                                                              "scsi_target");
        /* Check for SAS */
-       value = udev_device_get_sysattr_value(parent, "sas_address");
+       if (!target)
+               return 1;
+       value = udev_device_get_sysattr_value(device, "sas_address");
        if (value) {
-               tgtdev = udev_device_get_parent(parent);
+               tgtdev = udev_device_get_parent(target);
                while (tgtdev) {
                        char c;
 
@@ -362,7 +366,7 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
        }
 
        /* Check for USB */
-       tgtdev = udev_device_get_parent(parent);
+       tgtdev = target;
        while (tgtdev) {
                value = udev_device_get_subsystem(tgtdev);
                if (value && !strcmp(value, "usb")) {
@@ -375,11 +379,8 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
                }
                tgtdev = udev_device_get_parent(tgtdev);
        }
-       parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, 
"scsi", "scsi_target");
-       if (!parent)
-               return 1;
        /* Check for FibreChannel */
-       tgtdev = udev_device_get_parent(parent);
+       tgtdev = udev_device_get_parent(target);
        value = udev_device_get_sysname(tgtdev);
        if (value && sscanf(value, "rport-%d:%d-%d",
                   &host, &channel, &tgtid) == 3) {
@@ -399,13 +400,15 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
                                strlcpy(node, value, NODE_NAME_SIZE);
                                udev_device_unref(tgtdev);
                                return 0;
-                       } else
+                       } else {
                                udev_device_unref(tgtdev);
+                               return 1;
+                       }
                }
        }
 
        /* Check for iSCSI */
-       parent = pp->udev;
+       parent = target;
        tgtname = NULL;
        while (parent) {
                tgtname = udev_device_get_sysname(parent);
@@ -428,13 +431,14 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
                                strlcpy(node, value, NODE_NAME_SIZE);
                                udev_device_unref(tgtdev);
                                return 0;
-                       }
-                       else
+                       } else {
                                udev_device_unref(tgtdev);
+                               return 1;
+                       }
                }
        }
        /* Check for libata */
-       parent = pp->udev;
+       parent = target;
        tgtname = NULL;
        while (parent) {
                tgtname = udev_device_get_sysname(parent);
diff --git a/tests/test-lib.c b/tests/test-lib.c
index e529711..963e289 100644
--- a/tests/test-lib.c
+++ b/tests/test-lib.c
@@ -292,12 +292,13 @@ static void mock_sysfs_pathinfo(const struct mocked_path 
*mp)
 
        /* sysfs_get_tgt_nodename */
        will_return(__wrap_udev_device_get_sysattr_value, NULL);
+       will_return(__wrap_udev_device_get_subsystem, NULL);
        will_return(__wrap_udev_device_get_parent, NULL);
        will_return(__wrap_udev_device_get_parent, NULL);
        will_return(__wrap_udev_device_get_sysname, "nofibre");
        will_return(__wrap_udev_device_get_sysname, "noiscsi");
-       will_return(__wrap_udev_device_get_parent, NULL);
        will_return(__wrap_udev_device_get_sysname, "ata25");
+       will_return(__wrap_udev_device_get_parent, NULL);
 }
 
 /*
@@ -328,7 +329,7 @@ void mock_pathinfo(int mask, const struct mocked_path *mp)
            (mask & DI_BLACKLIST && mask & DI_SYSFS))
                return;
 
-       /* path_offline */
+       /* path_sysfs_state */
        will_return(__wrap_udev_device_get_subsystem, "scsi");
        will_return(__wrap_sysfs_attr_get_value, "running");
 
-- 
2.52.0


Reply via email to