[PATCH 2/3] erofs-utils: lib: avoid using lseek in diskbuf

Yifan Zhao zhaoyifan28 at huawei.com
Fri Sep 19 01:12:44 AEST 2025


From: zhaoyifan <zhaoyifan28 at huawei.com>

The current `diskbuf` implementation uses `lseek` to operate file offset,
preventing multiple streams from writing to the same file. Let's replace
`write` + `lseek` with `pwrite` to enable this use pattern.

Signed-off-by: Yifan Zhao <zhaoyifan28 at huawei.com>
---
 lib/diskbuf.c | 7 +------
 lib/tar.c     | 2 +-
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/lib/diskbuf.c b/lib/diskbuf.c
index 3789654..0bf42da 100644
--- a/lib/diskbuf.c
+++ b/lib/diskbuf.c
@@ -36,9 +36,6 @@ int erofs_diskbuf_reserve(struct erofs_diskbuf *db, int sid, u64 *off)
 
 	if (strm->tailoffset & (strm->alignsize - 1)) {
 		strm->tailoffset = round_up(strm->tailoffset, strm->alignsize);
-		if (lseek(strm->fd, strm->tailoffset + strm->devpos,
-			  SEEK_SET) != strm->tailoffset + strm->devpos)
-			return -EIO;
 	}
 	db->offset = strm->tailoffset;
 	if (off)
@@ -108,9 +105,6 @@ int erofs_diskbuf_init(unsigned int nstrms)
 			strm->devpos = 1ULL << 40;
 			if (!ftruncate(g_sbi.bdev.fd, strm->devpos << 1)) {
 				strm->fd = dup(g_sbi.bdev.fd);
-				if (lseek(strm->fd, strm->devpos,
-					  SEEK_SET) != strm->devpos)
-					return -EIO;
 				goto setupone;
 			}
 		}
@@ -141,4 +135,5 @@ void erofs_diskbuf_exit(void)
 		close(strm->fd);
 		strm->fd = -1;
 	}
+	free(dbufstrm);
 }
diff --git a/lib/tar.c b/lib/tar.c
index 8d068cb..44714e0 100644
--- a/lib/tar.c
+++ b/lib/tar.c
@@ -675,7 +675,7 @@ static int tarerofs_write_file_data(struct erofs_inode *inode,
 		nread = erofs_iostream_read(&tar->ios, &buf, j);
 		if (nread < 0)
 			break;
-		if (write(fd, buf, nread) != nread) {
+		if (pwrite(fd, buf, nread, off) != nread) {
 			nread = -EIO;
 			break;
 		}
-- 
2.46.0



More information about the Linux-erofs mailing list