Skip to content

Commit 300fb88

Browse files
rghaddabcarlescufi
authored andcommitted
[nrf fromlist] settings: zms: recover linked list if broken
When the linked list is broken, recover it instead of reinitializing it. Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit 057c1d65fa5822d9145330c87183d0b652707ab2) (cherry picked from commit 4a3c56c)
1 parent eae1680 commit 300fb88

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

subsys/settings/src/settings_zms.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,6 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
526526
return rc;
527527
}
528528
}
529-
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
530-
no_ll_update:
531-
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
532529
return 0;
533530
}
534531

@@ -547,16 +544,31 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
547544
rc = zms_read(&cf->cf_zms, ll_last_hash_id, &settings_element,
548545
sizeof(settings_element));
549546
if (rc == -ENOENT) {
550-
/* header doesn't exist or linked list broken, reinitialize the header */
551-
const struct settings_hash_linked_list settings_element = {
552-
.previous_hash = 0, .next_hash = 0};
553-
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
554-
sizeof(struct settings_hash_linked_list));
555-
if (rc < 0) {
556-
return rc;
547+
/* header doesn't exist or linked list broken, reinitialize the header
548+
* if it doesn't exist and recover it if it is broken
549+
*/
550+
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
551+
/* header doesn't exist */
552+
const struct settings_hash_linked_list settings_element = {
553+
.previous_hash = 0, .next_hash = 0};
554+
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
555+
sizeof(struct settings_hash_linked_list));
556+
if (rc < 0) {
557+
return rc;
558+
}
559+
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
560+
cf->second_to_last_hash_id = 0;
561+
} else {
562+
/* let's recover it by keeping all nodes until the last one */
563+
const struct settings_hash_linked_list settings_element = {
564+
.previous_hash = cf->second_to_last_hash_id,
565+
.next_hash = 0};
566+
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
567+
sizeof(struct settings_hash_linked_list));
568+
if (rc < 0) {
569+
return rc;
570+
}
557571
}
558-
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
559-
cf->second_to_last_hash_id = 0;
560572
return 0;
561573
} else if (rc < 0) {
562574
return rc;

0 commit comments

Comments
 (0)