Skip to content

Commit 59b4ad2

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]>
1 parent f4bac16 commit 59b4ad2

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
@@ -608,21 +608,36 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
608608
rc = zms_read(&cf->cf_zms, ll_last_hash_id, &settings_element,
609609
sizeof(settings_element));
610610
if (rc == -ENOENT) {
611-
/* header doesn't exist or linked list broken, reinitialize the header */
612-
const struct settings_hash_linked_list settings_element = {
613-
.previous_hash = 0, .next_hash = 0};
614-
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
615-
sizeof(struct settings_hash_linked_list));
616-
if (rc < 0) {
617-
return rc;
618-
}
619-
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
620-
cf->second_to_last_hash_id = 0;
611+
/* header doesn't exist or linked list broken, reinitialize the header
612+
* if it doesn't exist and recover it if it is broken
613+
*/
614+
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
615+
/* header doesn't exist */
616+
const struct settings_hash_linked_list settings_element = {
617+
.previous_hash = 0, .next_hash = 0};
618+
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
619+
sizeof(struct settings_hash_linked_list));
620+
if (rc < 0) {
621+
return rc;
622+
}
623+
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
624+
cf->second_to_last_hash_id = 0;
621625
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
622-
/* store the LL header in cache */
623-
cf->ll_cache_next = 0;
624-
cf->ll_cache[cf->ll_cache_next++] = settings_element;
626+
/* store the LL header in cache */
627+
cf->ll_cache_next = 0;
628+
cf->ll_cache[cf->ll_cache_next++] = settings_element;
625629
#endif
630+
} else {
631+
/* let's recover it by keeping all nodes until the last one */
632+
const struct settings_hash_linked_list settings_element = {
633+
.previous_hash = cf->second_to_last_hash_id,
634+
.next_hash = 0};
635+
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
636+
sizeof(struct settings_hash_linked_list));
637+
if (rc < 0) {
638+
return rc;
639+
}
640+
}
626641
return 0;
627642
} else if (rc < 0) {
628643
return rc;

0 commit comments

Comments
 (0)