This is an automated email from the ASF dual-hosted git repository.

janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 46979bc7a fs/fcb: Fix FCB  async issues
46979bc7a is described below

commit 46979bc7adbd4d2acc20aee07b41a216275a1557
Author: Vipul Rahane <[email protected]>
AuthorDate: Thu Mar 20 01:28:42 2025 -0700

    fs/fcb: Fix FCB  async issues
    
    FCB API can be called from multiple threads. Add missing mutex locking.
---
 fs/fcb/include/fcb/fcb.h |  8 +++++++
 fs/fcb/src/fcb.c         | 59 +++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/fs/fcb/include/fcb/fcb.h b/fs/fcb/include/fcb/fcb.h
index 16d97305a..09d129a1e 100644
--- a/fs/fcb/include/fcb/fcb.h
+++ b/fs/fcb/include/fcb/fcb.h
@@ -216,11 +216,19 @@ int fcb_append_to_scratch(struct fcb *);
 
 /**
  * How many sectors are unused.
+ *
+ * @param fcb - fcb to check
+ * @return number of free sectors if successful,
+ *  negative value on error
  */
 int fcb_free_sector_cnt(struct fcb *fcb);
 
 /**
  * Whether FCB has any data.
+ *
+ * @param fcb - fcb to check
+ * @return 1 if FCB is empty, 0 if FCB has data,
+ *  negative value on error
  */
 int fcb_is_empty(struct fcb *fcb);
 
diff --git a/fs/fcb/src/fcb.c b/fs/fcb/src/fcb.c
index 89856ef69..1494f9a53 100644
--- a/fs/fcb/src/fcb.c
+++ b/fs/fcb/src/fcb.c
@@ -110,6 +110,12 @@ fcb_free_sector_cnt(struct fcb *fcb)
 {
     int i;
     struct flash_area *fa;
+    int rc = 0;
+
+    rc = os_mutex_pend(&fcb->f_mtx, OS_WAIT_FOREVER);
+    if (rc && rc != OS_NOT_STARTED) {
+        return FCB_ERR_ARGS;
+    }
 
     fa = fcb->f_active.fe_area;
     for (i = 0; i < fcb->f_sector_cnt; i++) {
@@ -118,14 +124,40 @@ fcb_free_sector_cnt(struct fcb *fcb)
             break;
         }
     }
+
+    os_mutex_release(&fcb->f_mtx);
+
     return i;
 }
 
+int
+fcb_is_empty_nolock(struct fcb *fcb)
+{
+    bool ret = false;
+
+    ret = (fcb->f_active.fe_area == fcb->f_oldest &&
+           fcb->f_active.fe_elem_off == sizeof(struct fcb_disk_area));
+
+    return ret;
+}
+
 int
 fcb_is_empty(struct fcb *fcb)
 {
-    return (fcb->f_active.fe_area == fcb->f_oldest &&
+    int rc = 0;
+    bool ret = false;
+
+    rc = os_mutex_pend(&fcb->f_mtx, OS_WAIT_FOREVER);
+    if (rc && rc != OS_NOT_STARTED) {
+        return FCB_ERR_ARGS;
+    }
+
+    ret = (fcb->f_active.fe_area == fcb->f_oldest &&
       fcb->f_active.fe_elem_off == sizeof(struct fcb_disk_area));
+
+    os_mutex_release(&fcb->f_mtx);
+
+    return ret;
 }
 
 /**
@@ -226,8 +258,14 @@ fcb_offset_last_n(struct fcb *fcb, uint8_t entries,
         struct fcb_entry *last_n_entry)
 {
     struct fcb_entry loc;
+    int rc = 0;
     int i;
 
+    rc = os_mutex_pend(&fcb->f_mtx, OS_WAIT_FOREVER);
+    if (rc && rc != OS_NOT_STARTED) {
+        return FCB_ERR_ARGS;
+    }
+
     /* assure a minimum amount of entries */
     if (!entries) {
         entries = 1;
@@ -235,17 +273,19 @@ fcb_offset_last_n(struct fcb *fcb, uint8_t entries,
 
     i = 0;
     memset(&loc, 0, sizeof(loc));
-    while (!fcb_getnext(fcb, &loc)) {
+    while (!fcb_getnext_nolock(fcb, &loc)) {
         if (i == 0) {
             /* Start from the beginning of fcb entries */
             *last_n_entry = loc;
         } else if (i > (entries - 1)) {
             /* Update last_n_entry after n entries and keep updating */
-            fcb_getnext(fcb, last_n_entry);
+            fcb_getnext_nolock(fcb, last_n_entry);
         }
         i++;
     }
 
+    os_mutex_release(&fcb->f_mtx);
+
     return (i == 0) ? FCB_ERR_NOVAR : 0;
 }
 
@@ -257,14 +297,21 @@ fcb_offset_last_n(struct fcb *fcb, uint8_t entries,
 int
 fcb_clear(struct fcb *fcb)
 {
-    int rc;
+    int rc = 0;
+
+    rc = os_mutex_pend(&fcb->f_mtx, OS_WAIT_FOREVER);
+    if (rc && rc != OS_NOT_STARTED) {
+        return FCB_ERR_ARGS;
+    }
 
-    rc = 0;
-    while (!fcb_is_empty(fcb)) {
+    while (!fcb_is_empty_nolock(fcb)) {
         rc = fcb_rotate(fcb);
         if (rc) {
             break;
         }
     }
+
+    os_mutex_release(&fcb->f_mtx);
+
     return rc;
 }

Reply via email to