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

Ajay newajay.11r at gmail.com
Fri Mar 20 03:05:13 AEDT 2026


👍

Ajay reacted via Gmail
<https://www.google.com/gmail/about/?utm_source=gmail-in-product&utm_medium=et&utm_campaign=emojireactionemail#app>

On Thu, 19 Mar 2026 at 19:12, Gao Xiang <hsiangkao at linux.alibaba.com> wrote:

> 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());
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/vnd.google.email-reaction+json
Size: 37 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/linux-erofs/attachments/20260319/04819258/attachment.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/linux-erofs/attachments/20260319/04819258/attachment.htm>


More information about the Linux-erofs mailing list