summaryrefslogtreecommitdiffstats
path: root/src/nvme/sysfs.c
blob: ea4f0e20c3ffb79ef6de2a1b24917e4c3d7b6058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
}