[v2 PATCH 2/3] erofs-utils: lib: avoid using lseek in diskbuf
Yifan Zhao
zhaoyifan28 at huawei.com
Tue Sep 23 12:46:07 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>
---
update since v1:
- missing `off += nread;` in v1, add it.
lib/diskbuf.c | 7 +------
lib/tar.c | 3 ++-
2 files changed, 3 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..b778081 100644
--- a/lib/tar.c
+++ b/lib/tar.c
@@ -675,11 +675,12 @@ 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;
}
j -= nread;
+ off += nread;
}
erofs_diskbuf_commit(inode->i_diskbuf, inode->i_size);
inode->datasource = EROFS_INODE_DATA_SOURCE_DISKBUF;
--
2.46.0
More information about the Linux-erofs
mailing list