Skip to content

Commit 0f6b823

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 d4564b7eaa6b0b9c6fa1810b001e318e01da68b3)
1 parent 810d478 commit 0f6b823

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

subsys/settings/src/settings_zms.c

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

0 commit comments

Comments
 (0)