Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features:
* Added opt-in drive health detection for NVMe disks, via udisks2 on Linux (no root required), NVMe SMART on macOS and the NVMe health log on Windows. (#2726, PhysicalDisk, Linux / macOS / Windows)
* Enable it with `"health": true`. The default output appends `Health <100 - percentage used>%` and any critical warnings.
* New format args: `{health}`, `{percent-used}`, `{available-spare}` and `{critical-warning}`.
* ATA disks report the SMART `failing` status as a critical warning.

Changes:
* Improved PCI GPU name resolution by checking udev's hardware database and the NVIDIA driver's procfs before falling back to pci.ids. (GPU, Linux)
Expand Down
1 change: 1 addition & 0 deletions src/detection/physicaldisk/physicaldisk.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef enum FFPhysicalDiskWarning: uint8_t {
FF_PHYSICALDISK_WARNING_READONLY = 1 << 3,
FF_PHYSICALDISK_WARNING_BACKUP = 1 << 4,
FF_PHYSICALDISK_WARNING_PMR_READONLY = 1 << 5,
FF_PHYSICALDISK_WARNING_FAILING = 1 << 6,
FF_PHYSICALDISK_WARNING_UNSET = UINT8_MAX,
} FFPhysicalDiskWarning;
static_assert(sizeof(FFPhysicalDiskWarning) == sizeof(uint8_t), "");
Expand Down
42 changes: 40 additions & 2 deletions src/detection/physicaldisk/physicaldisk_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
#include <IOKit/storage/IOBlockStorageDriver.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
#include <IOKit/storage/IOStorageProtocolCharacteristics.h>
#include <IOKit/storage/ata/ATASMARTLib.h>
#ifdef MAC_OS_X_VERSION_10_15
#include <IOKit/storage/nvme/NVMeSMARTLibExternal.h>
#endif

#ifdef MAC_OS_X_VERSION_10_15
static inline void wrapIoDestroyPlugInInterface(IOCFPlugInInterface*** pluginInf) {
assert(pluginInf);
if (*pluginInf) {
IODestroyPlugInInterface(*pluginInf);
}
}
#endif

static const char* detectNvmeSmart(io_service_t entryPhysical, FFPhysicalDiskResult* device, FFPhysicalDiskOptions* options) {
#ifdef MAC_OS_X_VERSION_10_15
Expand Down Expand Up @@ -59,6 +58,38 @@ static const char* detectNvmeSmart(io_service_t entryPhysical, FFPhysicalDiskRes
#endif
}

static const char* detectAtaSmart(io_service_t entryPhysical, FFPhysicalDiskResult* device) {
[[gnu::cleanup(wrapIoDestroyPlugInInterface)]] IOCFPlugInInterface** pluginInf = nullptr;
int32_t score;
if (IOCreatePlugInInterfaceForService(entryPhysical, kIOATASMARTUserClientTypeID, kIOCFPlugInInterfaceID, &pluginInf, &score) != kIOReturnSuccess) {
FF_DEBUG("IOCreatePlugInInterfaceForService(kIOATASMARTUserClientTypeID) failed");
return "IOCreatePlugInInterfaceForService() failed";
}

IOATASMARTInterface** smartInf = nullptr;
if ((*pluginInf)->QueryInterface(pluginInf, CFUUIDGetUUIDBytes(kIOATASMARTInterfaceID), (LPVOID) &smartInf) != kIOReturnSuccess) {
FF_DEBUG("QueryInterface(kIOATASMARTInterfaceID) failed");
return "QueryInterface() failed";
}

Boolean exceeded = false;
const char* error = nullptr;
if ((*smartInf)->SMARTReturnStatus(smartInf, &exceeded) == kIOReturnSuccess) {
if (device->criticalWarning == FF_PHYSICALDISK_WARNING_UNSET) {
device->criticalWarning = FF_PHYSICALDISK_WARNING_NONE;
}
if (exceeded) {
device->criticalWarning |= FF_PHYSICALDISK_WARNING_FAILING;
}
} else {
FF_DEBUG("SMARTReturnStatus() failed");
error = "SMARTReturnStatus() failed";
}

(*pluginInf)->Release(smartInf);
return error;
}

const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) {
FF_IOOBJECT_AUTO_RELEASE io_iterator_t iterator = 0;
if (IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOBlockStorageDriverClass), &iterator) != KERN_SUCCESS) {
Expand Down Expand Up @@ -200,6 +231,13 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
}
}
#endif

if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && options->health) {
FF_CFTYPE_AUTO_RELEASE CFBooleanRef ataSMARTCapable = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertySMARTCapableKey), nullptr, kNilOptions);
if (ataSMARTCapable && CFBooleanGetValue(ataSMARTCapable)) {
detectAtaSmart(entryPhysical, device);
}
}
}

return nullptr;
Expand Down
92 changes: 73 additions & 19 deletions src/detection/physicaldisk/physicaldisk_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#define FF_UDISKS_BUS "org.freedesktop.UDisks2"
#define FF_UDISKS_NVME_CONTROLLER "org.freedesktop.UDisks2.NVMe.Controller"
#define FF_UDISKS_DRIVE_ATA "org.freedesktop.UDisks2.Drive.Ata"

static bool hasUDisks(FFDBusData* dbus) {
DBusMessage* reply = ffDBusGetMethodReply(dbus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameHasOwner", FF_UDISKS_BUS, nullptr);
Expand Down Expand Up @@ -80,7 +81,7 @@ static void parseSmartAttributes(FFDBusData* dbus, DBusMessage* reply, FFPhysica
DBusMessageIter entryIter;
dbus->lib->ffdbus_message_iter_recurse(&dictIter, &entryIter);
if (dbus->lib->ffdbus_message_iter_get_arg_type(&entryIter) != DBUS_TYPE_STRING) {
return;
break;
}

const char* key;
Expand All @@ -102,43 +103,81 @@ static void parseSmartAttributes(FFDBusData* dbus, DBusMessage* reply, FFPhysica
}
}

static void detectNvmeHealth(FFDBusData* dbus, const char* devName, FFPhysicalDiskResult* device) {
char blockPath[128];
snprintf(blockPath, ARRAY_SIZE(blockPath), "/org/freedesktop/UDisks2/block_devices/%s", devName);

FF_STRBUF_AUTO_DESTROY drivePath = ffStrbufCreate();
if (!ffDBusGetPropertyString(dbus, FF_UDISKS_BUS, blockPath, "org.freedesktop.UDisks2.Block", "Drive", &drivePath) || drivePath.length <= 1) {
FF_DEBUG("Failed to get udisks drive of %s", devName);
return;
}

static void detectNvmeHealth(FFDBusData* dbus, const char* drivePath, FFPhysicalDiskResult* device) {
uint64_t smartUpdated = 0;
if (!ffDBusGetPropertyUint(dbus, FF_UDISKS_BUS, drivePath.chars, FF_UDISKS_NVME_CONTROLLER, "SmartUpdated", &smartUpdated)) {
FF_DEBUG("%s has no NVMe controller interface", drivePath.chars);
if (!ffDBusGetPropertyUint(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_NVME_CONTROLLER, "SmartUpdated", &smartUpdated)) {
FF_DEBUG("%s has no NVMe controller interface", drivePath);
return;
}
if (smartUpdated == 0) {
FF_DEBUG("udisks has not collected SMART data of %s yet", drivePath.chars);
FF_DEBUG("udisks has not collected SMART data of %s yet", drivePath);
return;
}

DBusMessage* reply = ffDBusGetProperty(dbus, FF_UDISKS_BUS, drivePath.chars, FF_UDISKS_NVME_CONTROLLER, "SmartCriticalWarning");
DBusMessage* reply = ffDBusGetProperty(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_NVME_CONTROLLER, "SmartCriticalWarning");
if (reply == nullptr) {
FF_DEBUG("Failed to get SmartCriticalWarning of %s", drivePath.chars);
FF_DEBUG("Failed to get SmartCriticalWarning of %s", drivePath);
return;
}
device->criticalWarning = parseCriticalWarning(dbus, reply);
dbus->lib->ffdbus_message_unref(reply);

reply = ffDBusGetMethodReplyEmptyDict(dbus, FF_UDISKS_BUS, drivePath.chars, FF_UDISKS_NVME_CONTROLLER, "SmartGetAttributes");
reply = ffDBusGetMethodReplyEmptyDict(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_NVME_CONTROLLER, "SmartGetAttributes");
if (reply == nullptr) {
FF_DEBUG("SmartGetAttributes failed for %s", drivePath.chars);
FF_DEBUG("SmartGetAttributes failed for %s", drivePath);
return;
}
parseSmartAttributes(dbus, reply, device);
dbus->lib->ffdbus_message_unref(reply);
}

static void detectAtaHealth(FFDBusData* dbus, const char* drivePath, FFPhysicalDiskResult* device) {
DBusMessage* reply = ffDBusGetAllProperties(dbus, FF_UDISKS_BUS, drivePath, FF_UDISKS_DRIVE_ATA);
if (reply == nullptr) {
FF_DEBUG("%s has no ATA interface", drivePath);
return;
}

bool smartFailing = false;
uint64_t smartUpdated = 0;
DBusMessageIter iter;
if (dbus->lib->ffdbus_message_iter_init(reply, &iter) && dbus->lib->ffdbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_ARRAY) {
DBusMessageIter dictIter;
dbus->lib->ffdbus_message_iter_recurse(&iter, &dictIter);
while (dbus->lib->ffdbus_message_iter_get_arg_type(&dictIter) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entryIter;
dbus->lib->ffdbus_message_iter_recurse(&dictIter, &entryIter);
if (dbus->lib->ffdbus_message_iter_get_arg_type(&entryIter) != DBUS_TYPE_STRING) {
break;
}

const char* key;
dbus->lib->ffdbus_message_iter_get_basic(&entryIter, &key);
dbus->lib->ffdbus_message_iter_next(&entryIter);

if (ffStrEquals(key, "SmartUpdated")) {
ffDBusGetUint(dbus, &entryIter, &smartUpdated);
} else if (ffStrEquals(key, "SmartFailing")) {
ffDBusGetBool(dbus, &entryIter, &smartFailing);
}

dbus->lib->ffdbus_message_iter_next(&dictIter);
}
}
dbus->lib->ffdbus_message_unref(reply);

if (smartUpdated == 0) {
FF_DEBUG("udisks has not collected SMART data of %s yet", drivePath);
return;
}
if (device->criticalWarning == FF_PHYSICALDISK_WARNING_UNSET) {
device->criticalWarning = FF_PHYSICALDISK_WARNING_NONE;
}
if (smartFailing) {
device->criticalWarning |= FF_PHYSICALDISK_WARNING_FAILING;
}
}

static void detectHealth(FFlist* result) {
FF_DBUS_AUTO_DESTROY_DATA FFDBusData dbus = {};
const char* error = ffDBusLoadData(DBUS_BUS_SYSTEM, &dbus);
Expand All @@ -152,8 +191,23 @@ static void detectHealth(FFlist* result) {
}

FF_LIST_FOR_EACH (FFPhysicalDiskResult, device, *result) {
if (device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) {
continue;
}

char blockPath[128];
snprintf(blockPath, ARRAY_SIZE(blockPath), "/org/freedesktop/UDisks2/block_devices/%s", device->devPath.chars + strlen("/dev/"));

FF_STRBUF_AUTO_DESTROY drivePath = ffStrbufCreate();
if (!ffDBusGetPropertyString(&dbus, FF_UDISKS_BUS, blockPath, "org.freedesktop.UDisks2.Block", "Drive", &drivePath) || drivePath.length <= 1) {
FF_DEBUG("Failed to get udisks drive of %s", device->devPath.chars);
continue;
}

if (ffStrbufStartsWithS(&device->devPath, "/dev/nvme")) {
detectNvmeHealth(&dbus, device->devPath.chars + strlen("/dev/"), device);
detectNvmeHealth(&dbus, drivePath.chars, device);
} else {
detectAtaHealth(&dbus, drivePath.chars, device);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/detection/physicaldisk/physicaldisk_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,32 @@ static const char* detectPhysicalDisk(const char* physicalType, const wchar_t* s
}
}

if (options->health && sdd && !(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && (sdd->BusType == BusTypeAta || sdd->BusType == BusTypeSata)) {
STORAGE_PREDICT_FAILURE spf = {};
if (DeviceIoControl(
hDevice,
IOCTL_STORAGE_PREDICT_FAILURE,
nullptr,
0,
&spf,
sizeof(spf),
&retSize,
nullptr)) {
if (retSize >= sizeof(spf)) {
if (device->criticalWarning == FF_PHYSICALDISK_WARNING_UNSET) {
device->criticalWarning = FF_PHYSICALDISK_WARNING_NONE;
}
if (spf.PredictFailure) {
device->criticalWarning |= FF_PHYSICALDISK_WARNING_FAILING;
}
} else {
FF_DEBUG("ATA failure prediction reply too short: %u bytes", (unsigned) retSize);
}
} else {
FF_DEBUG("Failed to query ATA failure prediction: %s", ffDebugWin32Error(GetLastError()));
}
}

return nullptr;
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/physicaldisk/physicaldisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static const char* const warningNames[] = {
"read-only",
"backup failed",
"pmr read-only",
"failing",
};

static inline double getHealth(const FFPhysicalDiskResult* dev) {
Expand Down
Loading