Detect degraded filesystems in device commands and exit with EXIT_DEGRADED

This commit is contained in:
Andrea Mazzoleni 2026-09-18 23:28:47 +02:00
parent 2a4335ef7d
commit 929ba5cebe
47 changed files with 183 additions and 4 deletions

View file

@ -228,11 +228,13 @@ extern FILE* stdlog;
extern int exit_success;
extern int exit_failure;
extern int exit_sync_needed;
extern int exit_degraded;
#undef EXIT_SUCCESS
#undef EXIT_FAILURE
#define EXIT_SUCCESS exit_success
#define EXIT_FAILURE exit_failure
#define EXIT_SYNC_NEEDED exit_sync_needed
#define EXIT_DEGRADED exit_degraded
/**
* Standard SMART attributes.

View file

@ -1198,6 +1198,8 @@ int state_device(struct snapraid_state* state, int operation, tommy_list* filter
if (state->opt.gui)
log_tag("unixtime:%" PRIi64 "\n", (int64_t)now);
int degraded = 0;
if (state->opt.fake_device) {
ret = devtest(&high, &low, operation);
} else {
@ -1206,6 +1208,10 @@ int state_device(struct snapraid_state* state, int operation, tommy_list* filter
devsync(&high);
ret = devquery(&high, &low);
if (ret > 0) {
degraded = 1;
ret = 0;
}
}
/* if the list is empty, it's not supported in this platform */
@ -1251,6 +1257,9 @@ bail:
tommy_list_foreach(&high, free);
tommy_list_foreach(&low, free);
if (ret == 0 && degraded)
ret = 1;
return ret;
}

View file

@ -17,6 +17,7 @@
int exit_success = 0;
int exit_failure = 1;
int exit_sync_needed = 2;
int exit_degraded = 3;
/****************************************************************************/
/* signal */

View file

@ -1804,6 +1804,8 @@ int snapraid_main(int argc, char* argv[])
/* abort if required */
if (ret != 0) {
/* LCOV_EXCL_START */
if (ret == 1)
exit(EXIT_DEGRADED);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}

View file

@ -18,6 +18,7 @@
int exit_success = 0;
int exit_failure = 1;
int exit_sync_needed = 2;
int exit_degraded = 3;
/****************************************************************************/
/* signal */
@ -728,6 +729,8 @@ static int devdereference_btrfs(uint64_t device, const char* dir, int fd, tommy_
/* LCOV_EXCL_STOP */
}
int degraded = 0;
for (__u64 i = 1; i <= fs_info.max_id; ++i) {
struct btrfs_ioctl_dev_info_args dev_info;
memset(&dev_info, 0, sizeof(dev_info));
@ -743,10 +746,20 @@ static int devdereference_btrfs(uint64_t device, const char* dir, int fd, tommy_
/* LCOV_EXCL_STOP */
}
/* missing devices in degraded btrfs have empty path */
if (dev_info.path[0] == 0) {
degraded = 1;
continue;
}
/* get major:minor, use stat on the path returned */
struct stat st;
if (stat((char*)dev_info.path, &st) != 0) {
/* LCOV_EXCL_START */
if (errno == ENOENT) {
degraded = 1;
continue;
}
log_error(errno, "Failed stat %s. %s.", dev_info.path, strerror(errno));
goto bail;
/* LCOV_EXCL_STOP */
@ -770,7 +783,7 @@ static int devdereference_btrfs(uint64_t device, const char* dir, int fd, tommy_
if (tommy_list_empty(devlist))
goto bail;
return 0;
return degraded ? 1 : 0;
bail:
tommy_list_foreach(devlist, free);
@ -4013,6 +4026,7 @@ void devsync(tommy_list* high)
int devquery(tommy_list* high, tommy_list* low)
{
int degraded = 0;
#if HAVE_LINUX_DEVICE
tommy_node* i;
struct stat st;
@ -4034,12 +4048,18 @@ int devquery(tommy_list* high, tommy_list* low)
tommy_list_init(&devlist);
/* obtain the real devices */
if (devdereference(device, devinfo->mount, &devlist) != 0) {
int ret = devdereference(device, devinfo->mount, &devlist);
if (ret < 0) {
/* LCOV_EXCL_START */
log_fatal(EEXTERNAL, "Failed to dereference device '%u:%u' at '%s'.\n", major(device), minor(device), devinfo->mount);
return -1;
/* LCOV_EXCL_STOP */
}
if (ret > 0) {
log_fatal(ENXIO, "DANGER! Disk '%s' is degraded due to missing device(s).\n", devinfo->name);
log_tag("degraded:%s\n", esc_tag(devinfo->name));
degraded = 1;
}
devinfo->file[0] = 0;
for (tommy_node* j = tommy_list_head(&devlist); j != 0; j = j->next) {
@ -4085,7 +4105,7 @@ int devquery(tommy_list* high, tommy_list* low)
(void)low;
#endif
return 0;
return degraded ? 1 : 0;
}
int devrun(tommy_list* low, int operation)