Add support for specifying case-insensitive SMART attribute names in the smartignore option

This commit is contained in:
Andrea Mazzoleni 2026-06-25 22:52:53 +02:00
parent a6aa723b08
commit 82fdb5663d
51 changed files with 624 additions and 231 deletions

View file

@ -274,6 +274,14 @@ extern int exit_sync_needed;
*/
#define SMART_MAX 128
/**
* Smart ignore structure.
*/
struct smartignore_struct {
int attr_index; /**< 0 if not set */
char attr_name[SMART_MAX]; /**< Empty if not set */
};
/**
* Value for unassigned SMART attribute.
*/
@ -315,7 +323,7 @@ struct devinfo_struct {
char mount[PATH_MAX]; /**< Mount point or other contained directory. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Attribues to ignore */
struct smartignore_struct smartignore[SMART_IGNORE_MAX]; /**< Attributes to ignore */
char file[PATH_MAX]; /**< File device. */
#ifdef _WIN32
char wfile[PATH_MAX]; /**< File device in Windows format. Like \\.\PhysicalDriveX, or \\?\Volume{X}. */

View file

@ -533,14 +533,37 @@ static void state_info_log(devinfo_t* devinfo)
log_tag("attr:%s:%s:rotationrate:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->info[INFO_ROTATION_RATE]);
}
static void state_smart_ignore(struct snapraid_state* state, devinfo_t* devinfo)
void state_smart_ignore(struct snapraid_state* state, devinfo_t* devinfo)
{
int i, j;
/* clear attributes to ignore */
for (int i = 0; i < SMART_IGNORE_MAX; ++i) {
if (state->smartignore[i] < 256)
devinfo->smart[state->smartignore[i]].raw = SMART_UNASSIGNED;
if (devinfo->smartignore[i] < 256)
devinfo->smart[devinfo->smartignore[i]].raw = SMART_UNASSIGNED;
for (i = 0; i < SMART_IGNORE_MAX; ++i) {
/* global ignore */
if (state->smartignore[i].attr_index > 0 && state->smartignore[i].attr_index < 256) {
devinfo->smart[state->smartignore[i].attr_index].raw = SMART_UNASSIGNED;
}
if (state->smartignore[i].attr_name[0] != 0) {
for (j = 0; j < SMART_COUNT; ++j) {
if (devinfo->smart[j].name[0] != 0
&& strcasecmp(devinfo->smart[j].name, state->smartignore[i].attr_name) == 0) {
devinfo->smart[j].raw = SMART_UNASSIGNED;
}
}
}
/* devinfo ignore */
if (devinfo->smartignore[i].attr_index > 0 && devinfo->smartignore[i].attr_index < 256) {
devinfo->smart[devinfo->smartignore[i].attr_index].raw = SMART_UNASSIGNED;
}
if (devinfo->smartignore[i].attr_name[0] != 0) {
for (j = 0; j < SMART_COUNT; ++j) {
if (devinfo->smart[j].name[0] != 0
&& strcasecmp(devinfo->smart[j].name, devinfo->smartignore[i].attr_name) == 0) {
devinfo->smart[j].raw = SMART_UNASSIGNED;
}
}
}
}
}

View file

@ -844,8 +844,10 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev
disk->smartctl[0] = 0;
disk->smartctl_info[0] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
disk->smartignore[i] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i) {
disk->smartignore[i].attr_index = 0;
disk->smartignore[i].attr_name[0] = 0;
}
disk->fstype[0] = 0;
disk->fslabel[0] = 0;
disk->mount_device = dev;
@ -917,8 +919,10 @@ struct snapraid_extra* extra_alloc(const char* name, const char* dir, uint64_t d
extra->smartctl[0] = 0;
extra->smartctl_info[0] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
extra->smartignore[i] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i) {
extra->smartignore[i].attr_index = 0;
extra->smartignore[i].attr_name[0] = 0;
}
extra->fstype[0] = 0;
extra->fslabel[0] = 0;
extra->device = dev;

View file

@ -345,7 +345,7 @@ struct snapraid_extra {
char dir[PATH_MAX]; /**< Mount point of the disk. It always terminates with /. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
struct smartignore_struct smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
char uuid[UUID_MAX]; /**< UUID of the disk. They are probed during the config reading. */
char fstype[FSINFO_MAX]; /**< Filesystem type */
char fslabel[FSINFO_MAX]; /**< Filesystem label */
@ -384,7 +384,7 @@ struct snapraid_disk {
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
struct smartignore_struct smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
char uuid[UUID_MAX]; /**< UUID of the disk. They are probed during the config reading. */
char fstype[FSINFO_MAX]; /**< Filesystem type */
char fslabel[FSINFO_MAX]; /**< Filesystem label */
@ -538,7 +538,7 @@ struct snapraid_parity {
unsigned split_mac; /**< Number of parity splits. */
char smartctl[SMART_MAX]; /**< Custom option for smartctl. Empty means auto. */
char smartctl_info[SMART_MAX]; /* Info options for smartctl. Empty means -a. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
struct smartignore_struct smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
block_off_t total_blocks; /**< Number of total blocks. */
block_off_t free_blocks; /**< Number of free blocks at the last sync. */
int is_excluded_by_filter; /**< If the parity is excluded by filters. */

View file

@ -932,6 +932,69 @@ static void test_parse_smartctl(void)
}
}
static void test_smart_ignore(void)
{
struct snapraid_state state;
devinfo_t devinfo;
int i;
memset(&state, 0, sizeof(state));
memset(&devinfo, 0, sizeof(devinfo));
/* Initialize devinfo attributes with mock data */
for (i = 0; i < SMART_COUNT; ++i) {
devinfo.smart[i].raw = 100;
devinfo.smart[i].norm = 100;
snprintf(devinfo.smart[i].name, sizeof(devinfo.smart[i].name), "Attr_%d", i);
}
/* Special name for test */
pathcpy(devinfo.smart[5].name, sizeof(devinfo.smart[5].name), "Reallocated_Sector_Ct");
pathcpy(devinfo.smart[197].name, sizeof(devinfo.smart[197].name), "Current_Pending_Sector");
/* 1. Test numerical global ignore */
state.smartignore[0].attr_index = 5;
state.smartignore[0].attr_name[0] = 0;
/* 2. Test name global ignore (case-insensitive) */
state.smartignore[1].attr_index = 0;
pathcpy(state.smartignore[1].attr_name, sizeof(state.smartignore[1].attr_name), "current_pending_sector");
/* 3. Test numerical devinfo ignore */
devinfo.smartignore[0].attr_index = 10;
devinfo.smartignore[0].attr_name[0] = 0;
/* 4. Test name devinfo ignore (case-insensitive) */
devinfo.smartignore[1].attr_index = 0;
pathcpy(devinfo.smartignore[1].attr_name, sizeof(devinfo.smartignore[1].attr_name), "aTtR_20");
state_smart_ignore(&state, &devinfo);
/* Check that ignored attributes are set to SMART_UNASSIGNED */
if (devinfo.smart[5].raw != SMART_UNASSIGNED) {
log_fatal(EINTERNAL, "test_smart_ignore: numerical global ignore failed\n");
exit(EXIT_FAILURE);
}
if (devinfo.smart[197].raw != SMART_UNASSIGNED) {
log_fatal(EINTERNAL, "test_smart_ignore: name global ignore failed\n");
exit(EXIT_FAILURE);
}
if (devinfo.smart[10].raw != SMART_UNASSIGNED) {
log_fatal(EINTERNAL, "test_smart_ignore: numerical devinfo ignore failed\n");
exit(EXIT_FAILURE);
}
if (devinfo.smart[20].raw != SMART_UNASSIGNED) {
log_fatal(EINTERNAL, "test_smart_ignore: name devinfo ignore failed\n");
exit(EXIT_FAILURE);
}
/* Check that non-ignored attributes are still present */
if (devinfo.smart[6].raw != 100) {
log_fatal(EINTERNAL, "test_smart_ignore: unmodified attribute was cleared\n");
exit(EXIT_FAILURE);
}
}
void selftest(void)
{
log_tag("selftest:\n");
@ -958,6 +1021,7 @@ void selftest(void)
test_tommy();
test_wnmatch();
test_parse_smartctl();
test_smart_ignore();
if (raid_selftest() != 0) {
/* LCOV_EXCL_START */
log_fatal(EINTERNAL, "Failed SELF test\n");

View file

@ -146,8 +146,10 @@ void state_init(struct snapraid_state* state)
}
state->parity[l].smartctl[0] = 0;
state->parity[l].smartctl_info[0] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
state->parity[l].smartignore[i] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i) {
state->parity[l].smartignore[i].attr_index = 0;
state->parity[l].smartignore[i].attr_name[0] = 0;
}
state->parity[l].total_blocks = 0;
state->parity[l].free_blocks = 0;
state->parity[l].skip_access = 0;
@ -167,8 +169,10 @@ void state_init(struct snapraid_state* state)
state->lockfile[0] = 0;
state->level = 1; /* default is the lowest protection */
state->no_conf = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i)
state->smartignore[i] = 0;
for (i = 0; i < SMART_IGNORE_MAX; ++i) {
state->smartignore[i].attr_index = 0;
state->smartignore[i].attr_name[0] = 0;
}
state->rehash_blocks = 0;
state->bad_blocks = 0;
state->unsynced_blocks = 0;
@ -1265,7 +1269,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
}
}
} else if (strcmp(tag, "smartignore") == 0) {
int* smart;
struct smartignore_struct* smart;
ret = sgettok(f, buffer, sizeof(buffer));
if (ret < 0) {
@ -1341,20 +1345,32 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
smart[si] = strtou(buffer, &e, 0);
int val = strtou(buffer, &e, 0);
if (!e || *e) {
/* LCOV_EXCL_START */
log_fatal(EUSER, "Invalid 'smartignore' specification in '%s' at line %u. It should be a number.\n", path, line);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (smart[si] < 1 || smart[si] >= 256) {
/* LCOV_EXCL_START */
log_fatal(EUSER, "Invalid 'smartignore' specification in '%s' at line %u. It should be between 1 and 255.\n", path, line);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
if (e && !*e) {
/* numerical form */
if (val < 1 || val >= 256) {
/* LCOV_EXCL_START */
log_fatal(EUSER,
"Invalid 'smartignore' specification in '%s' at line %u. It should be between 1 and 255.\n",
path, line);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
smart[si].attr_index = val;
smart[si].attr_name[0] = 0;
} else {
/* name form */
if (strlen(buffer) >= SMART_MAX) {
/* LCOV_EXCL_START */
log_fatal(EUSER,
"Invalid 'smartignore' specification in '%s' at line %u. Attribute name too long.\n",
path, line);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
smart[si].attr_index = 0;
pathcpy(smart[si].attr_name, sizeof(smart[si].attr_name), buffer);
}
++si;

View file

@ -134,7 +134,7 @@ struct snapraid_state {
unsigned prevhash; /**< Previous hash kind used. In case of rehash. */
unsigned besthash; /**< Best hash suggested. */
const char* command; /**< Command running. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for all devices. */
struct smartignore_struct smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for all devices. */
uint64_t rehash_blocks; /**< Blocks marked rehash */
uint64_t bad_blocks; /**< Blocks marked bad */
uint64_t unsynced_blocks; /**< If the parity is invalid, and a sync is needed. */
@ -465,5 +465,10 @@ void generate_configuration(const char* content);
*/
int parse_smartctl(const char* custom, char* smartctl, size_t smartctl_size, char* smartctl_info, size_t smartctl_info_size);
/**
* Apply smart ignore logic to devinfo.
*/
void state_smart_ignore(struct snapraid_state* state, devinfo_t* devinfo);
#endif

View file

@ -2412,6 +2412,10 @@ int smartctl_attribute(OS_FILE* f, const char* file, const char* name, struct sm
for (i = 0; i < INFO_COUNT; ++i)
info[i] = SMART_UNASSIGNED;
pathcpy(smart[SMART_ERROR_PROTOCOL].name, sizeof(smart[SMART_ERROR_PROTOCOL].name), "error_protocol");
pathcpy(smart[SMART_ERROR_MEDIUM].name, sizeof(smart[SMART_ERROR_MEDIUM].name), "error_medium");
pathcpy(smart[SMART_WEAR_LEVEL].name, sizeof(smart[SMART_WEAR_LEVEL].name), "wear_level");
/* read the file */
inside = 0;
while (1) {