@@ -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
@@ -182,11 +190,6 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
182190 return rc ;
183191 }
184192
185- /* Now delete the current linked list element */
186- rc = zms_delete (& cf -> cf_zms , name_hash | 1 );
187- if (rc < 0 ) {
188- return rc ;
189- }
190193#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
191194#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
192195 /* Update the flag of the Settings entry in cache. */
@@ -513,16 +516,13 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
513516
514517 /* write the name if required */
515518 if (write_name ) {
516- rc = zms_write (& cf -> cf_zms , name_hash , name , strlen (name ));
517- if (rc < 0 ) {
518- return rc ;
519- }
519+ /* First let's update the linked list */
520520#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
521521 if (ll_node_exist ) {
522522 goto no_ll_update ;
523523 }
524524 /* verify that the ll_node doesn't exist otherwise do not update it */
525- rc = zms_read (& cf -> cf_zms , name_hash | 1 , & settings_element ,
525+ rc = zms_read (& cf -> cf_zms , ZMS_LL_NODE_FROM_NAME_ID ( name_hash ) , & settings_element ,
526526 sizeof (struct settings_hash_linked_list ));
527527 if (rc >= 0 ) {
528528 goto no_ll_update ;
@@ -544,7 +544,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
544544 }
545545 }
546546 settings_element .previous_hash = cf -> last_hash_id ;
547- rc = zms_write (& cf -> cf_zms , name_hash | 1 , & settings_element ,
547+ rc = zms_write (& cf -> cf_zms , ZMS_LL_NODE_FROM_NAME_ID ( name_hash ) , & settings_element ,
548548 sizeof (struct settings_hash_linked_list ));
549549 if (rc < 0 ) {
550550 return rc ;
@@ -555,25 +555,31 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
555555 }
556556#endif
557557 /* Now update the previous linked list element */
558- settings_element .next_hash = name_hash | 1 ;
558+ settings_element .next_hash = ZMS_LL_NODE_FROM_NAME_ID ( name_hash ) ;
559559 settings_element .previous_hash = cf -> second_to_last_hash_id ;
560560 rc = zms_write (& cf -> cf_zms , cf -> last_hash_id , & settings_element ,
561561 sizeof (struct settings_hash_linked_list ));
562562 if (rc < 0 ) {
563563 return rc ;
564564 }
565565 cf -> second_to_last_hash_id = cf -> last_hash_id ;
566- cf -> last_hash_id = name_hash | 1 ;
566+ cf -> last_hash_id = ZMS_LL_NODE_FROM_NAME_ID ( name_hash ) ;
567567#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
568568 if ((cf -> ll_cache_next <= CONFIG_SETTINGS_ZMS_LL_CACHE_SIZE ) &&
569569 (cf -> ll_cache_next > 1 )) {
570570 cf -> ll_cache [cf -> ll_cache_next - 2 ] = settings_element ;
571571 }
572572#endif
573- }
574573#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
575574no_ll_update :
576575#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
576+ /* Now let's write the name */
577+ rc = zms_write (& cf -> cf_zms , name_hash , name , strlen (name ));
578+ if (rc < 0 ) {
579+ return rc ;
580+ }
581+ }
582+
577583#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
578584 /* Add the flags of the written settings entry in cache */
579585 cache_flags = 0 ;
@@ -590,6 +596,7 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
590596{
591597 struct settings_hash_linked_list settings_element ;
592598 uint32_t ll_last_hash_id = ZMS_LL_HEAD_HASH_ID ;
599+ uint32_t previous_ll_hash_id = 0 ;
593600 int rc = 0 ;
594601
595602#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
@@ -621,6 +628,27 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
621628 return rc ;
622629 }
623630
631+ if (settings_element .previous_hash != previous_ll_hash_id ) {
632+ /* This is a special case that can happen when a power down occurred
633+ * when deleting a linked list node.
634+ * If the power down occurred after updating the previous linked list node,
635+ * then we would end up with a state where the previous_hash of the linked
636+ * list is broken. Let's recover from this
637+ */
638+ rc = zms_delete (& cf -> cf_zms , settings_element .previous_hash );
639+ if (rc < 0 ) {
640+ return rc ;
641+ }
642+ /* Now recover the linked list */
643+ settings_element .previous_hash = previous_ll_hash_id ;
644+ zms_write (& cf -> cf_zms , ll_last_hash_id , & settings_element ,
645+ sizeof (struct settings_hash_linked_list ));
646+ if (rc < 0 ) {
647+ return rc ;
648+ }
649+ }
650+ previous_ll_hash_id = ll_last_hash_id ;
651+
624652#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
625653 if (cf -> ll_cache_next <= CONFIG_SETTINGS_ZMS_LL_CACHE_SIZE ) {
626654 cf -> ll_cache [cf -> ll_cache_next ++ ] = settings_element ;
0 commit comments