Skip to content

Commit 1c7a063

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 b19675575bb8f73b808864ff1ff709faa1b658c6)
1 parent e85fe14 commit 1c7a063

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
@@ -635,22 +635,37 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
635635
rc = zms_read(&cf->cf_zms, ll_last_hash_id, &settings_element,
636636
sizeof(settings_element));
637637
if (rc == -ENOENT) {
638-
/* header doesn't exist or linked list broken, reinitialize the header */
639-
const struct settings_hash_linked_list settings_element = {
640-
.previous_hash = 0, .next_hash = 0};
641-
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
642-
sizeof(struct settings_hash_linked_list));
643-
if (rc < 0) {
644-
return rc;
645-
}
646-
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
647-
cf->second_to_last_hash_id = 0;
638+
/* header doesn't exist or linked list broken, reinitialize the header
639+
* if it doesn't exist and recover it if it is broken
640+
*/
641+
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
642+
/* header doesn't exist */
643+
const struct settings_hash_linked_list settings_element = {
644+
.previous_hash = 0, .next_hash = 0};
645+
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
646+
sizeof(struct settings_hash_linked_list));
647+
if (rc < 0) {
648+
return rc;
649+
}
650+
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
651+
cf->second_to_last_hash_id = 0;
648652
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
649-
/* store the LL header in cache */
650-
cf->ll_cache_next = 0;
651-
cf->ll_cache[cf->ll_cache_next] = settings_element;
652-
cf->ll_cache_next = cf->ll_cache_next + 1;
653+
/* store the LL header in cache */
654+
cf->ll_cache_next = 0;
655+
cf->ll_cache[cf->ll_cache_next] = settings_element;
656+
cf->ll_cache_next = cf->ll_cache_next + 1;
653657
#endif
658+
} else {
659+
/* let's recover it by keeping all nodes until the last one */
660+
const struct settings_hash_linked_list settings_element = {
661+
.previous_hash = cf->second_to_last_hash_id,
662+
.next_hash = 0};
663+
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
664+
sizeof(struct settings_hash_linked_list));
665+
if (rc < 0) {
666+
return rc;
667+
}
668+
}
654669
return 0;
655670
} else if (rc < 0) {
656671
return rc;

0 commit comments

Comments
 (0)