Skip to content

Commit 5a978f4

Browse files
rghaddabkartben
authored andcommitted
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. Signed-off-by: Riadh Ghaddab <rghaddab@baylibre.com>
1 parent dffdec1 commit 5a978f4

File tree

2 files changed

+66
-36
lines changed

2 files changed

+66
-36
lines changed

subsys/settings/include/settings/settings_zms.h

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

6464
/* some useful macros */
6565
#define ZMS_NAME_ID_FROM_LL_NODE(x) (x & ~BIT(0))
66+
#define ZMS_LL_NODE_FROM_NAME_ID(x) (x | BIT(0))
6667
#define ZMS_UPDATE_COLLISION_NUM(x, y) \
6768
((x & ~ZMS_COLLISIONS_MASK) | ((y << 1) & ZMS_COLLISIONS_MASK))
6869
#define ZMS_COLLISION_NUM(x) ((x & ZMS_COLLISIONS_MASK) >> 1)

subsys/settings/src/settings_zms.c

Lines changed: 65 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

@@ -146,12 +154,6 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
146154
return rc;
147155
}
148156

149-
/* Now delete the current linked list element */
150-
rc = zms_delete(&cf->cf_zms, name_hash | 1);
151-
if (rc < 0) {
152-
return rc;
153-
}
154-
155157
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
156158
return rc;
157159
}
@@ -469,13 +471,10 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
469471

470472
/* write the name if required */
471473
if (write_name) {
472-
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
473-
if (rc < 0) {
474-
return rc;
475-
}
474+
/* First let's update the linked list */
476475
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
477476
/* verify that the ll_node doesn't exist otherwise do not update it */
478-
rc = zms_read(&cf->cf_zms, name_hash | 1, &settings_element,
477+
rc = zms_read(&cf->cf_zms, ZMS_LL_NODE_FROM_NAME_ID(name_hash), &settings_element,
479478
sizeof(struct settings_hash_linked_list));
480479
if (rc >= 0) {
481480
goto no_ll_update;
@@ -497,28 +496,36 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
497496
}
498497
}
499498
settings_element.previous_hash = cf->last_hash_id;
500-
rc = zms_write(&cf->cf_zms, name_hash | 1, &settings_element,
499+
rc = zms_write(&cf->cf_zms, ZMS_LL_NODE_FROM_NAME_ID(name_hash), &settings_element,
501500
sizeof(struct settings_hash_linked_list));
502501
if (rc < 0) {
503502
return rc;
504503
}
505504

506505
/* Now update the previous linked list element */
507-
settings_element.next_hash = name_hash | 1;
506+
settings_element.next_hash = ZMS_LL_NODE_FROM_NAME_ID(name_hash);
508507
settings_element.previous_hash = cf->second_to_last_hash_id;
509508
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
510509
sizeof(struct settings_hash_linked_list));
511510
if (rc < 0) {
512511
return rc;
513512
}
514513
cf->second_to_last_hash_id = cf->last_hash_id;
515-
cf->last_hash_id = name_hash | 1;
514+
cf->last_hash_id = ZMS_LL_NODE_FROM_NAME_ID(name_hash);
516515
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
517516
if (cf->ll_cache_next < CONFIG_SETTINGS_ZMS_LL_CACHE_SIZE) {
518517
cf->ll_cache[cf->ll_cache_next] = settings_element;
519518
cf->ll_cache_next = cf->ll_cache_next + 1;
520519
}
521520
#endif
521+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
522+
no_ll_update:
523+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
524+
/* Now let's write the name */
525+
rc = zms_write(&cf->cf_zms, name_hash, name, strlen(name));
526+
if (rc < 0) {
527+
return rc;
528+
}
522529
}
523530
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
524531
no_ll_update:
@@ -530,6 +537,7 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
530537
{
531538
struct settings_hash_linked_list settings_element;
532539
uint32_t ll_last_hash_id = ZMS_LL_HEAD_HASH_ID;
540+
uint32_t previous_ll_hash_id = 0;
533541
int rc = 0;
534542

535543
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
@@ -555,6 +563,27 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
555563
return rc;
556564
}
557565

566+
if (settings_element.previous_hash != previous_ll_hash_id) {
567+
/* This is a special case that can happen when a power down occurred
568+
* when deleting a linked list node.
569+
* If the power down occurred after updating the previous linked list node,
570+
* then we would end up with a state where the previous_hash of the linked
571+
* list is broken. Let's recover from this
572+
*/
573+
rc = zms_delete(&cf->cf_zms, settings_element.previous_hash);
574+
if (rc < 0) {
575+
return rc;
576+
}
577+
/* Now recover the linked list */
578+
settings_element.previous_hash = previous_ll_hash_id;
579+
zms_write(&cf->cf_zms, ll_last_hash_id, &settings_element,
580+
sizeof(struct settings_hash_linked_list));
581+
if (rc < 0) {
582+
return rc;
583+
}
584+
}
585+
previous_ll_hash_id = ll_last_hash_id;
586+
558587
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
559588
if ((cf->ll_cache_next < CONFIG_SETTINGS_ZMS_LL_CACHE_SIZE) &&
560589
(settings_element.next_hash)) {

0 commit comments

Comments
 (0)