public inbox for linux-erofs@ozlabs.org
 help / color / mirror / Atom feed
From: Ajay Rajera <newajay.11r@gmail.com>
To: linux-erofs@lists.ozlabs.org
Cc: xiang@kernel.org, Ajay Rajera <newajay.11r@gmail.com>
Subject: [PATCH 1/2] erofs-utils: lib: replace bool locked with erofs_mutex_t for MT safety
Date: Thu, 19 Mar 2026 19:09:47 +0530	[thread overview]
Message-ID: <20260319133948.396-1-newajay.11r@gmail.com> (raw)

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@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



             reply	other threads:[~2026-03-19 13:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-19 13:39 Ajay Rajera [this message]
2026-03-19 13:39 ` [PATCH 2/2] erofs-utils: lib: fix meta_blkaddr handling for 48-bit layout Ajay Rajera
2026-03-19 13:45   ` Gao Xiang
2026-03-19 16:53     ` Ajay
2026-03-19 13:42 ` [PATCH 1/2] erofs-utils: lib: replace bool locked with erofs_mutex_t for MT safety Gao Xiang
2026-03-19 16:05   ` Ajay
2026-03-19 13:44 ` Ajay
2026-03-19 15:57 ` [PATCH v2 " Ajay Rajera

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260319133948.396-1-newajay.11r@gmail.com \
    --to=newajay.11r@gmail.com \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=xiang@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox