Skip to content

Commit e85fe14

Browse files
committed
[nrf fromlist] settings: zms: add more robustness to the save function
When a power off happens after writing the settings name and before writing the linked list node we cannot write the settings name again in the future. Fix this by writing the linked list node before writing the name. When loading all settings, we already delete linked list node that do not have any name or value written. Adds as well a recover path if a power down happens in the middle of unlinking an LL node after a delete. Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit 3e983a8efecd71edcaba8bf0d762b781803594ca)
1 parent b68765c commit e85fe14

File tree

2 files changed

+65
-36
lines changed

2 files changed

+65
-36
lines changed

subsys/settings/include/settings/settings_zms.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ extern "C" {
7272

7373
/* some useful macros */
7474
#define ZMS_NAME_ID_FROM_LL_NODE(x) (x & ~BIT(0))
75+
#define ZMS_LL_NODE_FROM_NAME_ID(x) (x | BIT(0))
7576
#define ZMS_UPDATE_COLLISION_NUM(x, y) \
7677
((x & ~ZMS_COLLISIONS_MASK) | ((y << 1) & ZMS_COLLISIONS_MASK))
7778
#define ZMS_COLLISION_NUM(x) ((x & ZMS_COLLISIONS_MASK) >> 1)

subsys/settings/src/settings_zms.c

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,39 @@ static int settings_zms_unlink_ll_node(struct settings_zms *cf, uint32_t name_ha
7272
struct settings_hash_linked_list settings_update_element;
7373

7474
/* let's update the linked list */
75-
rc = zms_read(&cf->cf_zms, name_hash | 1, &settings_element,
75+
rc = zms_read(&cf->cf_zms, ZMS_LL_NODE_FROM_NAME_ID(name_hash), &settings_element,
7676
sizeof(struct settings_hash_linked_list));
7777
if (rc < 0) {
7878
return rc;
7979
}
80+
81+
/* update the previous element */
82+
if (settings_element.previous_hash) {
83+
rc = zms_read(&cf->cf_zms, settings_element.previous_hash, &settings_update_element,
84+
sizeof(struct settings_hash_linked_list));
85+
if (rc < 0) {
86+
return rc;
87+
}
88+
if (!settings_element.next_hash) {
89+
/* we are deleting the last element of the linked list,
90+
* let's update the second_to_last_hash_id
91+
*/
92+
cf->second_to_last_hash_id = settings_update_element.previous_hash;
93+
}
94+
settings_update_element.next_hash = settings_element.next_hash;
95+
rc = zms_write(&cf->cf_zms, settings_element.previous_hash,
96+
&settings_update_element, sizeof(struct settings_hash_linked_list));
97+
if (rc < 0) {
98+
return rc;
99+
}
100+
}
101+
102+
/* Now delete the current linked list element */
103+
rc = zms_delete(&cf->cf_zms, ZMS_LL_NODE_FROM_NAME_ID(name_hash));
104+
if (rc < 0) {
105+
return rc;
106+
}
107+
80108
/* update the next element */
81109
if (settings_element.next_hash) {
82110
rc = zms_read(&cf->cf_zms, settings_element.next_hash, &settings_update_element,
@@ -100,28 +128,8 @@ static int settings_zms_unlink_ll_node(struct settings_zms *cf, uint32_t name_ha
100128
*/
101129
cf->last_hash_id = settings_element.previous_hash;
102130
}
103-
/* update the previous element */
104-
if (settings_element.previous_hash) {
105-
rc = zms_read(&cf->cf_zms, settings_element.previous_hash, &settings_update_element,
106-
sizeof(struct settings_hash_linked_list));
107-
if (rc < 0) {
108-
return rc;
109-
}
110-
if (!settings_element.next_hash) {
111-
/* we are deleting the last element of the linked list,
112-
* let's update the second_to_last_hash_id
113-
*/
114-
cf->second_to_last_hash_id = settings_update_element.previous_hash;
115-
}
116-
settings_update_element.next_hash = settings_element.next_hash;
117-
rc = zms_write(&cf->cf_zms, settings_element.previous_hash,
118-
&settings_update_element, sizeof(struct settings_hash_linked_list));
119-
if (rc < 0) {
120-
return rc;
121-
}
122-
}
123131

124-
return rc;
132+
return 0;
125133
}
126134
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
127135

@@ -183,11 +191,6 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
183191
return rc;
184192
}
185193

186-
/* Now delete the current linked list element */
187-
rc = zms_delete(&cf->cf_zms, name_hash | 1);
188-
if (rc < 0) {
189-
return rc;
190-
}
191194
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
192195
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
193196
/* Update the flag of the Settings entry in cache. */
@@ -539,16 +542,13 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
539542

540543
/* write the name if required */
541544
if (write_name) {
542-
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
543-
if (rc < 0) {
544-
return rc;
545-
}
545+
/* First let's update the linked list */
546546
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
547547
if (ll_node_exist) {
548548
goto no_ll_update;
549549
}
550550
/* verify that the ll_node doesn't exist otherwise do not update it */
551-
rc = zms_read(&cf->cf_zms, name_hash | 1, &settings_element,
551+
rc = zms_read(&cf->cf_zms, ZMS_LL_NODE_FROM_NAME_ID(name_hash), &settings_element,
552552
sizeof(struct settings_hash_linked_list));
553553
if (rc >= 0) {
554554
goto no_ll_update;
@@ -570,7 +570,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
570570
}
571571
}
572572
settings_element.previous_hash = cf->last_hash_id;
573-
rc = zms_write(&cf->cf_zms, name_hash | 1, &settings_element,
573+
rc = zms_write(&cf->cf_zms, ZMS_LL_NODE_FROM_NAME_ID(name_hash), &settings_element,
574574
sizeof(struct settings_hash_linked_list));
575575
if (rc < 0) {
576576
return rc;
@@ -582,25 +582,31 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
582582
}
583583
#endif
584584
/* Now update the previous linked list element */
585-
settings_element.next_hash = name_hash | 1;
585+
settings_element.next_hash = ZMS_LL_NODE_FROM_NAME_ID(name_hash);
586586
settings_element.previous_hash = cf->second_to_last_hash_id;
587587
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
588588
sizeof(struct settings_hash_linked_list));
589589
if (rc < 0) {
590590
return rc;
591591
}
592592
cf->second_to_last_hash_id = cf->last_hash_id;
593-
cf->last_hash_id = name_hash | 1;
593+
cf->last_hash_id = ZMS_LL_NODE_FROM_NAME_ID(name_hash);
594594
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
595595
if ((cf->ll_cache_next < CONFIG_SETTINGS_ZMS_LL_CACHE_SIZE) &&
596596
(cf->ll_cache_next > 1)) {
597597
cf->ll_cache[cf->ll_cache_next - 2] = settings_element;
598598
}
599599
#endif
600-
}
601600
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
602601
no_ll_update:
603602
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
603+
/* Now let's write the name */
604+
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
605+
if (rc < 0) {
606+
return rc;
607+
}
608+
}
609+
604610
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
605611
/* Add the flags of the written settings entry in cache */
606612
cache_flags = 0;
@@ -617,6 +623,7 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
617623
{
618624
struct settings_hash_linked_list settings_element;
619625
uint32_t ll_last_hash_id = ZMS_LL_HEAD_HASH_ID;
626+
uint32_t previous_ll_hash_id = 0;
620627
int rc = 0;
621628

622629
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
@@ -649,6 +656,27 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
649656
return rc;
650657
}
651658

659+
if (settings_element.previous_hash != previous_ll_hash_id) {
660+
/* This is a special case that can happen when a power down occurred
661+
* when deleting a linked list node.
662+
* If the power down occurred after updating the previous linked list node,
663+
* then we would end up with a state where the previous_hash of the linked
664+
* list is broken. Let's recover from this
665+
*/
666+
rc = zms_delete(&cf->cf_zms, settings_element.previous_hash);
667+
if (rc < 0) {
668+
return rc;
669+
}
670+
/* Now recover the linked list */
671+
settings_element.previous_hash = previous_ll_hash_id;
672+
zms_write(&cf->cf_zms, ll_last_hash_id, &settings_element,
673+
sizeof(struct settings_hash_linked_list));
674+
if (rc < 0) {
675+
return rc;
676+
}
677+
}
678+
previous_ll_hash_id = ll_last_hash_id;
679+
652680
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
653681
if (cf->ll_cache_next < CONFIG_SETTINGS_ZMS_LL_CACHE_SIZE) {
654682
cf->ll_cache[cf->ll_cache_next] = settings_element;

0 commit comments

Comments
 (0)