summaryrefslogtreecommitdiffstats
path: root/src/nvme/sysfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvme/sysfs.c')
-rw-r--r--src/nvme/sysfs.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/nvme/sysfs.c b/src/nvme/sysfs.c
new file mode 100644
index 0000000..ea4f0e2
--- /dev/null
+++ b/src/nvme/sysfs.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "private.h"
+
+#define PATH_UUID_IBM "/proc/device-tree/ibm,partition-uuid"
+#define PATH_SYSFS_BLOCK "/sys/block"
+#define PATH_SYSFS_SLOTS "/sys/bus/pci/slots"
+#define PATH_SYSFS_NVME_SUBSYSTEM "/sys/class/nvme-subsystem"
+#define PATH_SYSFS_NVME "/sys/class/nvme"
+#define PATH_DMI_ENTRIES "/sys/firmware/dmi/entries"
+
+static const char *make_sysfs_dir(const char *path)
+{
+ char *basepath = getenv("LIBNVME_SYSFS_PATH");
+ char *str;
+
+ if (!basepath)
+ return path;
+
+ if (asprintf(&str, "%s%s", basepath, path) < 0)
+ return NULL;
+
+ return str;
+}
+
+const char *nvme_subsys_sysfs_dir(void)
+{
+ static const char *str;
+
+ if (str)
+ return str;
+
+ return str = make_sysfs_dir(PATH_SYSFS_NVME_SUBSYSTEM);
+}
+
+const char *nvme_ctrl_sysfs_dir(void)
+{
+ static const char *str;
+
+ if (str)
+ return str;
+
+ return str = make_sysfs_dir(PATH_SYSFS_NVME);
+}
+
+const char *nvme_ns_sysfs_dir(void)
+{
+ static const char *str;
+
+ if (str)
+ return str;
+
+ return str = make_sysfs_dir(PATH_SYSFS_BLOCK);
+}
+
+const char *nvme_slots_sysfs_dir(void)
+{
+ static const char *str;
+
+ if (str)
+ return str;
+
+ return str = make_sysfs_dir(PATH_SYSFS_SLOTS);
+}
+
+const char *nvme_uuid_ibm_filename(void)
+{
+ static const char *str;
+
+ if (str)
+ return str;
+
+ return str = make_sysfs_dir(PATH_UUID_IBM);
+}
+
+const char *nvme_dmi_entries_dir(void)
+{
+ static const char *str;
+
+ if (str)
+ return str;
+
+ return str = make_sysfs_dir(PATH_DMI_ENTRIES);
+}