This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new edcbf96df41 fs/romfs: use a per-file lock for read and seek with XIP
media
edcbf96df41 is described below
commit edcbf96df41db0455480fdd8a47faf4a3adc6ac9
Author: ligd <[email protected]>
AuthorDate: Mon Nov 23 18:09:03 2026 +0800
fs/romfs: use a per-file lock for read and seek with XIP media
romfs_read() and romfs_seek() take the mount-level rmutex, so reads of
different files on the same mount are serialized.
With XIP media a read only copies from the mapped address and touches
only the per-file state (f_pos, rf_buffer, rf_cachesector). Take a new
per-file mutex there instead, so reads of different files run in
parallel while threads that share one open file are still serialized.
Without XIP the read goes through the block driver, which may rely on
the file system to serialize its requests (the FTL read-ahead buffer is
not locked without CONFIG_DRVR_WRITEBUFFER), so keep the mount lock in
that case.
Signed-off-by: liguiding1 <[email protected]>
Signed-off-by: ligd <[email protected]>
Signed-off-by: jingfei <[email protected]>
---
fs/romfs/fs_romfs.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++----
fs/romfs/fs_romfs.h | 1 +
2 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c
index dfff39220fd..d5e9287e322 100644
--- a/fs/romfs/fs_romfs.c
+++ b/fs/romfs/fs_romfs.c
@@ -156,6 +156,46 @@ const struct mountpt_operations g_romfs_operations =
* Private Functions
****************************************************************************/
+/****************************************************************************
+ * Name: romfs_rdlock
+ *
+ * Description:
+ * Take the lock that serializes read() and lseek() on one open file. In
+ * XIP mode a read only copies from the mapped media, so the lock of the
+ * file is enough and reads of other files proceed in parallel. Otherwise
+ * the read goes to the block driver, which may rely on the file system to
+ * serialize its requests, so the mount lock is taken.
+ *
+ ****************************************************************************/
+
+static int romfs_rdlock(FAR struct romfs_mountpt_s *rm,
+ FAR struct romfs_file_s *rf)
+{
+ if (rm->rm_xipbase != NULL)
+ {
+ return nxmutex_lock(&rf->rf_lock);
+ }
+
+ return nxrmutex_lock(&rm->rm_lock);
+}
+
+/****************************************************************************
+ * Name: romfs_rdunlock
+ ****************************************************************************/
+
+static void romfs_rdunlock(FAR struct romfs_mountpt_s *rm,
+ FAR struct romfs_file_s *rf)
+{
+ if (rm->rm_xipbase != NULL)
+ {
+ nxmutex_unlock(&rf->rf_lock);
+ }
+ else
+ {
+ nxrmutex_unlock(&rm->rm_lock);
+ }
+}
+
/****************************************************************************
* Name: romfs_open
****************************************************************************/
@@ -291,6 +331,8 @@ static int romfs_open(FAR struct file *filep, FAR const
char *relpath,
goto errout_with_lock;
}
+ nxmutex_init(&rf->rf_lock);
+
/* Attach the private date to the struct file instance */
filep->f_priv = rf;
@@ -352,6 +394,7 @@ static int romfs_close(FAR struct file *filep)
/* Then free the file structure itself. */
+ nxmutex_destroy(&rf->rf_lock);
fs_heap_free(rf);
filep->f_priv = NULL;
return ret;
@@ -391,7 +434,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char
*buffer,
/* Make sure that the mount is still healthy */
- ret = nxrmutex_lock(&rm->rm_lock);
+ ret = romfs_rdlock(rm, rf);
if (ret < 0)
{
return (ssize_t)ret;
@@ -495,7 +538,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char
*buffer,
}
errout_with_lock:
- nxrmutex_unlock(&rm->rm_lock);
+ romfs_rdunlock(rm, rf);
return readsize ? readsize : ret;
}
@@ -550,7 +593,7 @@ static off_t romfs_seek(FAR struct file *filep, off_t
offset, int whence)
/* Make sure that the mount is still healthy */
- ret = nxrmutex_lock(&rm->rm_lock);
+ ret = romfs_rdlock(rm, rf);
if (ret < 0)
{
return (off_t)ret;
@@ -585,7 +628,7 @@ static off_t romfs_seek(FAR struct file *filep, off_t
offset, int whence)
finfo("New file position: %jd\n", (intmax_t)filep->f_pos);
errout_with_lock:
- nxrmutex_unlock(&rm->rm_lock);
+ romfs_rdunlock(rm, rf);
return ret < 0 ? ret : position;
}
@@ -748,6 +791,8 @@ static int romfs_dup(FAR const struct file *oldp, FAR
struct file *newp)
goto errout_with_lock;
}
+ nxmutex_init(&newrf->rf_lock);
+
/* Attach the new private date to the new struct file instance */
newp->f_priv = newrf;
diff --git a/fs/romfs/fs_romfs.h b/fs/romfs/fs_romfs.h
index 34c4846adaa..d549d033281 100644
--- a/fs/romfs/fs_romfs.h
+++ b/fs/romfs/fs_romfs.h
@@ -157,6 +157,7 @@ struct romfs_file_s
uint32_t rf_ncachesector; /* Number of sectors in the rf_buffer */
FAR uint8_t *rf_buffer; /* File sector buffer, allocated if
rm_xipbase==0 */
uint8_t rf_type; /* File type (for fstat()) */
+ mutex_t rf_lock; /* Serializes read/seek in XIP mode */
char rf_path[1]; /* Path of open file */
};