Skip to content

Commit f44f945

Browse files
committed
[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 5d4935f694def7123a511853bddf7714f01b9886)
1 parent 6b23149 commit f44f945

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

subsys/settings/src/settings_zms.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -633,21 +633,36 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
633633
rc = zms_read(&cf->cf_zms, ll_last_hash_id, &settings_element,
634634
sizeof(settings_element));
635635
if (rc == -ENOENT) {
636-
/* header doesn't exist or linked list broken, reinitialize the header */
637-
const struct settings_hash_linked_list settings_element = {
638-
.previous_hash = 0, .next_hash = 0};
639-
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
640-
sizeof(struct settings_hash_linked_list));
641-
if (rc < 0) {
642-
return rc;
643-
}
644-
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
645-
cf->second_to_last_hash_id = 0;
636+
/* header doesn't exist or linked list broken, reinitialize the header
637+
* if it doesn't exist and recover it if it is broken
638+
*/
639+
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
640+
/* header doesn't exist */
641+
const struct settings_hash_linked_list settings_element = {
642+
.previous_hash = 0, .next_hash = 0};
643+
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
644+
sizeof(struct settings_hash_linked_list));
645+
if (rc < 0) {
646+
return rc;
647+
}
648+
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
649+
cf->second_to_last_hash_id = 0;
646650
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
647-
/* store the LL header in cache */
648-
cf->ll_cache_next = 0;
649-
cf->ll_cache[cf->ll_cache_next++] = settings_element;
651+
/* store the LL header in cache */
652+
cf->ll_cache_next = 0;
653+
cf->ll_cache[cf->ll_cache_next++] = settings_element;
650654
#endif
655+
} else {
656+
/* let's recover it by keeping all nodes until the last one */
657+
const struct settings_hash_linked_list settings_element = {
658+
.previous_hash = cf->second_to_last_hash_id,
659+
.next_hash = 0};
660+
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
661+
sizeof(struct settings_hash_linked_list));
662+
if (rc < 0) {
663+
return rc;
664+
}
665+
}
651666
return 0;
652667
} else if (rc < 0) {
653668
return rc;

0 commit comments

Comments
 (0)