| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "private.h" |
| 6 |
|
| 7 |
#define PATH_UUID_IBM "/proc/device-tree/ibm,partition-uuid" |
| 8 |
#define PATH_SYSFS_BLOCK "/sys/block" |
| 9 |
#define PATH_SYSFS_SLOTS "/sys/bus/pci/slots" |
| 10 |
#define PATH_SYSFS_NVME_SUBSYSTEM "/sys/class/nvme-subsystem" |
| 11 |
#define PATH_SYSFS_NVME "/sys/class/nvme" |
| 12 |
#define PATH_DMI_ENTRIES "/sys/firmware/dmi/entries" |
| 13 |
|
| 14 |
static const char *make_sysfs_dir(const char *path) |
| 15 |
{ |
| 16 |
char *basepath = getenv("LIBNVME_SYSFS_PATH"); |
| 17 |
char *str; |
| 18 |
|
| 19 |
if (!basepath) |
| 20 |
return path; |
| 21 |
|
| 22 |
if (asprintf(&str, "%s%s", basepath, path) < 0) |
| 23 |
return NULL; |
| 24 |
|
| 25 |
return str; |
| 26 |
} |
| 27 |
|
| 28 |
const char *nvme_subsys_sysfs_dir(void) |
| 29 |
{ |
| 30 |
static const char *str; |
| 31 |
|
| 32 |
if (str) |
| 33 |
return str; |
| 34 |
|
| 35 |
return str = make_sysfs_dir(PATH_SYSFS_NVME_SUBSYSTEM); |
| 36 |
} |
| 37 |
|
| 38 |
const char *nvme_ctrl_sysfs_dir(void) |
| 39 |
{ |
| 40 |
static const char *str; |
| 41 |
|
| 42 |
if (str) |
| 43 |
return str; |
| 44 |
|
| 45 |
return str = make_sysfs_dir(PATH_SYSFS_NVME); |
| 46 |
} |
| 47 |
|
| 48 |
const char *nvme_ns_sysfs_dir(void) |
| 49 |
{ |
| 50 |
static const char *str; |
| 51 |
|
| 52 |
if (str) |
| 53 |
return str; |
| 54 |
|
| 55 |
return str = make_sysfs_dir(PATH_SYSFS_BLOCK); |
| 56 |
} |
| 57 |
|
| 58 |
const char *nvme_slots_sysfs_dir(void) |
| 59 |
{ |
| 60 |
static const char *str; |
| 61 |
|
| 62 |
if (str) |
| 63 |
return str; |
| 64 |
|
| 65 |
return str = make_sysfs_dir(PATH_SYSFS_SLOTS); |
| 66 |
} |
| 67 |
|
| 68 |
const char *nvme_uuid_ibm_filename(void) |
| 69 |
{ |
| 70 |
static const char *str; |
| 71 |
|
| 72 |
if (str) |
| 73 |
return str; |
| 74 |
|
| 75 |
return str = make_sysfs_dir(PATH_UUID_IBM); |
| 76 |
} |
| 77 |
|
| 78 |
const char *nvme_dmi_entries_dir(void) |
| 79 |
{ |
| 80 |
static const char *str; |
| 81 |
|
| 82 |
if (str) |
| 83 |
return str; |
| 84 |
|
| 85 |
return str = make_sysfs_dir(PATH_DMI_ENTRIES); |
| 86 |
} |