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

Gao Xiang hsiangkao at linux.alibaba.com
Fri Mar 20 00:42:49 AEDT 2026


Hi Ajay,

On 2026/3/19 21:39, Ajay Rajera wrote:
> 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.

The commit message should be 72-char at maximum each line.

Otherwise it seems a good improvement.

> 
> 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());



More information about the Linux-erofs mailing list