Skip to content

Commit 6279943

Browse files
rghaddabkartben
authored andcommitted
settings: zms: recover linked list if broken
When the linked list is broken, recover it instead of reinitializing it. Signed-off-by: Riadh Ghaddab <rghaddab@baylibre.com>
1 parent 5a978f4 commit 6279943

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
@@ -527,9 +527,6 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
527527
return rc;
528528
}
529529
}
530-
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
531-
no_ll_update:
532-
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
533530
return 0;
534531
}
535532

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

0 commit comments

Comments
 (0)