[PATCH v2 1/2] erofs-utils: lib: replace bool locked with erofs_mutex_t for MT safety

Ajay Rajera newajay.11r at gmail.com
Fri Mar 20 02:57:53 AEDT 2026


Replace the bool locked field in erofs_diskbufstrm with
erofs_mutex_t lock to provide proper mutual exclusion
for multi-threaded disk buffer operations.

This addresses the TODO comment 'need a real lock for MT'
by using the erofs mutex API (erofs_mutex_lock/
erofs_mutex_unlock/erofs_mutex_init) instead of a simple
boolean flag that provided no actual synchronization.

Signed-off-by: Ajay Rajera <newajay.11r at gmail.com>
---
 lib/diskbuf.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/diskbuf.c b/lib/diskbuf.c
index 0bf42da..4218df8 100644
--- a/lib/diskbuf.c
+++ b/lib/diskbuf.c
@@ -3,6 +3,7 @@
 #include "erofs/internal.h"
 #include "erofs/print.h"
 #include <stdio.h>
+#include "erofs/lock.h"
 #include <errno.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -14,7 +15,7 @@ static struct erofs_diskbufstrm {
 	u64 tailoffset, devpos;
 	int fd;
 	unsigned int alignsize;
-	bool locked;
+	erofs_mutex_t lock;
 } *dbufstrm;
 
 int erofs_diskbuf_getfd(struct erofs_diskbuf *db, u64 *fpos)
@@ -34,6 +35,7 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off)
 {
 	struct erofs_diskbufstrm *strm = dbufstrm + sid;
 
+	erofs_mutex_lock(&strm->lock);
 	if (strm->tailoffset & (strm->alignsize - 1)) {
 		strm->tailoffset = round_up(strm->tailoffset, strm->alignsize);
 	}
@@ -42,7 +44,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off)
 		*off = db->offset + strm->devpos;
 	db->sp = strm;
 	(void)erofs_atomic_inc_return(&strm->count);
-	strm->locked = true;	/* TODO: need a real lock for MT */
 	return strm->fd;
 }
 
@@ -51,9 +52,9 @@ void erofs_diskbuf_commit(struct erofs_diskbuf *db, u64 len)
 	struct erofs_diskbufstrm *strm = db->sp;
 
 	DBG_BUGON(!strm);
-	DBG_BUGON(!strm->locked);
 	DBG_BUGON(strm->tailoffset != db->offset);
 	strm->tailoffset += len;
+	erofs_mutex_unlock(&strm->lock);
 }
 
 void erofs_diskbuf_close(struct erofs_diskbuf *db)
@@ -115,6 +116,7 @@ int erofs_diskbuf_init(unsigned int nstrms)
 setupone:
 		strm->tailoffset = 0;
 		erofs_atomic_set(&strm->count, 1);
+		erofs_mutex_init(&strm->lock);
 		if (fstat(strm->fd, &st))
 			return -errno;
 		strm->alignsize = max_t(u32, st.st_blksize, getpagesize());
-- 
2.51.0.windows.1



More information about the Linux-erofs mailing list