Blame 0057-Try-even-harder-to-find-disk-device-symlinks-in-sysf.patch

38cfe28
From c9c1424d0e09bf33b747d37c43177c63367b1290 Mon Sep 17 00:00:00 2001
38cfe28
From: Peter Jones <pjones@redhat.com>
38cfe28
Date: Fri, 11 Oct 2019 14:20:54 -0400
38cfe28
Subject: [PATCH 57/86] Try even harder to find disk device symlinks in sysfs.
38cfe28
38cfe28
Today's realization is that the thing encoded into the structure of
38cfe28
sysfs is, in the best case, the dependency graph of the makefile targets
38cfe28
to build a device driver.
38cfe28
38cfe28
In the case of nvme-fabric, or really wherever the kernel has
38cfe28
class_create() and device_create() in the same function, there's an
38cfe28
extra level of indirection.
38cfe28
38cfe28
Anyway, in this patch we stop pretending sysfs isn't completely absurd,
38cfe28
and just try adding "/device" in the middle of the driver symlink path,
38cfe28
until we actually either get ENOENT on the device symlink or find a
38cfe28
device symlink that actually has a driver symlink under it.
38cfe28
38cfe28
Signed-off-by: Peter Jones <pjones@redhat.com>
38cfe28
---
38cfe28
 src/linux-nvme.c | 13 ++++-----
38cfe28
 src/linux.c      | 46 +++++++++++++++++--------------
38cfe28
 src/linux.h      | 71 +++++++++++++++++++++++++++++++++++++++++++++++-
38cfe28
 3 files changed, 102 insertions(+), 28 deletions(-)
38cfe28
38cfe28
diff --git a/src/linux-nvme.c b/src/linux-nvme.c
38cfe28
index b83c631119a..5d69a33c715 100644
38cfe28
--- a/src/linux-nvme.c
38cfe28
+++ b/src/linux-nvme.c
38cfe28
@@ -89,13 +89,12 @@ parse_nvme(struct device *dev, const char *path, const char *root UNUSED)
38cfe28
 	/*
38cfe28
 	 * now fish the eui out of sysfs is there is one...
38cfe28
 	 */
38cfe28
-	rc = read_sysfs_file(&filebuf,
38cfe28
-	                     "class/block/nvme%dn%d/eui",
38cfe28
-	                     ctrl_id, ns_id);
38cfe28
-	if ((rc < 0 && errno == ENOENT) || filebuf == NULL) {
38cfe28
-	        rc = read_sysfs_file(&filebuf,
38cfe28
-	                     "class/block/nvme%dn%d/device/eui",
38cfe28
-	                     ctrl_id, ns_id);
38cfe28
+	char *euipath = NULL;
38cfe28
+	rc = read_sysfs_file(&filebuf, "class/block/nvme%dn%d/eui", ctrl_id, ns_id);
38cfe28
+	if (rc < 0 && (errno == ENOENT || errno == ENOTDIR)) {
38cfe28
+		rc = find_device_file(&euipath, "eui", "class/block/nvme%dn%d", ctrl_id, ns_id);
38cfe28
+		if (rc >= 0 && euipath != NULL)
38cfe28
+			rc = read_sysfs_file(&filebuf, "%s", euipath);
38cfe28
 	}
38cfe28
 	if (rc >= 0 && filebuf != NULL) {
38cfe28
 	        uint8_t eui[8];
38cfe28
diff --git a/src/linux.c b/src/linux.c
38cfe28
index 1051894e7bc..0324e20c8cf 100644
38cfe28
--- a/src/linux.c
38cfe28
+++ b/src/linux.c
38cfe28
@@ -401,26 +401,32 @@ struct device HIDDEN
38cfe28
 	        goto err;
38cfe28
 	}
38cfe28
 
38cfe28
-	if (dev->device[0] != 0) {
38cfe28
-	        rc = sysfs_readlink(&tmpbuf, "block/%s/device/driver", dev->disk_name);
38cfe28
+	/*
38cfe28
+	 * So, on a normal disk, you get something like:
38cfe28
+	 * /sys/block/sda/device -> ../../0:0:0:0
38cfe28
+	 * /sys/block/sda/device/driver -> ../../../../../../../bus/scsi/drivers/sd
38cfe28
+	 *
38cfe28
+	 * On a directly attached nvme device you get:
38cfe28
+	 * /sys/block/nvme0n1/device -> ../../nvme0
38cfe28
+	 * /sys/block/nvme0n1/device/device -> ../../../0000:6e:00.0
38cfe28
+	 * /sys/block/nvme0n1/device/device/driver -> ../../../../bus/pci/drivers/nvme
38cfe28
+	 *
38cfe28
+	 * On a fabric-attached nvme device, you get something like:
38cfe28
+	 * /sys/block/nvme0n1/device -> ../../nvme0
38cfe28
+	 * /sys/block/nvme0n1/device/device -> ../../ctl
38cfe28
+	 * /sys/block/nvme0n1/device/device/device -> ../../../../../0000:6e:00.0
38cfe28
+	 * /sys/block/nvme0n1/device/device/device/driver -> ../../../../../../bus/pci/drivers/nvme-fabrics
38cfe28
+	 *
38cfe28
+	 * ... I think?  I don't have one in front of me.
38cfe28
+	 */
38cfe28
+
38cfe28
+	char *filepath = NULL;
38cfe28
+	rc = find_device_file(&filepath, "driver", "block/%s", dev->disk_name);
38cfe28
+	if (rc >= 0) {
38cfe28
+		rc = sysfs_readlink(&tmpbuf, "%s", filepath);
38cfe28
 	        if (rc < 0 || !tmpbuf) {
38cfe28
-	                if (errno == ENOENT) {
38cfe28
-	                        /*
38cfe28
-	                         * nvme, for example, will have nvme0n1/device point
38cfe28
-	                         * at nvme0, and we need to look for device/driver
38cfe28
-	                         * there.
38cfe28
-	                         */
38cfe28
-	                        rc = sysfs_readlink(&tmpbuf,
38cfe28
-	                                            "block/%s/device/device/driver",
38cfe28
-	                                            dev->disk_name);
38cfe28
-	                        if (rc >= 0 && tmpbuf)
38cfe28
-	                                efi_error_pop();
38cfe28
-	                }
38cfe28
-	                if (rc < 0 || !tmpbuf) {
38cfe28
-	                        efi_error("readlink of /sys/block/%s/device/driver failed",
38cfe28
-	                                  dev->disk_name);
38cfe28
-	                        goto err;
38cfe28
-	                }
38cfe28
+			efi_error("readlink of /sys/%s failed", filepath);
38cfe28
+	                goto err;
38cfe28
 	        }
38cfe28
 
38cfe28
 	        linkbuf = pathseg(tmpbuf, -1);
38cfe28
@@ -431,7 +437,7 @@ struct device HIDDEN
38cfe28
 
38cfe28
 	        dev->driver = strdup(linkbuf);
38cfe28
 	} else {
38cfe28
-	        dev->driver = strdup("");
38cfe28
+		dev->driver = strdup("");
38cfe28
 	}
38cfe28
 
38cfe28
 	if (!dev->driver) {
38cfe28
diff --git a/src/linux.h b/src/linux.h
38cfe28
index 5ae64ffaacf..97f9518bb5c 100644
38cfe28
--- a/src/linux.h
38cfe28
+++ b/src/linux.h
38cfe28
@@ -1,6 +1,6 @@
38cfe28
 /*
38cfe28
  * libefiboot - library for the manipulation of EFI boot variables
38cfe28
- * Copyright 2012-2015 Red Hat, Inc.
38cfe28
+ * Copyright 2012-2019 Red Hat, Inc.
38cfe28
  * Copyright (C) 2001 Dell Computer Corporation <Matt_Domsch@dell.com>
38cfe28
  *
38cfe28
  * This library is free software; you can redistribute it and/or
38cfe28
@@ -218,6 +218,22 @@ extern ssize_t HIDDEN make_mac_path(uint8_t *buf, ssize_t size,
38cfe28
 		_rc;							\
38cfe28
 	})
38cfe28
 
38cfe28
+#define sysfs_access(mode, fmt, args...)				\
38cfe28
+	({								\
38cfe28
+		int rc_;						\
38cfe28
+		char *pn_;						\
38cfe28
+									\
38cfe28
+		rc_ = asprintfa(&pn_, "/sys/" fmt, ## args);		\
38cfe28
+		if (rc_ >= 0) {						\
38cfe28
+			rc_ = access(pn_, mode);			\
38cfe28
+			if (rc_ < 0)					\
38cfe28
+				efi_error("could not access %s", pn_);  \
38cfe28
+		} else {						\
38cfe28
+			efi_error("could not allocate memory");		\
38cfe28
+		}							\
38cfe28
+		rc_;							\
38cfe28
+	})
38cfe28
+
38cfe28
 #define sysfs_stat(statbuf, fmt, args...)				\
38cfe28
 	({								\
38cfe28
 		int rc_;						\
38cfe28
@@ -251,6 +267,59 @@ extern ssize_t HIDDEN make_mac_path(uint8_t *buf, ssize_t size,
38cfe28
 		dir_;							\
38cfe28
 	})
38cfe28
 
38cfe28
+/*
38cfe28
+ * Iterate a /sys/block directory looking for device/foo, device/device/foo,
38cfe28
+ * etc.  I'm not proud of this method.
38cfe28
+ */
38cfe28
+#define find_device_file(result, name, fmt, args...)				\
38cfe28
+	({									\
38cfe28
+		int rc_ = 0;							\
38cfe28
+		debug("searching for %s from in %s", name, dev->disk_name);	\
38cfe28
+		for (unsigned int try_ = 0; true; try_++) {			\
38cfe28
+			char slashdev_[sizeof("device")				\
38cfe28
+				       + try_ * strlen("/device")];		\
38cfe28
+										\
38cfe28
+			char *nul_ = stpcpy(slashdev_, "device");		\
38cfe28
+			for (unsigned int i_ = 0; i_ < try_; i_++)		\
38cfe28
+				nul_ = stpcpy(nul_, "/device");			\
38cfe28
+										\
38cfe28
+			debug("trying /sys/" fmt "/%s/%s",			\
38cfe28
+			      ## args, slashdev_, name);			\
38cfe28
+										\
38cfe28
+			rc_ = sysfs_access(F_OK, fmt "/%s", ## args, slashdev_);\
38cfe28
+			if (rc_ < 0) {						\
38cfe28
+				if (errno == ENOENT) {				\
38cfe28
+					efi_error_pop();			\
38cfe28
+					break;					\
38cfe28
+				}						\
38cfe28
+				efi_error("cannot access /sys/"fmt"/%s: %m",	\
38cfe28
+					  ## args, slashdev_);			\
38cfe28
+				goto find_device_link_err_;			\
38cfe28
+			}							\
38cfe28
+										\
38cfe28
+			rc_ = sysfs_access(F_OK, fmt "/%s/%s",			\
38cfe28
+					   ## args, slashdev_, name);		\
38cfe28
+			if (rc_ < 0) {						\
38cfe28
+				if (errno == ENOENT) {				\
38cfe28
+					efi_error_pop();			\
38cfe28
+					break;					\
38cfe28
+				}						\
38cfe28
+				efi_error("cannot access /sys/"fmt"/%s/%s: %m",	\
38cfe28
+					  ## args, slashdev_, name);		\
38cfe28
+				goto find_device_link_err_;			\
38cfe28
+			}							\
38cfe28
+										\
38cfe28
+			rc_ = asprintfa(result, fmt "/%s/%s",			\
38cfe28
+					## args, slashdev_, name);		\
38cfe28
+			if (rc_ < 0) {						\
38cfe28
+				efi_error("cannot allocate memory: %m");	\
38cfe28
+				goto find_device_link_err_;			\
38cfe28
+			}							\
38cfe28
+		}								\
38cfe28
+find_device_link_err_:								\
38cfe28
+		rc_;								\
38cfe28
+	})
38cfe28
+
38cfe28
 #define DEV_PROVIDES_ROOT       1
38cfe28
 #define DEV_PROVIDES_HD	 2
38cfe28
 #define DEV_ABBREV_ONLY	 4
38cfe28
-- 
38cfe28
2.24.1
38cfe28